CARGO-TREE(1)
๐ cargo-tree(1) ๐
General Commands Manual
okay so bestie, pull up a chair and grab a snack, because we are about to get into cargo-tree โ the command that literally draws out your whole dependency graph like a cute little family tree. ๐ณโจ it's giving "main character energy" for your build, and honestly? we are obsessed. ๐
โจ NAME โจ
cargo-tree โ Display a tree visualization of a dependency graph
โจ SYNOPSIS โจ
cargo tree [options]
๐ธ DESCRIPTION ๐ธ
okay so this command literally prints a whole tree of your dependencies right there in the terminal. ๐ here's what a simple little project that depends on the rand package looks like:
myproject v0.1.0 (/myproject)
`-- rand v0.7.3
|-- getrandom v0.1.14
| |-- cfg-if v0.1.10
| `-- libc v0.2.68
|-- libc v0.2.68 (*)
|-- rand_chacha v0.2.2
| |-- ppv-lite86 v0.2.6
| `-- rand_core v0.5.1
| `-- getrandom v0.1.14 (*)
`-- rand_core v0.5.1 (*)
[build-dependencies]
`-- cc v1.0.50
see those packages with the (*) next to them? those have been "de-duplicated". basically, their dependencies already got shown somewhere else in the graph, so cargo is like "we've been here, no need to repeat ourselves ๐
" and just doesn't show them again. if you do want the duplicates repeated, hit it with the --no-dedupe option. ๐ซถ
the -e flag is how you pick which dependency kinds get displayed. the features kind is special โ it flips the output so you can see the features each dependency has enabled. for example, cargo tree -e features:
myproject v0.1.0 (/myproject)
`-- log feature "serde"
`-- log v0.4.8
|-- serde v1.0.106
`-- cfg-if feature "default"
`-- cfg-if v1.0.10
so in this tree, myproject depends on log with the serde feature. and log? it depends on cfg-if with the "default" features. when you're using -e features, it's honestly super helpful to also use the -i flag to see how the features flow into a package. ๐ (peep the examples further down for the tea โ)
๐ฆ Feature Unification ๐ฆ
okay so here's the thing โ this command shows you a graph that's way closer to the feature-unified graph that Cargo will actually build, rather than whatever you literally typed into your Cargo.toml. for instance, if you put the same dependency in both [dependencies] and [dev-dependencies] but with different features turned on โ this command may merge all the features together and slap a (*) on one of the dependencies to show you it's a duplicate. ๐ญ
so like, if you want a mostly-equivalent overview of what cargo build actually does, cargo tree -e normal,build is pretty close. and for a mostly-equivalent overview of what cargo test does, plain cargo tree is pretty close. however โ and this is important bestie โ it does not guarantee exact equivalence to what Cargo is going to build, because compilation is complicated and depends on a whole bunch of different factors. no cap.
want to learn more about feature unification? check out this dedicated section: https://doc.rust-lang.org/cargo/reference/features.html#feature-unification ๐
๐ OPTIONS ๐
๐ณ Tree Options ๐ณ
-i spec,--invert specโ Show the reverse dependencies for the given package. This flag will invert the tree and display the packages that depend on the given package.
Note that in a workspace, by default it will only display the package's reverse dependencies inside the tree of the workspace member in the current directory. The --workspace flag can be used to extend it so that it will show the package's reverse dependencies across the entire workspace. The -p flag can be used to display the package's reverse dependencies only within the subtree of the package given to -p.
--prune specโ Prune the given package from the display of the dependency tree. โ๏ธ--depth depthโ Maximum display depth of the dependency tree. A depth of1displays the direct dependencies, for example.
If the given value is workspace, only shows the dependencies that are members of the current workspace, instead.
--no-dedupeโ Do not de-duplicate repeated dependencies. Usually, when a package has already displayed its dependencies, further occurrences will not re-display its dependencies, and will include a(*)to indicate it has already been shown. This flag will cause those duplicates to be repeated. ๐-d,--duplicatesโ Show only dependencies which come in multiple versions (implies--invert). When used with the-pflag, only shows duplicates within the subtree of the given package.
ngl it can be genuinely beneficial for build times and executable sizes to avoid building that same package multiple times. this flag is your bestie for identifying the offending packages. ๐ต๏ธ once you find them, you can investigate whether the package that depends on the duplicate with the older version can be updated to the newer version so that only one instance gets built. slay.
-e kinds,--edges kindsโ The dependency kinds to display. Takes a comma separated list of values:allโ Show all edge kinds.normalโ Show normal dependencies.buildโ Show build dependencies.devโ Show development dependencies.featuresโ Show features enabled by each dependency. If this is the only kind given, then it will automatically include the other dependency kinds.no-normalโ Do not include normal dependencies.no-buildโ Do not include build dependencies.no-devโ Do not include development dependencies.no-proc-macroโ Do not include procedural macro dependencies.
bestie listen โ thenormal,build,dev, andalldependency kinds cannot be mixed with theno-normal,no-build, orno-devdependency kinds. pick a lane. ๐
The default is normal,build,dev.
--target tripleโ Filter dependencies matching the given target triple (https://doc.rust-lang.org/cargo/appendix/glossary.html#target). The default is the host platform. Use the valueallto include all targets. ๐ฏ
๐ Tree Formatting Options ๐
--charset charsetโ Chooses the character set to use for the tree. Valid values are"utf8"or"ascii". When unspecified, cargo will auto-select a value. โจ-f format,--format formatโ Set the format string for each package. The default is"{p}".
This is an arbitrary string which will be used to display each package. The following strings will be replaced with the corresponding value:
{p}โ The package name.{l}โ The package license.{r}โ The package repository URL.{f}โ Comma-separated list of package features that are enabled.{lib}โ The name, as used in ausestatement, of the package's library.
--prefix prefixโ Sets how each line is displayed. Theprefixvalue can be one of:indent(default) โ Shows each line indented as a tree.depthโ Show as a list, with the numeric depth printed before each entry.noneโ Show as a flat list.
๐ Package Selection ๐
okay so by default, when you don't give any package selection options, the packages selected depend on the selected manifest file (based on the current working directory if --manifest-path is not given). If the manifest is the root of a workspace then the workspace's default members are selected, otherwise only the package defined by the manifest will be selected. ๐ซถ
the default members of a workspace can be set explicitly with the workspace.default-members key in the root manifest. If this is not set, a virtual workspace will include all workspace members (equivalent to passing --workspace), and a non-virtual workspace will include only the root crate itself.
-p specโฆ,--package specโฆโ Display only the specified packages. Seecargo-pkgid(1)for the SPEC format. This flag may be specified multiple times and supports common Unix glob patterns like*,?and[]. However โ to avoid your shell accidentally expanding glob patterns before Cargo handles them โ you must use single quotes or double quotes around each pattern. ๐--workspaceโ Display all members in the workspace. ๐--exclude SPECโฆโ Exclude the specified packages. Must be used in conjunction with the--workspaceflag. This flag may be specified multiple times and supports common Unix glob patterns like*,?and[]. However โ to avoid your shell accidentally expanding glob patterns before Cargo handles them โ you must use single quotes or double quotes around each pattern. ๐ซ
๐ฆ Manifest Options ๐ฆ
--manifest-path pathโ Path to theCargo.tomlfile. By default, Cargo searches for theCargo.tomlfile in the current directory or any parent directory. ๐--lockedโ Asserts that the exact same dependencies and versions are used as when the existingCargo.lockfile was originally generated. Cargo will exit with an error when either of the following scenarios arises:- The lock file is missing.
- Cargo attempted to change the lock file due to a different dependency resolution.
It may be used in environments where deterministic builds are desired, such as in CI pipelines. ๐๏ธ
--offlineโ Prevents Cargo from accessing the network for any reason. Without this flag, Cargo will stop with an error if it needs to access the network and the network is not available. With this flag, Cargo will attempt to proceed without the network if possible.
โ ๏ธ beware bestie โ this may result in different dependency resolution than online mode. Cargo will restrict itself to crates that are downloaded locally, even if there might be a newer version as indicated in the local copy of the index. See the cargo-fetch(1) command to download dependencies before going offline. ๐ฅ
May also be specified with the net.offline config value (https://doc.rust-lang.org/cargo/reference/config.html).
--frozenโ Equivalent to specifying both--lockedand--offline. ๐ง--lockfile-path PATHโ Changes the path of the lockfile from the default (<workspace_root>/Cargo.lock) toPATH.PATHmust end withCargo.lock(e.g.--lockfile-path /tmp/temporary-lockfile/Cargo.lock). Note that providing--lockfile-pathwill ignore the existing lockfile at the default path, and instead will either use the lockfile fromPATH, or write a new lockfile into the providedPATHif it doesn't exist. This flag can be used to run most commands in read-only directories, writing the lockfile into the providedPATH. ๐
๐ heads up โ this option is only available on the nightly channel (https://doc.rust-lang.org/book/appendix-07-nightly-rust.html) and requires the-Z unstable-optionsflag to enable (see#14421athttps://github.com/rust-lang/cargo/issues/14421).
โจ Feature Selection โจ
the feature flags are how you control which features get enabled. when no feature options are given, the default feature is activated for every selected package. ๐
see the features documentation for more details: https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options
-Ffeatures,--featuresfeatures โ a list of features to turn ON, separated by spaces or commas, your choice bestie. If you wanna enable features of workspace members, use thepackage-name/feature-namesyntax. And yes, you can pass this flag multiple times โ every feature you list across all those flags gets enabled. Stack 'em. ๐--all-featuresโ activate every available feature of all the selected packages. All of them. No crumbs left. โจ--no-default-featuresโ do NOT activate the default feature of the selected packages. Sometimes you just don't want the default vibe. ๐
๐ Display Options ๐
-v,--verboseโ verbose output, queen. Pass it twice and you get "very verbose" output, which throws in extra stuff like dependency warnings and build script output. Can also be set with theterm.verboseconfig value (it lives over athttps://doc.rust-lang.org/cargo/reference/config.html).-q,--quietโ do not print cargo log messages. Silence. Peace. ๐ง Can also be set with theterm.quietconfig value (https://doc.rust-lang.org/cargo/reference/config.html).--colorwhen โ controls when colored output shows up. Valid values are:auto(the default): automatically detects whether your terminal can do color support. It's giving psychic. ๐ฎalways: always display colors. rainbow brain ๐never: never display colors. monochrome era ๐
Can also be set with the term.color config value (https://doc.rust-lang.org/cargo/reference/config.html).
๐ Common Options ๐
+toolchainโ if Cargo was installed with rustup and the first argument tocargostarts with+, it gets interpreted as a rustup toolchain name, like+stableor+nightly. The rustup docs over athttps://rust-lang.github.io/rustup/overrides.htmlhave the whole lore on how toolchain overrides work. ๐ฆ--configKEY=VALUE or PATH โ overrides a Cargo configuration value. The argument should be in TOML syntax asKEY=VALUE, or you can hand it a path to an extra configuration file instead. You can specify this flag multiple times. More tea in the command-line overrides section athttps://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides. ๐ซถ-CPATH โ changes the current working directory before executing any specified operations. This affects stuff like where cargo looks by default for the project manifest (Cargo.toml), and the directories it searches to discover.cargo/config.toml, for example. Heads up: this option must appear before the command name, likecargo -C path/to/my-project build.
This option is only available on the nightly channel (https://doc.rust-lang.org/book/appendix-07-nightly-rust.html) and requires the-Z unstable-optionsflag to enable (see issue #10098 athttps://github.com/rust-lang/cargo/issues/10098). Nightly-only, bestie, don't try this on stable and then act surprised. ๐ญ
-h,--helpโ prints help information. When in doubt, ask. ๐-Zflag โ unstable (nightly-only) flags for Cargo. Runcargo -Z helpfor the details. Not for the faint of heart. ๐ฅ
๐ธ ENVIRONMENT ๐ธ
Cargo reads environment variables! For all the deets on which ones, see the reference at https://doc.rust-lang.org/cargo/reference/environment-variables.html. โจ
๐ EXIT STATUS ๐
0: Cargo succeeded. slay ๐101: Cargo failed to complete. not slay ๐ญ
๐ EXAMPLES ๐
- Display the tree for the package in the current directory:
cargo tree
- Display all the packages that depend on the
synpackage:
cargo tree -i syn
- Show the features enabled on each package:
cargo tree --format "{p} {f}"
- Show all packages that are built multiple times. This can happen if multiple semver-incompatible versions appear in the tree (like
1.0.0and2.0.0).
cargo tree -d
- Explain why features are enabled for the
synpackage:
cargo tree -e features -i syn
The -e features flag is used to show features. The -i flag is used to invert the graph so that it displays the packages that depend on syn. An example of what this would display:
syn v1.0.17
|-- syn feature "clone-impls"
| `-- syn feature "default"
| `-- rustversion v1.0.2
| `-- rustversion feature "default"
| `-- myproject v0.1.0 (/myproject)
| `-- myproject feature "default" (command-line)
|-- syn feature "default" (*)
|-- syn feature "derive"
| `-- syn feature "default" (*)
|-- syn feature "full"
| `-- rustversion v1.0.2 (*)
|-- syn feature "parsing"
| `-- syn feature "default" (*)
|-- syn feature "printing"
| `-- syn feature "default" (*)
|-- syn feature "proc-macro"
| `-- syn feature "default" (*)
`-- syn feature "quote"
|-- syn feature "printing" (*)
`-- syn feature "proc-macro" (*)
To read this graph, you can follow the chain for each feature from the root to see why it's included. For example, the "full" feature is added by the rustversion crate, which is included from myproject (with the default features), and myproject is the package selected on the command-line. All of the other syn features are added by the "default" feature ("quote" is added by "printing" and "proc-macro", both of which are default features). It's giving family tree but make it dependencies. ๐ณ
If you're having difficulty cross-referencing the de-duplicated (*) entries, try with the --no-dedupe flag to get the full output. iykyk ๐
๐ฆ SEE ALSO ๐ฆ
okay that's the whole page bestie, you're officially a cargo tree scholar ๐โจ This one's CARGO-TREE(1) โ part of the Cargo manual family โ and it came to you dated the same date as the rest of the cargo docs. Go forth and tree responsibly. ๐๐๐ซถ
๐ฆ where this page comes from
- ubuntu jammy: cargo-1.85 1.85.1+dfsg0ubuntu2~bpo0-0ubuntu1.22.04.1 ยท Apache-2.0 WITH LLVM exception
- ubuntu noble: cargo-1.85 1.85.1+dfsg0ubuntu2~bpo0-0ubuntu0.24.04.2 ยท Apache-2.0 WITH LLVM exception
- ubuntu questing: cargo-1.85 1.85.1+dfsg0ubuntu2-0ubuntu1
- ubuntu questing: cargo-1.85 1.85.1+dfsg0ubuntu2-0ubuntu1.25.04.1
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms