Skip to content

Commit 07137a1

Browse files
committed
Improve docs around only and optional, closes #13782
1 parent b8cec30 commit 07137a1

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

lib/elixir/pages/references/library-guidelines.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ Projects are often made available to other developers [by publishing a Hex packa
3535

3636
## Dependency handling
3737

38-
When your library is published and used as a dependency, its [lockfile](https://hexdocs.pm/mix/Mix.Project.html#module-configuration) (usually named `mix.lock`) is _ignored by the host project_. Running `mix deps.get` in the host project attempts to get the latest possible versions of your library’s dependencies, as specified by the requirements in the `deps` section of your `mix.exs`. These versions might be greater than those stored in your `mix.lock` (and hence used in your tests / CI).
38+
When your library is used as a dependency, it runs by default in the `:prod` environment. Therefore, if your library has dependencies that are only useful in development or testing, you want to specify those dependencies with the `:only` option. You can also specify `:optional` dependencies in your library, which are not enforced upon users of your library. In such cases, you should also consider compiling your projects with the `mix compile --no-optional-deps --warnings-as-errors` in your test environments, to ensure your library compiles without warnings even if optional dependencies are missing. See `mix deps` for all available options.
3939

40-
On the other hand, contributors of your library, need a deterministic build, which implies the presence of `mix.lock` in your Version Control System (VCS).
40+
Keep in mind your library's [lockfile](`Mix.Project#module-configuration`) (usually named `mix.lock`) is _ignored by the host project_. Running `mix deps.get` in the host project attempts to get the latest possible versions of your library’s dependencies, as specified by the requirements in the `deps` section of your `mix.exs`. These versions might be greater than those stored in your `mix.lock` (and hence used in your tests / CI).
4141

42-
The best practice of handling `mix.lock` file therefore would be to keep it in VCS, and run two different Continuous Integration (CI) workflows: the usual deterministic one, and another one, that starts with `mix deps.unlock --all` and always compiles your library and runs tests against latest versions of dependencies. The latter one might be even run nightly or otherwise recurrently to stay notified about any possible issue in regard to dependencies updates.
42+
On the other hand, contributors of your library, need a deterministic build, which implies the presence of `mix.lock` in your Version Control System (VCS), such as `git`.
43+
44+
If you want to validate both scenarios, you should check the `mix.lock` into version control and run two different Continuous Integration (CI) workflows: one that relies on the `mix.lock` for deterministic builds, and another one, that starts with `mix deps.unlock --all` and always compiles your library and runs tests against latest versions of dependencies. The latter one might be even run nightly or otherwise recurrently to stay notified about any possible issue in regard to dependencies updates.

lib/mix/lib/mix/tasks/deps.ex

+19-5
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ defmodule Mix.Tasks.Deps do
6262
* `:app` - when set to `false`, does not read the app file for this
6363
dependency. By default, the app file is read
6464
65-
* `:env` - the environment (as an atom) to run the dependency on; defaults to `:prod`.
66-
This is not necessary for dependencies on other apps within the same umbrella project,
67-
see the `:in_umbrella` option below.
65+
* `:env` - the environment (as an atom) to run the dependency on.
66+
While your current project runs in `:dev` by default, dependencies
67+
defaults to `:prod` (except for `:in_umbrella` dependencies, see
68+
below)
6869
6970
* `:compile` - a command (string) to compile the dependency; defaults to a `mix`,
7071
`rebar` or `make` command
@@ -75,13 +76,18 @@ defmodule Mix.Tasks.Deps do
7576
use the optional dependency. However, if the other project includes
7677
the optional dependency on its own, the requirements and options
7778
specified here will also be applied. Optional dependencies will _not_
78-
be started by the application.
79+
be started by the application. You should consider compiling your
80+
projects with the `mix compile --no-optional-deps --warnings-as-errors`
81+
during test, to ensure your project compiles without warnings even
82+
if optional dependencies are missing
7983
8084
* `:only` - the dependency is made available only in the given environments,
8185
useful when declaring dev- or test-only dependencies; by default the
8286
dependency will be available in all environments. The value of this option
8387
can either be a single environment (like `:dev`) or a list of environments
84-
(like `[:dev, :test]`)
88+
(like `[:dev, :test]`). Keep in mind that your project runs in the `:dev`
89+
environment by default, however, all of your dependencies run in the `:prod`
90+
environment (unless the `:env` option above is given)
8591
8692
* `:targets` - the dependency is made available only for the given targets.
8793
By default the dependency will be available in all targets. The value
@@ -109,6 +115,14 @@ defmodule Mix.Tasks.Deps do
109115
* `:system_env` - an enumerable of key-value tuples of binaries to be set
110116
as environment variables when loading or compiling the dependency
111117
118+
When a project is used as a dependency, it runs by default in the `:prod`
119+
environment. Therefore, if your project has dependencies that are only
120+
useful in development or testing, you want to specify those dependencies with
121+
the `:only` option above. You can also specify `:optional` dependencies
122+
in your project, which are not enforced upon users of your library, as outlined
123+
above. Finally, the [lockfile](`Mix.Project#module-configuration`) (usually
124+
named `mix.lock`) is ignored when a project is used as a dependency.
125+
112126
### Git options (`:git`)
113127
114128
* `:git` - the Git repository URI

0 commit comments

Comments
 (0)