|
| 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 | + |
| 3 | +# 5. `stack build` synonyms |
| 4 | + |
| 5 | +Let's look at a subset of the `stack --help` output: |
| 6 | + |
| 7 | +~~~text |
| 8 | +build Build the package(s) in this directory/configuration |
| 9 | +install Shortcut for 'build --copy-bins' |
| 10 | +test Shortcut for 'build --test' |
| 11 | +bench Shortcut for 'build --bench' |
| 12 | +haddock Shortcut for 'build --haddock' |
| 13 | +~~~ |
| 14 | + |
| 15 | +Four of these commands are just synonyms for the `build` command. They are |
| 16 | +provided for convenience for common cases (e.g., `stack test` instead of |
| 17 | +`stack build --test`) and so that commonly expected commands just work. |
| 18 | + |
| 19 | +What's so special about these commands being synonyms? It allows us to make |
| 20 | +much more composable command lines. For example, we can have a command that |
| 21 | +builds executables, generates Haddock documentation (Haskell API-level docs), |
| 22 | +and builds and runs your test suites, with: |
| 23 | + |
| 24 | +~~~text |
| 25 | +stack build --haddock --test |
| 26 | +~~~ |
| 27 | + |
| 28 | +You can even get more inventive as you learn about other flags. For example, |
| 29 | +take the following command: |
| 30 | + |
| 31 | +~~~text |
| 32 | +stack build --pedantic --haddock --test --exec "echo Yay, it succeeded" --file-watch |
| 33 | +~~~ |
| 34 | + |
| 35 | +This command will: |
| 36 | + |
| 37 | +* turn on all warnings and errors (the `--pedantic` flag) |
| 38 | +* build your library and executables |
| 39 | +* generate Haddocks (the `--haddock` flag) |
| 40 | +* build and run your test suite (the `--test` flag) |
| 41 | +* run the command `echo Yay, it succeeded` when that completes (the `--exec` |
| 42 | + option) |
| 43 | +* after building, watch for changes in the files used to build the project, and |
| 44 | + kick off a new build when done (the `--file-watch` flag) |
| 45 | + |
| 46 | +## The `stack install` command and `copy-bins` option |
| 47 | + |
| 48 | +It's worth calling out the behavior of the `install` command and `--copy-bins` |
| 49 | +option, since this has confused a number of users (especially when compared to |
| 50 | +behavior of other tools like Cabal (the tool)). The `install` command does |
| 51 | +precisely one thing in addition to the build command: it copies any generated |
| 52 | +executables to the local binary directory. You may recognize the default value |
| 53 | +for that path: |
| 54 | + |
| 55 | +On Unix-like operating systems, command: |
| 56 | + |
| 57 | +~~~text |
| 58 | +stack path --local-bin |
| 59 | +/home/<user_name>/.local/bin |
| 60 | +~~~ |
| 61 | + |
| 62 | +On Windows, command: |
| 63 | + |
| 64 | +~~~text |
| 65 | +stack path --local-bin |
| 66 | +C:\Users\<user_name>\AppData\Roaming\local\bin |
| 67 | +~~~ |
| 68 | + |
| 69 | +That's why the download page recommends adding that directory to your PATH. This |
| 70 | +feature is convenient, because now you can simply run `executable-name` in your |
| 71 | +shell instead of having to run `stack exec executable-name` from inside your |
| 72 | +project directory. |
| 73 | + |
| 74 | +Since it's such a point of confusion, let me list a number of things Stack does |
| 75 | +*not* do specially for the `install` command: |
| 76 | + |
| 77 | +* Stack will always build any necessary dependencies for your code. The install |
| 78 | + command is not necessary to trigger this behavior. If you just want to build a |
| 79 | + project, run `stack build`. |
| 80 | +* Stack will *not* track which files it's copied to your local binary directory |
| 81 | + nor provide a way to automatically delete them. There are many great tools out |
| 82 | + there for managing installation of binaries, and Stack does not attempt to |
| 83 | + replace those. |
| 84 | +* Stack will not necessarily be creating a relocatable executable. If your |
| 85 | + executables hard-codes paths, copying the executable will not change those |
| 86 | + hard-coded paths. |
| 87 | + |
| 88 | + * At the time of writing, there's no way to change those kinds of paths with |
| 89 | + Stack, but see |
| 90 | + [issue #848 about --prefix](https://github.com/commercialhaskell/stack/issues/848) |
| 91 | + for future plans. |
| 92 | + |
| 93 | +That's really all there is to the `install` command: for the simplicity of what |
| 94 | +it does, it occupies a much larger mental space than is warranted. |
0 commit comments