Stack's primary command is
build. This page describes its interface. The goal
of the interface is to do the right thing for simple input, and allow
flexibility for more complicated goals.
See the introductory part of Stack's user's guide for an introduction to the command.
The synonym commands for
The advantage of the synonym commands is that they are convenient and short. The
advantage of the flags is that they compose. For example,
stack build --test --copy-bins will build libraries, executables, and test
suites, run the test suites, and then copy the executables to your local bin
path (more on this below).
Every Cabal package is made up of one or more components. It can have an optional library component, one or more optional executable components, one or more optional test suite components, and one or more optional benchmark components.
Stack allows you to identify a specific component to be built. For example,
stack build mypackage:test:mytests will build (and run - see further below)
mytests component of the
mytests must be a test
By default, if a test suite component is targeted, the component is built and
run. The running behaviour can be disabled with the
Similarly, if a benchmark component is targeted, it is built and run unless the
running behaviour is disabled with the
This ability to specify a component applies only to a local package. With
dependencies, Stack will always build the library (if present) and all
executables (if any), and ignore test suites and benchmarks. If you want more
control over a package, you must add it to your
packages setting in your
project-level configuration file (
stack build takes a list of one or more optional targets to be built. The
supported syntaxes for targets are:
stack build foobar, is the most commonly used target. It will try to find the package in the following locations: local packages, extra deps, snapshots, and package index (e.g. Hackage). If it's found in the package index, then the latest version of that package from the index is implicitly added to your extra dependencies.
If the package is a local package, the library and executable components are selected to be built. If the
--benchflags are set, then all of the test suite and benchmark components, respectively, are selected to be built.
package identifier, e.g.
stack build foobar-1.2.3, is usually used to include specific package versions from the package index. If the version selected conflicts with an existing local package or extra dep, then Stack fails with an error. Otherwise, this is the same as using
stack build foobar, except instead of using the latest version from the package index, the version specified is used.
component. Instead of referring to an entire package and letting Stack decide which components to build, you select individual components from inside a package. This can be done for more fine-grained control over which test suites to run, or to have a faster compilation cycle. There are multiple ways to refer to a specific component (provided for convenience):
packagename:comptype:compnameis the most explicit. The available comptypes are
execomponent is specified, all of the package's executable components will be built. This is due to limitations in all currently released versions of Cabal. See issue#1046
packagename:compnameallows you to leave out the component type, as that will (almost?) always be redundant with the component name. For example,
stack build mypackage:mytestsuite.
:compnameis a useful shortcut, saying "find the component in all of the local packages." This will result in an error if multiple packages have a component with the same name. To continue the above example,
stack build :mytestsuite.
stack build foo/bar, will find all local packages that exist in the given directory hierarchy and then follow the same procedure as passing in package names as mentioned above. There's an important caveat here: if your directory name is parsed as one of the above target types, it will be treated as that. Explicitly starting your target with
./can be a good way to avoid that, e.g.
stack build ./foo.
stack build .will target local packages in the current working directory or its subdirectories.
stack build with no targets specified will build all local packages.
stack ide targets to get a list of the available targets in your
Controlling what gets built¶
Stack will automatically build the necessary dependencies. See the introductory part of Stack's user's guide for information about how these dependencies get specified.
In addition to specifying targets, you can also control what gets built, or retained, with the following flags:
stack build --bench flag¶
Pass the flag to add benchmark components to the targets, if specific components are not identified.
stack build --dependencies-only flag¶
Pass the flag to skip building the targets. The flag
the same effect.
stack build --[no-]dry-run flag¶
Set the flag to build nothing and output information about the build plan.
stack build --flag option¶
stack build --flag <package_name>:[-]<flag_name> sets (or unsets) the
specified Cabal flag for the specified package.
This option can be specified multiple times to set (or unset) multiple Cabal flags.
The same Cabal flag name can be set (or unset) for multiple packages (at the command line only) with:
Currently you needs to list all of your modules that interpret flags in the
other-modules section of a Cabal file. Cabal (the tool) has a different
behavior currently and doesn't require that the modules be listed. This may
change in a future release.
stack build --[no-]force-dirty flag¶
Set the flag to force rebuild of packages even when it doesn't seem necessary based on file dirtiness.
stack build --[no-]haddock flag¶
Set the flag to build Haddock documentation. This may cause a lot of packages to get re-built, so that the documentation links work.
stack build --[no-]keep-going flag¶
stack build): Disabled
stack test or
stack bench): Enabled
Set the flag to continue building packages even after some build step fails. The packages which depend upon the failed build won't get built.
stack build --[no-]keep-tmp-files flag¶
Set the flag to keep intermediate files and build directories that would otherwise be considered temporary and deleted. It may be useful to inspect these, if a build fails. By default, they are not kept.
stack build --only-configure flag¶
Pass the flag to perform only the configure step, not any builds. This is intended for tool usage. It may break when used on multiple packages at once.
If there are downstream actions that require a package to be built then a full build will occur, even if the flag is passed.
stack build --only-dependencies flag¶
Pass the flag to skip building the targets. The flag
the same effect.
stack build --only-snapshot flag¶
Pass the flag to build only snapshot dependencies, which are cached and shared with other projects.
stack build --[no-]reconfigure flag¶
Set the flag to force reconfiguration even when it doesn't seem necessary based
on file dirtiness. This is sometimes useful with custom
Setup.hs files, in
particular when they depend on external data files.
stack build --skip option¶
stack build --skip <component> skips building the specified components of a
local package. It allows you to skip test suites and benchmark without
specifying other components (e.g.
stack test --skip long-test-suite will run
the tests without the
long-test-suite test suite). Be aware that skipping
executables won't work the first time the package is built due to an issue in
This option can be specified multiple times to skip multiple components.
stack build --test flag¶
Pass the flag to add test suite components to the targets, if specific components are not identified.
Other flags and options¶
There are a number of other flags accepted by
stack build. Instead of listing
all of them, please use
stack build --help. Some particularly convenient ones
worth mentioning here since they compose well with the rest of the build system
stack build --coverage flag¶
Pass the flag to generate a code coverage report. For further information, see the code coverage documentation.
stack build --exec option¶
stack build --exec "<command> [<arguments>]" will run a command after a
stack build --file-watch flag¶
Pass the flag to rebuild your project every time a file changes. By default it
will take into account all files belonging to the targets you specify. See also
stack build --[no-]interleaved-output flag¶
Set the flag for interleaved output. With interleaved output, each line of output from each package being built (targets and dependencies) is sent to the console as it happens and output relating to different packages can be interleaved. Each line will be prefixed with the name of the relevant package. The spacing between the prefix and the output will be set based on the longest relevant package name, so that the start of the output itself aligns. For example (extract):
hpack > build mustache > configure hpack > Preprocessing library for hpack-0.35.0.. hpack > Building library for hpack-0.35.0.. mustache > Configuring mustache-2.4.1... hpack > [ 1 of 29] Compiling Data.Aeson.Config.Key hpack > [ 2 of 29] Compiling Data.Aeson.Config.KeyMap mustache > build hpack > [ 3 of 29] Compiling Data.Aeson.Config.Util mustache > Preprocessing library for mustache-2.4.1.. mustache > Building library for mustache-2.4.1.. hpack > [ 4 of 29] Compiling Hpack.Haskell hpack > [ 5 of 29] Compiling Hpack.Utf8 mustache > [1 of 8] Compiling Paths_mustache hpack > [ 6 of 29] Compiling Imports hpack > [ 7 of 29] Compiling Hpack.Util mustache > [2 of 8] Compiling Text.Mustache.Internal
Unset the flag for non-interleaved output. With non-interleaved output, the
build output from GHC (as opposed to from Stack) in respect of dependencies is
ignored. The behaviour then depends whether there is one target package or more
than one. There can be one target if the project has a single package or if one
package is targetted in a multi-package project (for example, using
stack build <package_name>).
One target package: The build output for the target package is sent to the console as it happens.
More than one target package: The build output from GHC (as opposed to from Stack) for each target package is sent to a log file for that package, unless an error occurs. At the end of the build, the location of the directory containing the log files is reported. To also output the contents of the log files to the console at the end of the build, use Stack's
dump-logsoption. For further information about that option, see the YAML configuration documentation. The default
dump-logsmode is to output the contents of the log files that are warnings.
stack build --pedantic flag¶
Pass the flag to build your project with the GHC options
-Wall turns on all warning options that indicate potentially suspicious code.
-Werror makes any warning into a fatal error.
stack build --watch-all flag¶
Pass the flag to rebuild your project every time any local file changes (from
project packages or from local dependencies). See also the
To come back to the composable approach described above, consider this example
(which uses the
wai repository). The
will start Stack up in file watch mode, waiting for files in your project to change. When first starting, and each time a file changes, it will do all of the following.
- Build the wai-extra package and its test suites
- Build the
- Build the warp package's doctest component (which, as you may guess, is a test site)
- Run all of the wai-extra package's test suite components and the doctest test suite component
- If all of that succeeds:
- Copy generated executables to the local bin path
- Run the command
echo Yay, it worked!