Sub topic 10
Inspecting the dependency tree with cargo tree
This chapter assumes you can add dependencies with cargo add, read version requirements in Cargo.toml, and know that Cargo.lock pins exact versions. Here you will use cargo tree to see what Cargo actually resolved.
Why look at the tree
Your Cargo.toml lists only direct dependencies. Each of those crates has its own dependencies, and so on. The full set is the dependency graph, and Cargo.lock records every node in it. cargo tree prints that graph as a tree so you can answer questions like these:
- Why is this crate in my build?
- Which versions of a crate are present?
- Which features are switched on?
cargo tree is built into Cargo and needs no installation. It reads Cargo.lock when one exists, so the output matches what you build.
Reading the default output
Run it in the json_demo project from earlier chapters:
cargo treeThe output looks like this. Exact versions on your machine will differ.
json_demo v0.1.0 (/home/you/json_demo)
├── serde v1.0.217
│ └── serde_derive v1.0.217 (proc-macro)
│ ├── proc-macro2 v1.0.92
│ │ └── unicode-ident v1.0.14
│ ├── quote v1.0.38
│ │ └── proc-macro2 v1.0.92 (*)
│ └── syn v2.0.93
│ ├── proc-macro2 v1.0.92 (*)
│ ├── quote v1.0.38 (*)
│ └── unicode-ident v1.0.14
└── serde_json v1.0.134
├── itoa v1.0.14
├── memchr v2.7.4
├── ryu v1.0.18
└── serde v1.0.217 (*)The root is your own package. Direct dependencies sit one level below it, and indentation shows who depends on whom. A (*) marks a subtree already printed above, so Cargo omits it to keep the output short. A (proc-macro) marker means the crate runs at compile time.
Finding why a crate is present
The inverse view, -i (--invert), starts at one crate and shows everything that pulls it in:
cargo tree -i proc-macro2Use this when a crate you never chose shows up in your build. The output walks upward from proc-macro2 through quote, syn and serde_derive to serde, which is the dependency you declared.
Spotting duplicates
Two different versions of one crate can appear in a build when requirements cannot be satisfied by a single version. This is legal, but it increases compile time and binary size. It can also cause type errors when a type from version 1 is passed to code expecting version 2.
cargo tree --duplicatesThe short form is -d. It lists only crates that appear with more than one version. If it prints nothing, there are no duplicates.
Inspecting features
Chapter 7 showed how features switch on optional code. To see which features are enabled, and which dependency enabled them, use the features edge kind:
cargo tree -e featuresEach crate is followed by lines like serde feature "derive" showing the enabled feature. Combine it with -i to answer "who turned this feature on?":
cargo tree -e features -i serdeUseful options
| Option | Effect |
|---|---|
| `-i <crate>` | Show what depends on the crate |
| `-d` | Show only crates with duplicate versions |
| `-e features` | Include feature information |
| `-e no-dev` | Leave out dev-dependencies |
| `--depth 1` | Show only direct dependencies |
| `-p <package>` | Choose a package in a workspace |
--depth 1 is a quick way to check what you declared directly, including any cargo add result.
cargo tree --depth 1Common mistakes
- Running
cargo treeand expecting build-only crates. Build dependencies appear, but target-specific ones for other platforms do not. Pass--target allto see them. - Reading
(*)as a missing dependency. It means the subtree was printed earlier. - Assuming
-itakes a version. To pick one version, use the form-i [email protected].
Exercise
Your build includes a crate called memchr that you never added. Which command shows which of your dependencies pulls it in, and which command shows only the crates you declared directly?
Show answer
cargo tree -i memchr shows the chain of dependents leading back to your package. cargo tree --depth 1 shows only your direct dependencies.
Exercise
A compile error says a Url type from one version of a crate does not match a Url from another version. Which cargo tree option helps you confirm the cause?
Show answer
cargo tree -d (--duplicates) lists crates present in more than one version. If url appears twice, two dependencies require incompatible versions. Use -i url@<version> for each to see which crates need them.