TExcelTAD
Home › Cargo in Depth › Duplicate Versions & cargo tree -d

Duplicate Versions & cargo tree -d

npm can install many copies of a package; Python can install exactly one. Cargo sits in between, and the rule is precise: one version per SemVer-compatible range, several across incompatible ones. If two crates want rand = "0.8.3" and rand = "0.8.5", both get 0.8.5. If one wants "0.8" and the other "0.10", both versions are compiled and linked into the same binary.

Why Cargo allows it

Rust crates of different major versions are, to the compiler, unrelated crates that happen to share a name. Their symbols are mangled with a hash that includes the version, so they can't clash at link time. That lets the ecosystem move forward one crate at a time: you can upgrade to rand 0.10 before every library you use has.

The price is paid in three places:

Cost Why
build time two versions of rand, rand_core, getrandom… are all compiled
binary size both versions end up in the binary
type mismatches a rand 0.8 trait and a rand 0.10 trait are different traits; values don't cross between them

Finding duplicates

Here an application, svc, uses thiserror 2 for its error types, but it also depends on an older internal library, legacy, that still uses thiserror 1. cargo tree -d (--duplicates) lists every crate that appears in more than one version, with an inverted tree showing who pulls each one in:

bash · try it yourself
mkdir app && cd app
cargo new --lib legacy --vcs none -q
cargo new svc --vcs none -q
echo 'thiserror = "1"' >> legacy/Cargo.toml
printf 'legacy = { path = "../legacy" }\nthiserror = "2"\n' >> svc/Cargo.toml
cd svc
cargo generate-lockfile 2>&1
cargo tree -d 2>&1 | grep -v Unpacking
     Locking 10 packages to highest Rust 1.99.1 compatible versions
syn v2.0.119
└── thiserror-impl v1.0.69 (proc-macro)
    └── thiserror v1.0.69
        └── legacy v0.1.0 (/home/learner/app/legacy)
            └── svc v0.1.0 (/home/learner/app/svc)

syn v3.0.9
└── thiserror-impl v2.0.21 (proc-macro)
    └── thiserror v2.0.21
        └── svc v0.1.0 (/home/learner/app/svc)

thiserror v1.0.69 (*)

thiserror v2.0.21 (*)

thiserror-impl v1.0.69 (proc-macro) (*)

thiserror-impl v2.0.21 (proc-macro) (*)

Read it bottom-up. thiserror v1.0.69 is used by legacy, which svc depends on; thiserror v2.0.21 by svc itself. The duplication doesn't stop there: each thiserror has its own proc-macro crate thiserror-impl, and those depend on different major versions of syn (syn 2 for thiserror 1, syn 3 for thiserror 2). syn is one of the slowest crates in the ecosystem to compile, and now it's built twice. One outdated dependency doubled three crates; with a crate family like rand (rand, rand_core, getrandom, a ChaCha implementation) the count grows quickly.

Two more views that help:

bash · try it yourself
mkdir app && cd app
cargo new --lib legacy --vcs none -q
cargo new svc --vcs none -q
echo 'thiserror = "1"' >> legacy/Cargo.toml
printf 'legacy = { path = "../legacy" }\nthiserror = "2"\n' >> svc/Cargo.toml
cd svc
cargo generate-lockfile -q
echo "--- only the duplicated names, no trees"
cargo tree -d --depth 0 2>&1 | grep -v -e Unpacking -e '^
  


echo "--- who needs syn 2?"
cargo tree -i syn@2.0.119
--- only the duplicated names, no trees
syn v2.0.119
syn v3.0.9
thiserror v1.0.69
thiserror v2.0.21
thiserror-impl v1.0.69 (proc-macro)
thiserror-impl v2.0.21 (proc-macro)
--- who needs syn 2?
syn v2.0.119
└── thiserror-impl v1.0.69 (proc-macro)
    └── thiserror v1.0.69
        └── legacy v0.1.0 (/home/learner/app/legacy)
            └── svc v0.1.0 (/home/learner/app/svc)

--depth 0 turns the report into a list, handy in CI logs. -i (--invert) takes a package spec, and when there are several versions you must say which one (syn@2.0.119, or syn@2 when that's unambiguous).

When duplicates break the build

Duplicates compile fine until a type crosses from one version to the other. Type mismatches are where duplicates of a crate like rand hurt. Here legacy exposes a function that takes "anything implementing rand's Rng", meaning rand 0.8's Rng, and dice passes a generator from rand 0.10:

bash · try it yourself
mkdir app && cd app
cargo new --lib legacy --vcs none -q
cargo new dice --vcs none -q
echo 'rand = "0.8"' >> legacy/Cargo.toml
cat > legacy/src/lib.rs <<'RS'
use rand::Rng;
pub fn roll(rng: &mut impl Rng) -> u8 { rng.gen_range(1..=6) }
RS
printf 'legacy = { path = "../legacy" }\nrand = "0.10"\n' >> dice/Cargo.toml
cat > dice/src/main.rs <<'RS'
fn main() {
    let mut rng = rand::rng();
    println!("rolled {}", legacy::roll(&mut rng));
}
RS
cd dice
cargo build -q 2>&1 | grep -E '^(error|note)|multiple different versions'
echo "--- both rand families are in the graph"
cargo tree -d --depth 0 2>&1 | grep -E '^rand'
error[E0277]: the trait bound `ThreadRng: rand::rng::Rng` is not satisfied
note: there are multiple different versions of crate `rand_core` in the dependency graph
    = note: there are multiple different versions of crate `rand` in the dependency graph
note: required by a bound in `roll`
error: could not compile `dice` (bin "dice") due to 1 previous error
--- both rand families are in the graph
rand v0.8.5
rand v0.10.3
rand_core v0.6.4
rand_core v0.10.1

The compiler even tells you the cause: there are multiple different versions of crate rand in the dependency graph. When you see "expected trait X, found trait X" with the same name on both sides, think duplicates first.

Fixing it

In order of preference:

  1. Upgrade the laggard. Move legacy to rand 0.10. Then cargo tree -d is empty for rand and the error disappears.
  2. Re-export the dependency from the library that exposes it in its API (pub use rand; in legacy), and have callers use legacy::rand::.... Then callers always get the matching version. Any crate whose public API mentions another crate's types should do this.
  3. Accept the duplicate if no types cross the boundary (an internal use of thiserror 1 is harmless). Document it, and put it on the upgrade list.

Tip: cargo-deny has a bans check (multiple-versions = "warn" or "deny") that turns cargo tree -d into a CI gate, with a skip list for accepted duplicates.

Test yourself

Two crates in your graph require serde = "1.0.180" and serde = "1.0.229". How many serde versions are built?

  1. Two
  2. One: 1.0.229 satisfies both caret requirements
  3. One: 1.0.180, the oldest
  4. It's a resolution error

Which command shows every crate that appears in more than one version?

  1. cargo tree --all
  2. cargo tree -d
  3. cargo metadata --dups
  4. cargo update -d

Error: the trait rand::Rng is not implemented for ThreadRng, with a note about multiple versions of rand. What's the root cause?

  1. ThreadRng is private
  2. A value from one rand version is passed where another rand version's trait is expected
  3. rand needs the std feature
  4. The lockfile is corrupt