From 30b0c5053db3a1ba3d02c9a2b61ec92c00d168c5 Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Wed, 18 Dec 2024 17:29:11 -0500 Subject: [PATCH] Advise library authors on how best to depend on child dependencies --- lib/elixir/pages/references/library-guidelines.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/elixir/pages/references/library-guidelines.md b/lib/elixir/pages/references/library-guidelines.md index c669d3793e3..4427a772b01 100644 --- a/lib/elixir/pages/references/library-guidelines.md +++ b/lib/elixir/pages/references/library-guidelines.md @@ -42,3 +42,9 @@ Keep in mind your library's [lockfile](`Mix.Project#module-configuration`) (usua 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`. 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. + +### Dependency Version Requirements + +When depending on other libraries, the dependency version requirements are ultimately up to you. However, you should consider the effects that an overly strict dependency requirement can have on users of your library. Most dependencies adopt semver, and therefore provide reasonable guarantees about what each release contains. For instance, if you use `{:some_dep, "== 0.2.3"}`, this prevents users from using other version but the one that you specified, which means that they cannot receive bug fix upgrades to that package. When in doubt, use a dependency in the format of `"~> x.y"`. This prevents the user from using a higher major version of the library, but allows them to upgrade to newer minor and patch versions, which should only include bug fixes and non-breaking improvements. + +A common mistake is to use a dependency in the format of `"~> x.y.z"`. For example, if you are depending on version `1.2`, and the dependency publishes a fix in version `1.2.1` that you need for the next version of your library. If you use `"~> 1.2.1"` to express that dependency, you are preventing users from upgrading to `"1.3.0"` or higher! Instead of `"~> 1.2.1"`, you should use `"~> 1.2 and >= 1.2.1"` as the version requirement. This allows users to use any version less thatn `2.0`, and greater than `1.2.1`.