Running stack ghci appears to trigger a recompilation of all modules on every startup, even when invoked with the -fobject-code option (as documented here) or the --no-build option (as documented here).
$ stack new example
$ cd example
$ stack build
.stack-work/odir directory:$ ls .stack-work/
dist install
-fobject-code, and observe that it compiles all modules:$ stack ghci --ghc-options -fobject-code
The following GHC options are incompatible with GHCi and have not been passed to it: -threaded
Configuring GHCi with the following packages: example
Using main module: 1. Package `example' component exe:example-exe with main-is file: /home/jsk/example/app/Main.hs
GHCi, version 8.0.2: http://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling Lib ( /home/jsk/example/src/Lib.hs, /home/jsk/example/.stack-work/odir/Lib.o )
Ok, modules loaded: Lib (/home/jsk/example/.stack-work/odir/Lib.o).
[2 of 2] Compiling Main ( /home/jsk/example/app/Main.hs, /home/jsk/example/.stack-work/odir/Main.o )
Ok, modules loaded: Lib (/home/jsk/example/.stack-work/odir/Lib.o), Main (/home/jsk/example/.stack-work/odir/Main.o).
Loaded GHCi configuration from /tmp/ghci7387/ghci-script
Prelude Lib Main>
Leaving GHCi.
.stack-work/odir directory:$ ls .stack-work/
dist install odir
$ ls -la --full-time .stack-work/odir/
total 56
drwxrwxr-x 2 jsk jsk 4096 2017-08-15 21:43:15.468485195 +0800 .
drwxrwxr-x 5 jsk jsk 4096 2017-08-15 21:43:15.444485108 +0800 ..
-rw-rw-r-- 1 jsk jsk 823 2017-08-15 21:43:15.448485123 +0800 Lib.hi
-rw-rw-r-- 1 jsk jsk 3176 2017-08-15 21:43:15.456485152 +0800 Lib.o
-rw-rw-r-- 1 jsk jsk 858 2017-08-15 21:43:15.464485181 +0800 Main.hi
-rw-rw-r-- 1 jsk jsk 2800 2017-08-15 21:43:15.468485195 +0800 Main.o
$ stack ghci --ghc-options -fobject-code
The following GHC options are incompatible with GHCi and have not been passed to it: -threaded
Configuring GHCi with the following packages: example
Using main module: 1. Package `example' component exe:example-exe with main-is file: /home/jsk/example/app/Main.hs
GHCi, version 8.0.2: http://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling Lib ( /home/jsk/example/src/Lib.hs, /home/jsk/example/.stack-work/odir/Lib.o ) [flags changed]
Ok, modules loaded: Lib (/home/jsk/example/.stack-work/odir/Lib.o).
[2 of 2] Compiling Main ( /home/jsk/example/app/Main.hs, /home/jsk/example/.stack-work/odir/Main.o ) [flags changed]
Ok, modules loaded: Lib (/home/jsk/example/.stack-work/odir/Lib.o), Main (/home/jsk/example/.stack-work/odir/Main.o).
Loaded GHCi configuration from /tmp/ghci7865/ghci-script
Prelude Lib Main>
Leaving GHCi.
.stack-work/odir directory have changed:$ ls -la --full-time .stack-work/odir/
total 56
drwxrwxr-x 2 jsk jsk 4096 2017-08-15 21:44:23.392722165 +0800 .
drwxrwxr-x 5 jsk jsk 4096 2017-08-15 21:43:15.444485108 +0800 ..
-rw-rw-r-- 1 jsk jsk 823 2017-08-15 21:44:23.364722071 +0800 Lib.hi
-rw-rw-r-- 1 jsk jsk 3176 2017-08-15 21:44:23.376722112 +0800 Lib.o
-rw-rw-r-- 1 jsk jsk 858 2017-08-15 21:44:23.384722138 +0800 Main.hi
-rw-rw-r-- 1 jsk jsk 2800 2017-08-15 21:44:23.396722179 +0800 Main.o
--no-build option, observing again that modules are recompiled:$ stack ghci --no-build --ghc-options -fobject-code
The following GHC options are incompatible with GHCi and have not been passed to it: -threaded
Configuring GHCi with the following packages: example
Using main module: 1. Package `example' component exe:example-exe with main-is file: /home/jsk/example/app/Main.hs
GHCi, version 8.0.2: http://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling Lib ( /home/jsk/example/src/Lib.hs, /home/jsk/example/.stack-work/odir/Lib.o ) [flags changed]
Ok, modules loaded: Lib (/home/jsk/example/.stack-work/odir/Lib.o).
[2 of 2] Compiling Main ( /home/jsk/example/app/Main.hs, /home/jsk/example/.stack-work/odir/Main.o ) [flags changed]
Ok, modules loaded: Lib (/home/jsk/example/.stack-work/odir/Lib.o), Main (/home/jsk/example/.stack-work/odir/Main.o).
Loaded GHCi configuration from /tmp/ghci7920/ghci-script
Prelude Lib Main>
Leaving GHCi.
.stack-work/odir directory have changed:$ ls -la --full-time .stack-work/odir/
total 56
drwxrwxr-x 2 jsk jsk 4096 2017-08-15 21:46:26.325111175 +0800 .
drwxrwxr-x 5 jsk jsk 4096 2017-08-15 21:43:15.444485108 +0800 ..
-rw-rw-r-- 1 jsk jsk 823 2017-08-15 21:46:26.305111115 +0800 Lib.hi
-rw-rw-r-- 1 jsk jsk 3176 2017-08-15 21:46:26.313111138 +0800 Lib.o
-rw-rw-r-- 1 jsk jsk 858 2017-08-15 21:46:26.321111163 +0800 Main.hi
-rw-rw-r-- 1 jsk jsk 2800 2017-08-15 21:46:26.329111186 +0800 Main.o
During steps 6 and 8, GHCi reloads _without_ recompiling every module.
GHCi appears to recompile every module, reporting [flags changed].
See https://gist.github.com/jonathanknowles/fd9616126313c9d3633620114e4d1eac
$ stack --version
Version 1.5.1, Git revision 600c1f01435a10d127938709556c1682ecfd694e (4861 commits) x86_64 hpack-0.17.1
$ which stack
/home/user/.local/bin/stack
Is this behaviour by design?
If so, is there another way to get ghci to reload already-compiled modules on startup, when invoked with stack?
This behavior is not by design, it was an unintended consequence of using a random tmpdir for generated files. @chrisdone resolved it recently in https://github.com/commercialhaskell/stack/pull/4038 :fireworks:
@mgsloan Thanks for the update! In that case, I'll definitely give this method of starting ghci another try. Thanks again!
Most helpful comment
This behavior is not by design, it was an unintended consequence of using a random tmpdir for generated files. @chrisdone resolved it recently in https://github.com/commercialhaskell/stack/pull/4038 :fireworks: