CARGO-TREE(1)

๐Ÿ“ฆ version 1.85.1, in ubuntu jammy, ubuntu noble, ubuntu questing

๐ŸŽ€ 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 of 1 displays 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 -p flag, 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 โ€” the normal, build, dev, and all dependency kinds cannot be mixed with the no-normal, no-build, or no-dev dependency kinds. pick a lane. ๐Ÿ’€

The default is normal,build,dev.

๐ŸŽ€ 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 a use statement, of the package's library.
  • --prefix prefix โ€” 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 ๐Ÿ’…

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. See cargo-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 --workspace flag. 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 the Cargo.toml file. By default, Cargo searches for the Cargo.toml file in the current directory or any parent directory. ๐Ÿ”
  • --locked โ€” Asserts that the exact same dependencies and versions are used as when the existing Cargo.lock file 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 --locked and --offline. ๐ŸงŠ
  • --lockfile-path PATH โ€” Changes the path of the lockfile from the default (<workspace_root>/Cargo.lock) to PATH. PATH must end with Cargo.lock (e.g. --lockfile-path /tmp/temporary-lockfile/Cargo.lock). Note that providing --lockfile-path will 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. ๐Ÿ“
๐Ÿ’€ 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-options flag to enable (see #14421 at https://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

  • -F features, --features features โ€” a list of features to turn ON, separated by spaces or commas, your choice bestie. If you wanna enable features of workspace members, use the package-name/feature-name syntax. 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 the term.verbose config value (it lives over at https://doc.rust-lang.org/cargo/reference/config.html).
  • -q, --quiet โ€” do not print cargo log messages. Silence. Peace. ๐Ÿง˜ Can also be set with the term.quiet config value (https://doc.rust-lang.org/cargo/reference/config.html).
  • --color when โ€” 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 to cargo starts with +, it gets interpreted as a rustup toolchain name, like +stable or +nightly. The rustup docs over at https://rust-lang.github.io/rustup/overrides.html have the whole lore on how toolchain overrides work. ๐Ÿฆ‹
  • --config KEY=VALUE or PATH โ€” overrides a Cargo configuration value. The argument should be in TOML syntax as KEY=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 at https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides. ๐Ÿซถ
  • -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 the directories it searches to discover .cargo/config.toml, for example. Heads up: this option must appear before the command name, like cargo -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 issue #10098 at https://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. ๐Ÿ‘‘
  • -Z flag โ€” unstable (nightly-only) flags for Cargo. Run cargo -Z help for 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 ๐ŸŒŸ

  1. Display the tree for the package in the current directory:
cargo tree
  1. Display all the packages that depend on the syn package:
cargo tree -i syn
  1. Show the features enabled on each package:
cargo tree --format "{p} {f}"
  1. Show all packages that are built multiple times. This can happen if multiple semver-incompatible versions appear in the tree (like 1.0.0 and 2.0.0).
cargo tree -d
  1. Explain why features are enabled for the syn package:
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