CARGO-TREE(1)
π CARGO-TREE(1) π
okay bestie, welcome to the cargo-tree page from the General Commands Manual π you're literally about to learn how to see your whole dependency graph, so buckle up π
β¨ NAME β¨
cargo-tree β Display a tree visualization of a dependency graph π³
πΈ SYNOPSIS πΈ
cargo tree [options]
π DESCRIPTION π
so this command literally prints out a tree of your dependencies right there in the terminal π«Ά like, straight to your screen, no cap.
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 marked with (*)? π those have been "de-duplicated". the dependencies for that package were already shown somewhere else in the graph, so they don't get repeated. it's giving efficiency π
and if you want to see the duplicates again, use the --no-dedupe option. boom, repeats.
now, the -e flag lets you pick which dependency kinds to display π and the "features" kind switches the whole output up so it shows you the features that are enabled by each dependency. like this β 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 in turn depends on cfg-if with "default" features.
pro tip bestie: when you're using-e features, it can be super helpful to also use the-iflag so you can see how the features flow into a package π¦ the examples below go into way more detail, iykyk.
π¦ Feature Unification π¦
okay so this command shows you a graph that's much closer to the feature-unified graph Cargo will actually build, rather than just what you listed in Cargo.toml π
for instance, say you specify the same dependency in both [dependencies] and [dev-dependencies], but with different features turned on. this command may merge all the features together and show a (*) on one of the dependencies to indicate the duplicate π«Ά
so as a result: for a mostly equivalent overview of what cargo build does, cargo tree -e normal,build is pretty close π― and for a mostly equivalent overview of what cargo test does, cargo tree is pretty close. however β and this is the important part, no cap β it doesn't guarantee the exact equivalence to what Cargo is going to build, because a compilation is complex and depends on lots of different factors π
if you wanna learn more about feature unification, go check out this dedicated section:
https://doc.rust-lang.org/cargo/reference/features.html#feature-unification
β¨ OPTIONS β¨
π³ Tree Options π³
-i spec,--invert specβ bestie this one shows the reverse dependencies of the package you name. It literally flips the whole tree upside down and shows you the packages that depend on that package. sooo useful.
heads up though: in a workspace, by default it only shows the package's reverse deps inside the tree of the workspace member in the current directory. The--workspaceflag extends that so you get reverse deps across the entire workspace. And the-pflag narrows it so you only see reverse deps within the subtree of whatever package you gave to-p.
--prune specβ cuts the given package out of the dependency tree display. gone. not showing. bye π--depth depthβ max display depth of the dependency tree. like, a depth of1shows you the direct dependencies only, for example.--no-dedupeβ turns OFF de-duplication of repeated dependencies. normally when a package already showed its deps, any further times it shows up it won't re-display them, it just slaps a(*)on there like "already been here bestie". this flag makes those duplicates repeat anyway.-d,--duplicatesβ shows only dependencies that come in multiple versions (and yes, it implies--invert). when you use it with the-pflag, it only shows duplicates within the subtree of the package you gave.
honestly it can be so beneficial for build times and executable sizes to avoid building the same package multiple times, and this flag helps you find the offending packages. then you can go investigate whether the package depending on the older duplicate can be updated to the newer version so only one instance gets built. efficiency era β¨
-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 automatically includes 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.
okay important: thenormal,build,dev, andalldependency kinds cannot be mixed withno-normal,no-build, orno-devdependency kinds. no mixing, fr fr.
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β picks the character set for the tree. valid values are"utf8"or"ascii". when you don't specify, cargo auto-selects a value for you.-f format,--format formatβ sets the format string for each package. the default is"{p}".
it's an arbitrary string used to display each package, and these strings get 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 π¦
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 isn't 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 gets selected.
the default members of a workspace can be set explicitly with the workspace.default-members key in the root manifest. if that's not set, a virtual workspace includes all workspace members (equivalent to passing --workspace), and a non-virtual workspace includes only the root crate itself.
-p specβ¦,--package specβ¦β display only the specified packages. seecargo-pkgid(1)for the SPEC format. this flag can be specified multiple times and supports common Unix glob patterns like*,?and[]. but 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 can be specified multiple times and supports common Unix glob patterns like*,?and[]. but again, 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 exits with an error when either of these scenarios comes up:- 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, like in CI pipelines.
--offlineβ prevents Cargo from accessing the network for any reason. without this flag, Cargo stops with an error if it needs the network and the network isn't available. with this flag, Cargo attempts to proceed without the network if possible.
beware bestie, this may result in a different dependency resolution than online mode. Cargo restricts 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. two for one, slay.
πΈ Feature Selection πΈ
the feature flags let you control which features are enabled. when no feature options are given, the default feature is activated for every selected package.
see the features documentation <https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options> for more details.
-F features,--features featuresβ space or comma separated list of features to activate. features of workspace members may be enabled withpackage-name/feature-namesyntax. this flag may be specified multiple times, which enables all specified features.--all-featuresβ activate all available features of all selected packages.--no-default-featuresβ do not activate the default feature of the selected packages.
π Display Options π
-v,--verboseβ use verbose output. may be specified twice for "very verbose" output, which includes extra output such as dependency warnings and build script output. may also be specified with theterm.verboseconfig value <https://doc.rust-lang.org/cargo/reference/config.html>.-q,--quietβ do not print cargo log messages. may also be specified with theterm.quietconfig value <https://doc.rust-lang.org/cargo/reference/config.html>.--color whenβ control when colored output is used. valid values:auto(default): automatically detect if color support is available on the terminal.always: always display colors.never: never display colors.
may also be specified with the term.color config value <https://doc.rust-lang.org/cargo/reference/config.html>.
π οΈ Common Options π οΈ
+toolchainβ if Cargo has been installed with rustup, and the first argument tocargobegins with+, it gets interpreted as a rustup toolchain name (such as+stableor+nightly). see the rustup documentation <https://rust-lang.github.io/rustup/overrides.html> for more information about how toolchain overrides work.--config KEY=VALUEorPATHβ overrides a Cargo configuration value. the argument should be in TOML syntax ofKEY=VALUE, or provided as a path to an extra configuration file. this flag may be specified multiple times. see the command-line overrides section <https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides> for more information.-C PATHβ changes the current working directory before executing any specified operations. this affects things like where cargo looks by default for the project manifest (Cargo.toml), as well as the directories searched for discovering.cargo/config.toml, for example. this option must appear before the command name, for examplecargo -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-options flag to enable (see #10098 <https://github.com/rust-lang/cargo/issues/10098>).
-h,--helpβ prints help information.-Z flagβ unstable (nightly-only) flags to Cargo. runcargo -Z helpfor details.
π ENVIRONMENT π
see the reference <https://doc.rust-lang.org/cargo/reference/environment-variables.html> for details on environment variables that Cargo reads.
β EXIT STATUS β
0: Cargo succeeded. π101: Cargo failed to complete. π
π EXAMPLES π
Bestie, this is the part where it all clicks. Five little scenarios, five commands, and suddenly the whole dependency graph is your bestie too. Let's go π
- Display the tree for the package in the current directory β no args, no thoughts, just vibes:
cargo tree
- Display all the packages that depend on the
synpackage β flip the graph, see who's obsessed withsyn:
cargo tree -i syn
- Show the features enabled on each package β the format string does the heavy lifting here:
cargo tree --format "{p} {f}"
- Show all packages that are built multiple times. This can happen when multiple semver-incompatible versions show up in the tree, like
1.0.0and2.0.0living their separate lives:
cargo tree -d
- Explain why features are enabled for the
synpackage β the detective arc:
cargo tree -e features -i syn
The -e features flag is what shows the features. The -i flag inverts the graph so instead of showing what syn pulls in, it shows the packages that depend on syn. Here's what that actually looks like:
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" (*)
So how do you read this graph? You follow the chain for each feature from the root, and that tells you why it's included. Walk it with me: the "full" feature is added by the rustversion crate, which is included from myproject (with the default features), and myproject is the package that got selected on the command-line. As for all the other syn features? Those are added by the "default" feature β and yes, "quote" is added by "printing" and "proc-macro", both of which are default features. Look at the girls supporting the girls πΈ
If you're having trouble cross-referencing all those de-duplicated (*) entries, try running it with the --no-dedupe flag to get the full, un-deduplicated output. No cap, that one saves lives.
π SEE ALSO π
That's the whole page, bestie. You did it. π«Ά
This is the Cargo manual's cargo-tree page, section 1 of the man pages β and if you scroll all the way down past everything, the footer is just CARGO-TREE(1) doing its little signature thing. That's the source tag and the page identity, no date drama attached here, so we're keeping it cute and exact. Go build something iconic πβ¨
π¦ where this page comes from
- ubuntu jammy: cargo-1.80 1.80.1+dfsg0ubuntu1~bpo0-0ubuntu0.22.04.1 Β· Apache-2.0 WITH LLVM exception
- ubuntu noble: cargo-1.80 1.80.1+dfsg0ubuntu1-0ubuntu0.24.04.01 Β· MIT OR Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms