LinuxCommandLibrary

cargo-tree

Display a crate's dependency tree

TLDR

Show a dependency tree of the current project

$ cargo tree
copy

Only show dependencies up to the specified depth (e.g. when n is 1, display only direct dependencies)
$ cargo tree --depth [n]
copy

Do not display the given package (and its dependencies) in the tree
$ cargo tree --prune [package_spec]
copy

Show all occurrences of repeated dependencies
$ cargo tree --no-dedupe
copy

Only show normal/build/development dependencies
$ cargo tree [[-e|--edges]] [normal|build|dev]
copy

SYNOPSIS

cargo tree [OPTIONS]

PARAMETERS

-p , --package
    Display the dependency tree for a specific package within the workspace. This is useful in multi-package workspaces.

--target
    Display dependencies for a specific compilation target (e.g., x86_64-unknown-linux-gnu). This limits the dependencies shown to only those relevant for that target.

--depth
    Limit the display of the dependency tree to the specified maximum depth. Helps in focusing on immediate dependencies.

--invert
    Invert the tree, showing reverse dependencies. This means it shows which packages depend on a given package, rather than what a package depends on.

--no-dedupe
    Do not deduplicate repeated dependencies in the tree. By default, duplicate entries are marked with (*) and not fully repeated.

--all-features
    Activate all available features of all packages involved in the dependency graph.

--no-default-features
    Do not activate the default features of the selected packages.

--features
    A space-separated list of features to activate for the selected packages.

--build
    Include build dependencies (from the [build-dependencies] section) in the tree.

--dev
    Include development dependencies (from the [dev-dependencies] section) in the tree.

--workspace
    Display the dependency tree for all packages in the current workspace.

--filter
    Filter out specified packages from the tree display. Can be used to reduce clutter.

--format
    Output the dependency tree in a specific format, such as json, dot, or graphviz.

--locked
    Require Cargo.lock to be up to date and not regenerate it.

--frozen
    Require Cargo.lock and Cargo.toml to be up to date and not regenerate or update them.

--offline
    Operate without network access. Requires all dependencies to be cached locally.

--manifest-path
    Path to the Cargo.toml file. Useful when not running from the project root.

DESCRIPTION

cargo tree is a powerful subcommand of cargo, the Rust package manager, designed to display the dependency graph of a Rust project. It recursively lists all direct and transitive dependencies required by a package, providing a clear hierarchical view of its entire dependency ecosystem. This tool is invaluable for understanding the composition of a project, identifying potential dependency conflicts (e.g., multiple versions of the same crate), debugging build issues, and optimizing project structure. It can filter dependencies by type (build, dev, target), show which features are enabled, and even invert the tree to show reverse dependencies. Its ability to represent the complex relationships between crates makes it an essential utility for Rust developers.

CAVEATS

The output for large projects can be extremely verbose, making it difficult to parse without filtering or limiting depth.
cargo tree relies on the project's Cargo.lock file or performs dependency resolution; an outdated Cargo.lock might not reflect the most current dependency graph.
Different versions of the same crate might appear in the tree, especially if explicit version ranges or feature flags lead to divergent dependency paths. Duplicates are marked with (*) by default.

UNDERSTANDING OUTPUT SYMBOLS

cargo tree uses special symbols to convey additional information about dependencies:

  • (*): Indicates a duplicate dependency that was resolved to an earlier version in the tree and thus not fully re-listed.
  • (proc-macro): Marks a procedural macro crate, which is compiled for the host system and executed during compilation.
  • (build): Denotes a build dependency, specified in the [build-dependencies] section of Cargo.toml.
  • (dev): Signifies a development dependency, found in the [dev-dependencies] section, typically used for tests or examples.
  • (target <TRIPLE>): Indicates a target-specific dependency, only included when building for a particular target triple.

FEATURE ACTIVATION AND CONDITIONAL COMPILATION

A significant strength of cargo tree is its ability to show which features are activated for each crate in the dependency graph. This is crucial for understanding how optional dependencies and conditional compilation affect the final build. The command can be configured with --all-features, --no-default-features, or --features to simulate different build configurations and observe their impact on the dependency tree.

HISTORY

Initially, cargo tree existed as an external cargo-tree subcommand, distributed as a separate Cargo plugin or binary. Due to its widespread utility and frequent adoption by the Rust community for dependency analysis, it was officially integrated into the main cargo binary as a built-in subcommand. This integration occurred around the release of Rust 1.44 in July 2020, making it more accessible and simplifying its usage and discoverability for all Rust developers.

SEE ALSO

Copied to clipboard