How can I run GHCi against a compiled package?

Viewed 173

I should really know this by now, but I don't. I'm often working on a Cabal-based package and have just run a successful cabal build. Now I want to try some things out in GHCi. If I run cabal repl, then GHC recompiles the whole package into bytecode and runs it in the interpreter. Not what I want at all! If I were just running GHCi directly, I'd use something like -O -fobject-code, but that won't give me the package context. I just want "Give me a repl with the package as it's been compiled, compiling additional things only as necessary." How do I do it?

2 Answers

I don't know the right way, but I do know a workaround that can sometimes be useful. If the thing you care about is a library component, you can ask for a repl for an executable component.

I believe --repl-options -fobject-code kind of does what you want:

cabal repl --repl-options -fobject-code --repl-options -O --builddir dist-repl 

This will give you incremental building of compiled code as you work in GHCi. Caveats:

  • dist-repl is an alternative directory for the -fobject-code build objects. As of cabal 3.6.2.0 at least, trying to reuse the regular output from cabal build leads to some unnecessary rebuilds and other strange behaviour, as reported at cabal issue #3565. That being so, it's better to compromise and use --builddir to keep a separate set of build objects. Note that cabal clean accepts the --builddir option just fine.

  • Setting the optimisation level explicitly is necessary, as otherwise the default -O0 from cabal repl will override your package setting.

Related