Skip to content

Commit b0b0d91

Browse files
committed
docs(ref): describe how the current resolver sometimes duplicates deps
This documents the behavior described in #9029, where sometimes Cargo will duplicate a dep when it doesn't have to.
1 parent 985d561 commit b0b0d91

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

src/doc/src/reference/resolver.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ result of the resolution is stored in the `Cargo.lock` file which "locks" the
77
dependencies to specific versions, and keeps them fixed over time.
88

99
The resolver attempts to unify common dependencies while considering possibly
10-
conflicting requirements. The sections below provide some details on how these
11-
constraints are handled, and how to work with the resolver.
10+
conflicting requirements. Dependency resolution is, however, an NP-hard problem,
11+
and so the resolver uses a heuristic algorithm that may yield counterintuitive
12+
results in some cases. The sections below provide some details on how
13+
requirements are handled, and how to work with the resolver.
1214

1315
See the chapter [Specifying Dependencies] for more details about how
1416
dependency requirements are specified.
@@ -91,6 +93,31 @@ at the time of this writing) and Package B will use the greatest `0.6` release
9193
(`0.6.5` for example). This can lead to potential problems, see the
9294
[Version-incompatibility hazards] section for more details.
9395

96+
The resolver algorithm favors the greatest available versions of dependencies
97+
and has limited support for backtracking in the solutions it attempts.
98+
Therefore, it may converge on a solution that includes two copies of a
99+
dependency when one would suffice. For example:
100+
101+
```toml
102+
# Package A
103+
[dependencies]
104+
rand = "0.7"
105+
106+
# Package B
107+
[dependencies]
108+
rand = ">=0.6"
109+
```
110+
111+
In this example, Cargo may build two copies of the rand crate: `0.8.5` (the
112+
greatest available at the time of this writing) for Package B and `0.7.3` for
113+
Package A — even though a single copy at version `0.7.3` meets all requirements.
114+
Future revisions of the resolver algorithm may change this behavior. In the
115+
meantime, the [`cargo update`] command with the `--precise` flag can be used to
116+
manually remove such duplications.
117+
118+
[`cargo update`]: ../commands/cargo-update.md
119+
120+
94121
Multiple versions within the same compatibility range are not allowed and will
95122
result in a resolver error if it is constrained to two different versions
96123
within a compatibility range. For example, if there are two packages in the

0 commit comments

Comments
 (0)