Skip to content

Commit 6c37c34

Browse files
committed
docs(ref): Provide guidance on version requirements
I've been dealing with these situations as either the package author, depending on such a package, or supporting users who run into problems that I figure documenting this guidance in a central place means I won't have to repeat myself as often and have to re-find all of the relevant links each time. Alternatives to how this was documented - Use a regular header. All of sections in this document are flat and its hard to see association between them. This also feels like its more on the level of the "note"s. - Put this in a central Recommendations page. I think we should do something more for these when we have more (nothing else in my quick scan stood out as "recommendations" like this). At that point we can have a better idea of how it would work (much like the rule of 3 for generalizing code). I also suspect a "Recommendations" index might be better. - Put this in the FAQ. This can easily be framed as a question and we put the `Cargo.lock` policy in there. I left out talking about alternative proc-macro designs as I feel like treading new ground in community practices is out of the scope of this. See also https://rust-lang.zulipchat.com/#narrow/stream/246057-t-cargo/topic/Version.20Requirements.20documentation
1 parent be42872 commit 6c37c34

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/doc/src/reference/specifying-dependencies.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,43 @@ Here are some examples of comparison requirements:
107107
As shown in the examples above, multiple version requirements can be
108108
separated with a comma, e.g., `>= 1.2, < 1.5`.
109109

110+
> **Recommendation:** When in doubt, use the default version requirement operator.
111+
>
112+
> In rare circumstances, a package with a "public dependency"
113+
> (re-exports the dependency or interoperates with it in its public API)
114+
> that is compatible with multiple semver-incompatible versions
115+
> (e.g. only uses a simple type that hasn't changed between releases, like an `Id`)
116+
> may support users choosing which version of the "public dependency" to use.
117+
> In this case, a version requirement like `">=0.4, <2"` may be of interest.
118+
> *However* users of the package will likely run into errors and need to to
119+
> manually select a version of the "public dependency" via `cargo update` if
120+
> they also depend on it as Cargo might pick different versions of the "public
121+
> dependency" when [resolving dependency versions](resolver.md) (see
122+
> [#10599]).
123+
>
124+
> Avoid constraining the upper bound of a version to be anything less than the
125+
> next semver incompatible version
126+
> (e.g. avoid `">=2.0, <2.4"`) as other packages in the dependency tree may
127+
> require a newer version, leading to an unresolvable error (see #6584).
128+
> Consider whether controlling the version in your [`Cargo.lock`] would be more
129+
> appropriate.
130+
>
131+
> In some instances this won't matter or the benefits might outweigh the cost, including:
132+
> - When no one else depends on your package e.g. it only has a `[[bin]]`
133+
> - When depending on a pre-release package and wishing to avoid breaking
134+
> changes then a fully specified `"=1.2.3-alpha.3"` might be warranted (see
135+
> [#2222])
136+
> - When a library re-exports a proc-macro but the proc-macro generates code that
137+
> calls into the re-exporting library then a fully specified `=1.2.3` might be
138+
> warranted to ensure the proc-macro isn't newer than the re-exporting library
139+
> and generating code that uses parts of the API that don't exist within the
140+
> current version
141+
142+
[`Cargo.lock`]: ../guide/cargo-toml-vs-cargo-lock.md
143+
[#2222]: https://github.com/rust-lang/cargo/issues/2222
144+
[#6584]: https://github.com/rust-lang/cargo/issues/6584
145+
[#10599]: https://github.com/rust-lang/cargo/issues/10599
146+
110147
### Specifying dependencies from other registries
111148

112149
To specify a dependency from a registry other than [crates.io], first the

0 commit comments

Comments
 (0)