CARGO-TREE(1)

πŸ“¦ version 1.79.0, in ubuntu jammy, ubuntu noble

πŸ’… 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 -i flag 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 --workspace flag extends that so you get reverse deps across the entire workspace. And the -p flag 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 of 1 shows 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 -p flag, 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: the normal, build, dev, and all dependency kinds cannot be mixed with no-normal, no-build, or no-dev dependency kinds. no mixing, fr fr.

the default is normal,build,dev.


πŸŽ€ 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 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 πŸ“¦

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. see cargo-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 --workspace flag. 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 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 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 --locked and --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 with package-name/feature-name syntax. 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 the term.verbose config value <https://doc.rust-lang.org/cargo/reference/config.html>.
  • -q, --quiet β€” do not print cargo log messages. may also be specified with the term.quiet config 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 to cargo begins with +, it gets interpreted as a rustup toolchain name (such as +stable or +nightly). see the rustup documentation <https://rust-lang.github.io/rustup/overrides.html> for more information about how toolchain overrides work.
  • --config KEY=VALUE or PATH β€” overrides a Cargo configuration value. the argument should be in TOML syntax of KEY=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 example 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 #10098 <https://github.com/rust-lang/cargo/issues/10098>).
  • -h, --help β€” prints help information.
  • -Z flag β€” unstable (nightly-only) flags to Cargo. run cargo -Z help for 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 πŸ’…


  1. Display the tree for the package in the current directory β€” no args, no thoughts, just vibes:
cargo tree
  1. Display all the packages that depend on the syn package β€” flip the graph, see who's obsessed with syn:
cargo tree -i syn
  1. Show the features enabled on each package β€” the format string does the heavy lifting here:
cargo tree --format "{p} {f}"
  1. Show all packages that are built multiple times. This can happen when multiple semver-incompatible versions show up in the tree, like 1.0.0 and 2.0.0 living their separate lives:
cargo tree -d
  1. Explain why features are enabled for the syn package β€” 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