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] [--] [<PATH>]

PARAMETERS

-d, --duplicates
    Only show duplicate dependencies

-e, --edges
    Show edges to dependencies

--format <FORMAT>
    Change format: normal, compact, indented
(default: normal)

--from <PATH>
    Show what depends on this package

-i, --invert
    Invert tree direction (reverse deps)

--no-dedupe
    Do not dedupe dependencies

--no-fail-fast
    Continue on errors

-p, --prefix <PREFIX>
    Prefix style: indent, line
(default: indent)

-t, --target <TARGET>
    Build for specific target triple

--all-features
    Include all features

--no-default-features
    Exclude default features

-p, --package <SPEC>...
    Select package(s)

--workspace
    Manipulate entire workspace

-h, --help
    Print help

-V, --version
    Print version

DESCRIPTION

cargo tree is a powerful Cargo subcommand that generates a visual tree representation of your Rust project's dependencies. It helps developers understand the dependency graph, identify version conflicts, duplicates, and transitive dependencies at a glance.

Installation is straightforward: cargo install cargo-tree. Once installed, navigate to your project directory and run cargo tree to see the full dependency tree, with your crate at the root and leaves as leaf dependencies.

Key features include customizable formats (normal, compact, indented), prefix styles for indentation, inversion to show reverse dependencies (what depends on a package), and filtering for duplicates or specific targets. It supports workspaces, multiple targets, and feature flags, mirroring Cargo's build options.

This tool is invaluable for debugging build issues, optimizing dependencies, and auditing security vulnerabilities in large projects. Output can be ASCII or UTF-8 for better visuals. It processes Cargo.lock and Cargo.toml to reflect exact resolved versions.

CAVEATS

Requires Rust and Cargo installed. Inherits Cargo's limitations on large workspaces. Output may be verbose for deeply nested deps.

INSTALLATION

cargo install cargo-tree
Requires Rust stable toolchain.

EXAMPLE

cargo tree --duplicates --format compact
Shows compact view of duplicate deps.

OUTPUT FORMATS

normal: Default tree.
compact: One line per dep.
indented: Indented list.

HISTORY

Developed by Embark Studios in 2018 as an open-source tool. Transferred to sfackler/cargo-tree in 2020; actively maintained with regular updates for Cargo compatibility.

SEE ALSO

cargo(1)

Copied to clipboard