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:
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:
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 '^