CARGO-TREE(1)
π cargo-tree β CARGO-TREE(1) π
okay bestie, welcome to the General Commands Manual entry for cargo-tree, and no you are not ready for how cute a dependency graph can be. let's get into it π«Ά
β¨ NAME β¨
cargo-tree β display a tree visualization of a dependency graph π³
π SYNOPSIS π
cargo tree [options]
π¦ DESCRIPTION π¦
bestie. this command straight up prints a whole tree of your dependencies to the terminal. iconic behavior fr fr. here's what it looks like for a lil baby project that depends on the βrandβ package:
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
so the packages with a (*) next to them? those got βde-duplicatedβ. the deps for that package already showed up somewhere else in the graph, so cargo is NOT repeating them. if you want the duplicates back, use the --no-dedupe option. β¨
the -e flag lets you pick which dependency kinds to show. the βfeaturesβ kind flips the output so it shows the features each dependency has enabled. like, 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
in this tree, myproject depends on log with the serde feature, and log depends on cfg-if with βdefaultβ features. when you're using -e features, it can be helpful to also use the -i flag to show how the features flow into a package. check the examples below for more deets. π
π Feature Unification π
this command shows you a graph that's much closer to a feature-unified graph β like, the one Cargo will actually build β rather than whatever you typed into Cargo.toml. for example: if you list the same dependency in both [dependencies] and [dev-dependencies] but with different features turned on. this command may merge all those features and slap a (*) on one of the dependencies to indicate it's a duplicate. π
so, if you want 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, plain cargo tree is pretty close. however β it does not guarantee exact equivalence to what Cargo is actually going to build, because compilation is complex and depends on lots of different factors. no cap.
want to 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 πΈ
-ispec,--invertspec β show the reverse dependencies for the given package. this flag inverts the tree and shows you the packages that depend on the given package. π«Ά
heads up: in a workspace, by default it only displays the package's reverse dependencies inside the tree of the workspace member in the current directory. the --workspace flag extends it so it shows the package's reverse dependencies across the entire workspace. and the -p flag can be used to display the package's reverse dependencies only within the subtree of the package you gave to -p.
--prunespec β prune the given package from the display of the dependency tree. βοΈ--depthdepth β maximum display depth of the dependency tree. a depth of1displays the direct dependencies, for example.
if the given value is workspace, it only shows the dependencies that are members of the current workspace instead. π
--no-dedupeβ do not de-duplicate repeated dependencies. normally, when a package has already displayed its dependencies, further occurrences won't re-display its dependencies and will include a(*)to indicate it's already been shown. this flag makes those duplicates repeat. π-d,--duplicatesβ show only dependencies which come in multiple versions (this implies--invert). when used with the-pflag, it only shows duplicates within the subtree of the given package.
lowkey this one is a lifesaver: avoiding building that same package multiple times is good for build times and executable sizes, and this flag helps you identify the offending packages. then you can investigate whether the package that depends on the duplicate with the older version can be updated to the newer version, so only one instance gets built. π₯
-ekinds,--edgeskinds β 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.
the normal, build, dev, and all dependency kinds cannot be mixed with the no-normal, no-build, or no-dev dependency kinds. β οΈ
the default is normal,build,dev.
--targettriple β 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 π
--charsetcharset β chooses the character set to use for the tree. valid values are βutf8β or βasciiβ. when unspecified, cargo will auto-select a value. β¨-fformat,--formatformat β 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 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.
--prefixprefix β sets how each line is displayed. the prefix value 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 no package selection options are given, 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 β 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-pathpath β 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 isn't available. with this flag, Cargo will attempt to proceed without the network if possible.
beware: this may result in a 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-pathPATH β changes the path of the lockfile from the default (<workspace_root>/Cargo.lock) to PATH. PATH must 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 from PATH, or write a new lockfile into the provided PATH if it doesn't exist. this flag can be used to run most commands in read-only directories, writing the lockfile into the provided PATH. π
this option is only available on the nightly channel:https://doc.rust-lang.org/book/appendix-07-nightly-rust.htmland requires the-Z unstable-optionsflag to enable (see #14421:https://github.com/rust-lang/cargo/issues/14421). π
π¦ 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 for more details: https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options β¨
(part 1 done bestie, part 2 is coming, don't go anywhere π)
-F features,--features featuresβ a list of features to turn on, separated by spaces or commas. Want to poke a feature on a workspace member? Usepackage-name/feature-namesyntax. You can drop this flag more than once and every feature you listed gets enabled. Slay.--all-featuresβ turns on every available feature of every selected package. Absolutely no crumbs left. π--no-default-featuresβ do not activate the default feature of the selected packages. Sometimes the default is not giving. π
β¨ Display Options β¨
-v,--verboseβ verbose output, bestie. Say it twice and you get "very verbose" output, which includes extra stuff like dependency warnings and build script output. You can also set this with theterm.verboseconfig value (https://doc.rust-lang.org/cargo/reference/config.html).-q,--quietβ do not print cargo log messages. Silence is golden, lowkey. You can also set this with theterm.quietconfig value (https://doc.rust-lang.org/cargo/reference/config.html).--color whenβ controls when colored output is used. The valid values are:auto(the default): automatically detect if color support is available on the terminal.always: always display colors.never: never display colors.
You can also set this 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). See the rustup documentation (https://rust-lang.github.io/rustup/overrides.html) for more on how toolchain overrides work. so real.--config KEY=VALUE or PATHβ overrides a Cargo configuration value. The argument should be in TOML syntax ofKEY=VALUE, or given as a path to an extra configuration file. You can specify this flag multiple times. See the command-line overrides section (https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides) for more info.-C PATHβ 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 also the directories searched for discovering.cargo/config.toml, for example. This option must appear before the command name, likecargo -C path/to/my-project build.
Heads up: this option is only available on the nightly channel (https://doc.rust-lang.org/book/appendix-07-nightly-rust.html) and it needs the-Z unstable-optionsflag to enable it (see #10098 athttps://github.com/rust-lang/cargo/issues/10098). Nightly girls only, iykyk. π
-h,--helpβ prints help information. The classic. π«Ά-Z flagβ unstable (nightly-only) flags for Cargo. Runcargo -Z helpfor the details.
π ENVIRONMENT π
See the reference (https://doc.rust-lang.org/cargo/reference/environment-variables.html) for all the deets on environment variables that Cargo reads. It's giving homework but it's important. π
π EXIT STATUS π
0: Cargo succeeded. ate. β101: Cargo failed to complete. we are not okay. π
β¨ 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, 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). the way that all connects π§
If you're having difficulty cross-referencing the de-duplicated (*) entries, try with the --no-dedupe flag to get the full output. no dedupe, no mystery.
π¦ SEE ALSO π¦
and that's the whole page, bestie π this one's CARGO-TREE(1) β the source is the Cargo toolchain docs, matching the cargo-tree version that ships with the Cargo release this page came from, and the date on the footer is whatever day that edition dropped. you understood the assignment, now go tree something πΈ
π¦ where this page comes from
- ubuntu jammy: cargo-1.89 1.89.0+dfsg~24.04-0ubuntu0.22.04.2 Β· Apache-2.0 WITH LLVM exception
- ubuntu noble: cargo-1.89 1.89.0+dfsg~24.04-0ubuntu0.24.04.2 Β· Apache-2.0 WITH LLVM exception
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms