|
1 | 1 | <div class="hidden-warning"><a href="https://docs.haskellstack.org/"><img src="https://cdn.jsdelivr.net/gh/commercialhaskell/stack/doc/img/hidden-warning.svg"></a></div>
|
2 | 2 |
|
3 |
| -# GHCi |
4 |
| - |
5 |
| -`stack ghci` allows you to load components and files of your project into |
6 |
| -`ghci`. It uses the same TARGET syntax as `stack build`, and can also take |
7 |
| -options like `--test`, `--bench`, and `--flag`. Similarly to `stack build`, the |
8 |
| -default is to load up ghci with all libraries and executables in the project. |
9 |
| - |
10 |
| -In order to load multiple components, `stack ghci` combines all of the ghc options |
11 |
| -together. This doesn't work in the general case, but when the packages being |
12 |
| -loaded share similar conventions, it should work out. A common source of issues |
13 |
| -is when one component defines default extensions which aren't assumed by another |
14 |
| -component. For example, specifying `NoImplicitPrelude` in one component but |
15 |
| -not another is quite likely to cause failures. `ghci` will be run with |
| 3 | +# REPL environment |
| 4 | + |
| 5 | +A read–evaluate–print loop (REPL) environment takes single user inputs, executes |
| 6 | +them, and returns the result to the user. GHCi is GHC's interactive environment. |
| 7 | +The `stack ghci` or `stack repl` commands, which are equivalent, allow you to |
| 8 | +load components and files of your project into GHCi. |
| 9 | + |
| 10 | +The command uses the same TARGET syntax as `stack build`. It can also take flags |
| 11 | +like `--test` and `--bench` and options like `--flag`. Similarly to |
| 12 | +`stack build`, the default is to load up GHCi with all libraries and executables |
| 13 | +in the project. |
| 14 | + |
| 15 | +In order to load multiple components, `stack ghci` combines all of the GHC |
| 16 | +options together. This doesn't work in the general case, but when the packages |
| 17 | +being loaded share similar conventions, it should work out. A common source of |
| 18 | +issues is when one component defines default extensions which aren't assumed by |
| 19 | +another component. For example, specifying `NoImplicitPrelude` in one component |
| 20 | +but not another is quite likely to cause failures. GHCi will be run with |
16 | 21 | `-XNoImplicitPrelude`, but it is likely that modules in the other component
|
17 |
| -assume that the Prelude is implicitly imported. |
| 22 | +assume that the `Prelude` is implicitly imported. |
18 | 23 |
|
19 | 24 | ## Selecting Main module
|
20 | 25 |
|
21 | 26 | When loading multiple packages, there may be multiple definitions for the `Main`
|
22 |
| -module. You can specify which Main module to load by passing in the |
23 |
| -`--main-is TARGET` flag. If no selection is made and there are multiple `Main` |
24 |
| -modules, you will be asked to select from a list of options. |
| 27 | +module. You can specify which `Main` module to load with the |
| 28 | +`--main-is <target>` option. If no selection is made and there are multiple |
| 29 | +`Main` modules, you will be asked to select from a list of options. |
25 | 30 |
|
26 | 31 | ## Speeding up initial load
|
27 | 32 |
|
28 |
| -There are two ways to speed up the initial startup of ghci: |
| 33 | +There are two ways to speed up the initial startup of GHCi: |
29 | 34 |
|
30 |
| -* `--no-build`, to skip an initial build step. This only works if the |
31 |
| - dependencies have already been built. |
| 35 | +1. Pass the `--no-build` flag, to skip an initial build step. This works only |
| 36 | + if the dependencies have already been built. |
32 | 37 |
|
33 |
| -* `--no-load`, to skip loading all defined modules into ghci. You can then |
34 |
| - directly use `:load MyModule` to load a specific module in your project. |
| 38 | +2. Pass the `--no-load` flag, to skip loading all defined modules into GHCi. |
| 39 | + You can then directly use `:load MyModule` in GHCi to load a specific module |
| 40 | + in your project. |
35 | 41 |
|
36 | 42 | ## Loading just the main module
|
37 | 43 |
|
38 | 44 | By default, `stack ghci` loads and imports all of the modules in the package.
|
39 |
| -This allows you to easily use anything exported by your package. This is |
40 |
| -usually quite convenient, but in some cases it makes sense to only load one |
41 |
| -module, or no modules at all. The `--only-main` flag allows this. It specifies |
42 |
| -that only the main module will be loaded, if any. This is particularly useful |
43 |
| -in the following circumstances: |
44 |
| - |
45 |
| -1. You're loading the project in order to run it in ghci (e.g. via `main`), and |
46 |
| - you intend to reload while developing. Without the `--only-main` flag, you |
47 |
| - will need to quit and restart ghci whenever a module gets deleted. With the |
| 45 | +This allows you to easily use anything exported by your package. This is usually |
| 46 | +quite convenient, but in some cases it makes sense to only load one module, or |
| 47 | +no modules at all. The `--only-main` flag allows this. It specifies that only |
| 48 | +the main module will be loaded, if any. This is particularly useful in the |
| 49 | +following circumstances: |
| 50 | + |
| 51 | +1. You're loading the project in order to run it in GHCi (e.g. via `main`), and |
| 52 | + you intend to reload while developing. Without the `--only-main` flag, you |
| 53 | + will need to quit and restart GHCi whenever a module gets deleted. With the |
48 | 54 | flag, reloading should work fine in this case.
|
49 | 55 |
|
50 |
| -2. If many of your modules have exports named the same thing, then you'll need to |
51 |
| - refer to them using qualified names. To avoid this, it may be easier to use |
52 |
| - `--only-main` to start with a blank slate and just import the modules you are |
53 |
| - interested in. |
| 56 | +2. If many of your modules have exports named the same thing, then you'll need |
| 57 | + to refer to them using qualified names. To avoid this, it may be easier to |
| 58 | + use the `--only-main` flag to start with a blank slate and just import the |
| 59 | + modules you are interested in. |
54 | 60 |
|
55 | 61 | ## Loading a filepath directly
|
56 | 62 |
|
57 |
| -Instead of the `TARGET` syntax, it is also possible to directly run |
58 |
| -`stack ghci src/MyFile.hs`. This will figure out which component the file is |
59 |
| -associated with, and use the options from that component. |
| 63 | +Instead of the `TARGET` syntax, it is also possible to command directly, for |
| 64 | +example: |
60 | 65 |
|
61 |
| -## Specifying extra packages to build / depend on |
| 66 | +~~~text |
| 67 | +stack ghci src/MyFile.hs |
| 68 | +~~~ |
62 | 69 |
|
63 |
| -Sometimes you want to load ghci with an additional package, that isn't a direct |
64 |
| -dependency of your components. This can be achieved by using the `--package` flag. |
65 |
| -For example, if I want to experiment with the lens library, I can run |
66 |
| -`stack ghci --package lens`. |
| 70 | +This will figure out which component the file is associated with, and use the |
| 71 | +options from that component. |
67 | 72 |
|
68 |
| -## Running plain ghci |
| 73 | +## Specifying extra packages to build or depend on |
69 | 74 |
|
70 |
| -`stack ghci` always runs ghci configured to load code from packages in your |
71 |
| -project. In particular, this means it passes in flags like `-hide-all-packages` |
72 |
| -and `-package-id=` in order to configure which packages are visible to ghci. |
| 75 | +Sometimes you want to load GHCi with an additional package, that isn't a direct |
| 76 | +dependency of your components. This can be achieved by using the `--package` |
| 77 | +option. For example, if I want to experiment with the `lens` library, I can |
| 78 | +command: |
| 79 | + |
| 80 | +~~~text |
| 81 | +stack ghci --package lens |
| 82 | +~~~ |
| 83 | + |
| 84 | +## Running plain GHCi |
| 85 | + |
| 86 | +`stack ghci` always runs GHCi configured to load code from packages in your |
| 87 | +project. In particular, this means it passes in flags like `-hide-all-packages` |
| 88 | +and `-package-id=` in order to configure which packages are visible to GHCi. |
73 | 89 |
|
74 | 90 | For doing experiments which just involve packages installed in your databases,
|
75 |
| -it may be useful to run ghci plainly like `stack exec ghci`. This will run a |
76 |
| -plain `ghci` in an environment which includes `GHC_PACKAGE_PATH`, and so will |
77 |
| -have access to your databases. |
78 |
| - |
79 |
| -*Note*: Running `stack ghci` on a pristine copy of the code doesn't currently |
80 |
| -build libraries |
81 |
| -([#2790](https://github.com/commercialhaskell/stack/issues/2790)) or internal |
82 |
| -libraries ([#4148](https://github.com/commercialhaskell/stack/issues/4148)). |
83 |
| -It is recommended to always run a `stack build` before running `stack ghci`, |
84 |
| -until these two issues are closed. |
| 91 | +it may be useful to run GHCi plainly like: |
| 92 | + |
| 93 | +~~~text |
| 94 | +stack exec ghci |
| 95 | +~~~ |
| 96 | + |
| 97 | +This will run a plain GHCi in an environment which includes `GHC_PACKAGE_PATH`, |
| 98 | +and so will have access to your databases. |
| 99 | + |
| 100 | +!!! note |
| 101 | + |
| 102 | + Running `stack ghci` on a pristine copy of the code doesn't currently build |
| 103 | + libraries |
| 104 | + (issue [#2790](https://github.com/commercialhaskell/stack/issues/2790)) or |
| 105 | + internal libraries |
| 106 | + (issue [#4148](https://github.com/commercialhaskell/stack/issues/4148)). It |
| 107 | + is recommended to always use `stack build` before using `stack ghci`, until |
| 108 | + these two issues are closed. |
0 commit comments