Home > Enterprise >  How can I run GHCi against a compiled package?
How can I run GHCi against a compiled package?

Time:11-19

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?

CodePudding user response:

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

cabal repl --repl-options -fobject-code --repl-options -O

This should give you incremental building of compiled code as you work in GHCi. One caveat is that the interaction between --repl-options -fobject-code and the rest of cabal seems to be, as of 3.6.2.0 at least, a tad messy. In particular:

  • Alternating between cabal repl and e.g. cabal build will often lead to rebuilds that should be unnecessary;

  • You have to specify the optimisation level on the command line, as otherwise the interpreter's default -O0 will override your package setting;

  • cabal build doesn't notice optimisation level changes specified in this way through cabal repl, so if you override your package default for a GHCi session, a subsequent cabal build won't rebuild your code to revert the optimisation level change.

CodePudding user response:

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.

  • Related