Why 'build' a package instead of using 'extra-deps' in 'stack.yml'?

586 views Asked by At

It's my understanding (1) that the build-depends section in my project's .cabal file will ensure packages specified there are present in the resolver specified in stack.yml are available (in the appropriate versions) and used when I test or run my package (e.g., with stack ghci or stack test, etc.).

I also gather (2) that the extra-deps section of my stack.yml is used to acquire packages that are not in the specified resolver.

But I'm confused about the role of

stack build some-package

How does this differ from extra-deps? Will some-package be used when I stack test or stack ghci etc.? Why use it instead of just adding some-package to extra-deps? (And, have I got (1) and (2) right?)

1

There are 1 answers

8
sjakobi On BEST ANSWER

The main purpose of the build command is to actually trigger the build, i.e. compilation. By default stack build will build all the "local" packages listed in the stack.yaml's packages section. If you pass a package name as an argument to build, that has usually one of the following two purposes:

  1. You want to build only a certain (local) package in a multi-package project
  2. Or you you want to install an executable from a non-local package, for example hlint. Typically you will do this with stack install PKG which is a shortcut for stack build --copy-bins PKG.

One core design principle of stack is that builds should be reproducible, i.e. a project with the same code and same configuration should always give the same result for stack build.

That means that stack build or stack install will never change the project configuration or add dependencies to the project.

For more details on the build command, take a look at the relevant docs.