Skip to content

Commit 720bf7a

Browse files
committed
Clarify and extend build command documents
1 parent e9b126c commit 720bf7a

File tree

1 file changed

+106
-83
lines changed

1 file changed

+106
-83
lines changed

doc/build_command.md

Lines changed: 106 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -4,117 +4,118 @@
44

55
## Overview
66

7-
The primary command you use in Stack is `build`. This page describes the `build`
8-
command's interface. The goal of the interface is to do the right thing for
9-
simple input, and allow a lot of flexibility for more complicated goals.
7+
Stack's primary command is `build`. This page describes its interface. The goal
8+
of the interface is to do the right thing for simple input, and allow
9+
flexibility for more complicated goals.
1010

1111
See the introductory part of Stack's
1212
[user's guide](GUIDE.md#the-stack-build-command) for an introduction to the
1313
command.
1414

1515
## Synonyms
1616

17-
One potential point of confusion is the synonym commands for `build`. These are
18-
provided to match commonly expected command line interfaces, and to make common
19-
workflows shorter. The important thing to note is that all of these are just
20-
the `build` command in disguise. Each of these commands are called out as
21-
synonyms in the `--help` output. These commands are:
17+
The synonym commands for `build` are:
2218

23-
* `stack test` is the same as `stack build --test`
24-
* `stack bench` is the same as `stack build --bench`
25-
* `stack haddock` is the same as `stack build --haddock`
26-
* `stack install` is the same as `stack build --copy-bins`
19+
|Synonym command|Equivalent `build` command flag|
20+
|---------------|-------------------------------|
21+
|`stack test` |`stack build --test` |
22+
|`stack bench` |`stack build --bench` |
23+
|`stack haddock`|`stack build --haddock` |
24+
|`stack install`|`stack build --copy-bins` |
2725

28-
The advantage of the synonym commands is that they're convenient and short. The
29-
advantage of the options is that they compose. For example,
26+
The advantage of the synonym commands is that they are convenient and short. The
27+
advantage of the flags is that they compose. For example,
3028
`stack build --test --copy-bins` will build libraries, executables, and test
3129
suites, run the test suites, and then copy the executables to your local bin
3230
path (more on this below).
3331

3432
## Components
3533

36-
Components are a subtle yet important point to how build operates under the
37-
surface. Every cabal package is made up of one or more components. It can have
38-
0 or 1 libraries, and then 0 or more of executable, test, and benchmark
39-
components. Stack allows you to call out a specific component to be built, e.g.
40-
`stack build mypackage:test:mytests` will build the `mytests` component of the
41-
`mypackage` package. `mytests` must be a test suite component.
42-
43-
We'll get into the details of the target syntax for how to select components in
44-
the next section. In this section, the important point is: whenever you target
45-
a test suite or a benchmark, it's built __and also run__, unless you explicitly
46-
disable running via `--no-run-tests` or `--no-run-benchmarks`. Case in point:
47-
the previous command will in fact build the `mytests` test suite *and* run it,
48-
even though you haven't used the `stack test` command or the `--test` option.
49-
(We'll get to what exactly `--test` does below.)
50-
51-
This gives you a lot of flexibility in choosing what you want Stack to do. You
52-
can run a single test component from a package, run a test component from one
53-
package and a benchmark from another package, etc.
54-
55-
One final note on components: you can only control components for local
56-
packages, not dependencies. With dependencies, Stack will *always* build the
57-
library (if present) and all executables, and ignore test suites and
58-
benchmarks. If you want more control over a package, you must add it to your
59-
`packages` setting in your project-level configuration file (`stack.yaml`).
34+
Every Cabal package is made up of one or more components. It can have an
35+
optional library component, one or more optional executable components, one or
36+
more optional test suite components, and one or more optional benchmark
37+
components.
38+
39+
Stack allows you to identify a specific component to be built. For example,
40+
`stack build mypackage:test:mytests` will build (and run - see further below)
41+
the `mytests` component of the `mypackage` package. `mytests` must be a test
42+
suite component.
43+
44+
By default, if a test suite component is targeted, the component is built and
45+
run. The running behaviour can be disabled with the `--no-run-tests` flag.
46+
Similarly, if a benchmark component is targeted, it is built and run unless the
47+
running behaviour is disabled with the `--no-run-benchmarks` flag.
48+
49+
This ability to specify a component applies only to a local package. With
50+
dependencies, Stack will *always* build the library (if present) and all
51+
executables (if any), and ignore test suites and benchmarks. If you want more
52+
control over a package, you must add it to your `packages` setting in your
53+
project-level configuration file (`stack.yaml`).
6054

6155
## Target syntax
6256

63-
In addition to a number of options (like the aforementioned `--test`),
64-
`stack build` takes a list of zero or more *targets* to be built. There are a
65-
number of different syntaxes supported for this list:
57+
`stack build` takes a list of one or more optional *targets* to be built. The
58+
supported syntaxes for targets are:
6659

6760
* *package*, e.g. `stack build foobar`, is the most commonly used target. It
6861
will try to find the package in the following locations: local packages,
69-
extra dependencies, snapshots, and package index (e.g. Hackage). If it's
70-
found in the package index, then the latest version of that package from
71-
the index is implicitly added to your extra dependencies.
62+
extra deps, snapshots, and package index (e.g. Hackage). If it's found in
63+
the package index, then the latest version of that package from the index is
64+
implicitly added to your extra dependencies.
7265

73-
This is where the `--test` and `--bench` flags come into play. If the
74-
package is a local package, then all of the test suite and benchmark
75-
components are selected to be built, respectively. In any event, the
76-
library and executable components are also selected to be built.
66+
If the package is a local package, the library and executable components are
67+
selected to be built. If the `--test` and `--bench` flags are set, then all
68+
of the test suite and benchmark components, respectively, are selected to be
69+
built.
7770

7871
* *package identifier*, e.g. `stack build foobar-1.2.3`, is usually used to
79-
include specific package versions from the index. If the version selected
80-
conflicts with an existing local package or extra dep, then stack fails
81-
with an error. Otherwise, this is the same as calling `stack build foobar`,
82-
except instead of using the latest version from the index, the version
83-
specified is used.
72+
include specific package versions from the package index. If the version
73+
selected conflicts with an existing local package or extra dep, then Stack
74+
fails with an error. Otherwise, this is the same as using
75+
`stack build foobar`, except instead of using the latest version from the
76+
package index, the version specified is used.
8477

8578
* *component*. Instead of referring to an entire package and letting Stack
8679
decide which components to build, you select individual components from
8780
inside a package. This can be done for more fine-grained control over which
8881
test suites to run, or to have a faster compilation cycle. There are
8982
multiple ways to refer to a specific component (provided for convenience):
9083

91-
* `packagename:comptype:compname` is the most explicit. The available
92-
comptypes are `exe`, `test`, and `bench`.
93-
* Side note: When any `exe` component is specified, all of the package's
94-
executable components will be built. This is due to limitations in all
95-
currently released versions of Cabal. See
96-
[issue#1046](https://github.com/commercialhaskell/stack/issues/1406)
97-
* `packagename:compname` allows you to leave off the component type, as
98-
that will (almost?) always be redundant with the component name. For
99-
example, `stack build mypackage:mytestsuite`.
100-
* `:compname` is a useful shortcut, saying "find the component in all of
101-
the local packages." This will result in an error if multiple packages
102-
have a component with the same name. To continue the above example,
103-
`stack build :mytestsuite`.
104-
105-
* *directory*, e.g. `stack build foo/bar`, will find all local packages that
106-
exist in the given directory hierarchy and then follow the same procedure as
107-
passing in package names as mentioned above. There's an important caveat
108-
here: if your directory name is parsed as one of the above target types, it
109-
will be treated as that. Explicitly starting your target with `./` can be a
110-
good way to avoid that, e.g. `stack build ./foo`
111-
112-
Finally: if you provide no targets (e.g., running `stack build`), Stack will
113-
implicitly pass in all of your local packages. If you only want to target
114-
packages in the current directory or deeper, you can pass in `.`, e.g.
115-
`stack build .`.
116-
117-
To get a list of the available targets in your project, use `stack ide targets`.
84+
* `packagename:comptype:compname` is the most explicit. The available
85+
comptypes are `exe`, `test`, and `bench`.
86+
87+
!!! note
88+
89+
When any `exe` component is specified, all of the package's
90+
executable components will be built. This is due to limitations in
91+
all currently released versions of Cabal. See
92+
[issue#1046](https://github.com/commercialhaskell/stack/issues/1406)
93+
94+
* `packagename:compname` allows you to leave out the component type, as
95+
that will (almost?) always be redundant with the component name. For
96+
example, `stack build mypackage:mytestsuite`.
97+
98+
* `:compname` is a useful shortcut, saying "find the component in all of
99+
the local packages." This will result in an error if multiple packages
100+
have a component with the same name. To continue the above example,
101+
`stack build :mytestsuite`.
102+
103+
* *directory*, e.g. `stack build foo/bar`, will find all local packages that
104+
exist in the given directory hierarchy and then follow the same procedure as
105+
passing in package names as mentioned above. There's an important caveat
106+
here: if your directory name is parsed as one of the above target types, it
107+
will be treated as that. Explicitly starting your target with `./` can be a
108+
good way to avoid that, e.g. `stack build ./foo`.
109+
110+
!!! note
111+
112+
`stack build .` will target local packages in the current working
113+
directory or its subdirectories.
114+
115+
`stack build` with no targets specified will build all local packages.
116+
117+
Command `stack ide targets` to get a list of the available targets in your
118+
project.
118119

119120
## Controlling what gets built
120121

@@ -125,6 +126,11 @@ about how these dependencies get specified.
125126
In addition to specifying targets, you can also control what gets built, or
126127
retained, with the following flags:
127128

129+
### The `stack build --bench` flag
130+
131+
Pass the flag to add benchmark components to the targets, if specific components
132+
are not identified.
133+
128134
### The `stack build --dependencies-only` flag
129135

130136
Pass the flag to skip building the targets. The flag `--only-dependencies` has
@@ -189,11 +195,28 @@ Set the flag to keep intermediate files and build directories that would
189195
otherwise be considered temporary and deleted. It may be useful to inspect
190196
these, if a build fails. By default, they are not kept.
191197

198+
### The `stack build --only-configure` flag
199+
200+
[:octicons-tag-24: 0.1.4.0](https://github.com/commercialhaskell/stack/releases/tag/v0.1.4.0)
201+
202+
Pass the flag to perform only the configure step, not any builds. This is
203+
intended for tool usage. It may break when used on multiple packages at once.
204+
205+
!!! note
206+
207+
If there are downstream actions that require a package to be built then a
208+
full build will occur, even if the flag is passed.
209+
192210
### The `stack build --only-dependencies` flag
193211

194212
Pass the flag to skip building the targets. The flag `--dependencies-only` has
195213
the same effect.
196214

215+
### The `stack build --only-snapshot` flag
216+
217+
Pass the flag to build only snapshot dependencies, which are cached and shared
218+
with other projects.
219+
197220
### The `stack build --[no-]reconfigure` flag
198221

199222
Default: Disabled
@@ -213,10 +236,10 @@ executables won't work the first time the package is built due to an issue in
213236

214237
This option can be specified multiple times to skip multiple components.
215238

216-
### The `stack build --only-snapshot` flag
239+
### The `stack build --test` flag
217240

218-
Pass the flag to build only snapshot dependencies, which are cached and shared
219-
with other projects.
241+
Pass the flag to add test suite components to the targets, if specific
242+
components are not identified.
220243

221244
## Other flags and options
222245

0 commit comments

Comments
 (0)