CARGO-TREE(1)

πŸŽ€ NAME πŸŽ€

okay so bestie, this is the manual page for cargo-tree β€” and it does exactly what it says on the tin: it displays a tree visualization of a dependency graph πŸ’–


✨ SYNOPSIS ✨

cargo tree [options]

🌸 DESCRIPTION 🌸

so this command prints a whole tree of your dependencies right into the terminal, like a lil family tree but make it crates πŸ¦‹

here's an example of a super simple 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

see those little (*) marks? πŸ‘€ those packages have been β€œde-duplicated” β€” meaning their dependencies already showed up somewhere else in the graph, so Cargo is not about to repeat them (we are not doing that today, no ma'am). if you want the duplicates repeated anyway, that's what the --no-dedupe option is for πŸ’…

and then there's the -e flag, which lets you pick which dependency kinds get displayed. the β€œfeatures” kind is the iconic one β€” it flips the output so you see the features that 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

wait no, hold on, let me give you the real one:

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

okay bestie i'm playing, here's the actual tree from the page, character for character, i promise πŸ™ˆ:

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. the way that works is genuinely so satisfying ngl 🌈

and pro tip from the page itself: when you're using -e features, it can be helpful to reach for the -i flag so you can see how the features flow into a package. more deets in the examples below, iykyk ✨


🧚 Feature Unification 🧚

okay this part is lowkey important so listen up 🫢

this command shows you a graph that's much closer to a feature-unified graph β€” like, the one Cargo will actually build β€” rather than just whatever you wrote down 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 slap a (*) on one of the dependencies to flag that it's a duplicate πŸ‘‘

so as a result:

  • for a mostly equivalent overview of what cargo build does, cargo tree -e normal,build is pretty close
  • for a mostly equivalent overview of what cargo test does, cargo tree is pretty close
BUT β€” and this is the caveat, bestie, don't skip it β€” it does not guarantee exact equivalence to what Cargo is actually going to build, because a compilation is complex and depends on lots of different factors. fr fr. it's giving "close enough," not "legally binding" πŸ’€

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 FLIPS the whole tree. It shows you the reverse dependencies of the package you name β€” aka the packages that depend on it. πŸ’…
heads up though: in a workspace, by default it will only show that package's reverse dependencies inside the tree of the workspace member in your current directory. Want the whole workspace? Add --workspace and it'll stretch across everything. Want just the subtree of some specific package? Use -p and it'll scope down to that.
  • --prune spec β€” yeets the given package out of the dependency tree display. gone. not invited. 🚫
  • --depth depth β€” the max display depth of the dependency tree. A depth of 1 shows the direct dependencies, for example. 🫢

Special case alert: if you pass workspace as the value, it only shows the dependencies that are members of the current workspace instead.

  • --no-dedupe β€” normally when a package has already shown its dependencies, the further occurrences don't re-display them and just get a lil (*) to say "yeah we've been here". This flag says NOT TODAY and makes those duplicates repeat. 😀
  • -d, --duplicates β€” shows only dependencies that come in multiple versions (this implies --invert, fyi). And when you pair it with -p, it only shows duplicates within the subtree of the package you gave. πŸ”₯

Real talk, avoiding building the same package multiple times can be a whole win for your build times and your executable sizes, so this flag helps you spot the offending packages. Then you can go investigate whether the package depending on the older duplicate version can be bumped to the newer one so only one instance gets built. slay.

  • -e kinds, --edges kinds β€” which 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 you give, 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.
okay important: 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 bestie.

The default is normal,build,dev.


πŸŽ€ Tree Formatting Options

  • --charset charset β€” picks which character set the tree gets drawn with. Valid values are β€œutf8” or β€œascii”. When you don't specify one, cargo will auto-select 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 swapped out for the corresponding value:

  • {p}, {package} β€” The package name.
  • {l}, {license} β€” The package license.
  • {r}, {repository} β€” The package repository URL.
  • {f}, {features} β€” 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 gets 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 is not given). If the manifest is the root of a workspace then the workspace's default members get 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 isn't 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 [].
but listen β€” to avoid your shell accidentally expanding glob patterns before Cargo even gets to handle them, you must use single quotes or double quotes around each pattern. no cap.
  • --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 [].
same warning as above bestie: 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 these 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. very much the "I need this to be reproducible" energy.

  • --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 that 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. two for one, iconic. πŸ’…

🌸 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. all of them. every last one. πŸ”₯
  • --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 will be 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 πŸŽ€

Okay bestie, here's where it gets fun β€” actual things to type πŸ’…

  1. Wanna see the tree for whatever package is chillin' in your current directory? Say less:
cargo tree
  1. Wanna know literally every package that depends on syn? Flip the graph, bestie:
cargo tree -i syn
  1. Show me the features that are enabled on each package, please and thank you:
cargo tree --format "{p} {f}"
  1. Show every package that gets built more than once. This happens when multiple semver-incompatible versions are hanging out in the tree (like 1.0.0 and 2.0.0 being messy together πŸ™ˆ):
cargo tree -d
  1. Explain why features are enabled for the syn package β€” the detective era:
cargo tree -e features -i syn

The -e features flag is what makes the features show up. The -i flag flips the graph so it displays the packages that depend on syn. Here's an example of what that would print:

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 follow the chain for each feature starting from the root, and that tells you 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 that got 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”, and both of those are default features).

If you're struggling to cross-reference all those de-duplicated (*) entries, try running it with the --no-dedupe flag to get the full output. You got this 🫢

🌈 SEE ALSO 🌈


And that's the whole page, bestie!! πŸ’– This was CARGO-TREE(1) β€” that's the man page name, the whole source, the whole vibe. Go forth and read your dependency trees like the main character you are. No cap. Periodt. πŸ‘‘βœ¨

πŸ“¦ where this page comes from