cargo-tree(1)
NAME
cargo-tree - Display a tree visualization of a dependency graph
SYNOPSIS
cargo tree
[options]
DESCRIPTION
This command will display a tree of dependencies to the terminal. An example of a 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
Packages marked with (*)
have been "de-duplicated". The dependencies for the
package have already been shown elsewhere in the graph, and so are not
repeated. Use the --no-dedupe
option to repeat the duplicates.
The -e
flag can be used to select the dependency kinds to display. The
"features" kind changes the output to display the features enabled by
each dependency. 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 v0.1.10
In this tree, myproject
depends on log
with the serde
feature. log
in
turn depends on cfg-if
with "default" features. When using -e features
it
can be helpful to use -i
flag to show how the features flow into a package.
See the examples below for more detail.
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 with 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.
--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.It can be beneficial for build times and executable sizes to avoid building that same package multiple times. This flag can help identify the offending packages. You can then investigate if the package that depends on the duplicate with the older version can be updated to the newer version so that only one instance is built.
-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.
The
normal
,build
,dev
, andall
dependency kinds cannot be mixed withno-normal
,no-build
, orno-dev
dependency kinds.The default is
normal,build,dev
. --target
triple- Filter dependencies matching the given target-triple. The default is the host
platform. Use the value
all
to include all targets.
Tree Formatting Options
--charset
charset- Chooses the character set to use for the tree. Valid values are "utf8" or "ascii". Default is "utf8".
-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 ause
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.
选择包
默认情况下,如果没有指定包,则根据清单文件来选择包(如果没有通过--manifest-path
给出清单文件路径,
则基于当前工作目录进行寻找)。如果是某个工作区的根清单,则选中该工作区的默认成员;否则仅选中清
单所定义的那个包。
工作区的默认成员可通过清单中workspace.default-members
项来显式指定。如果未指定,则其虚拟
工作区会包含全体工作区成员(等同于传递--workspace
标志参数时),而非虚拟工作区则仅包含根部箱自身。
-p
spec...--package
spec...- Display指定包. SPEC的格式参见 cargo-pkgid(1) 。
此标志参数可多次使用且支持Unix通配符(
*
,?
和[]
)。不过,为避免shell可能错误地在Cargo获取到 之前就将通配符展开,应在各个模式串两侧使用单引号或双引号。 --workspace
- Display 工作区中的全体成员.
--exclude
SPEC...- 排除指定包。必须与
--workspace
标志参数共同使用。 此标志参数可多次使用且支持Unix通配符(*
,?
和[]
)。不过,为避免shell可能错误地在Cargo获取到 之前就将通配符展开,应在各个模式串两侧使用单引号或双引号。
Manifest Options
--manifest-path
path- 用于指定
Cargo.toml
文件的路径。默认情况下,Cargo会在当前目录或上级目录中寻找Cargo.toml
文件。 --frozen
--locked
- 这两个选项用于保证
Cargo.lock
文件是最新的。如果该锁文件不存在,或者不是最新的,Cargo 会报错退出。其中--frozen
选项会阻止Cargo访问网络以检查锁文件是否是最新的。这些选项,可用于保证
Cargo.lock
文件是最新的(比如持续集成的构建过程), 或用于避免联网。 --offline
- 禁止Cargo访问网络。如果不添加此选项,Cargo在需要访问网络但网络不可用的情况下,会报错
并停止工作。添加此选项后,Cargo会尽可能尝试不使用网络来工作。
注意,在此情况下可能会产生与联网状态下不同的依赖解析(Dependency Resolution)结果。 Cargo只会使用本地已下载的crate,即便本地的索引副本中表明可能有新版本crate。在离线前下载 所需依赖的方法,参见 cargo-fetch(1) 。
也可以通过
net.offline
config value指定。
特性选择
可通过传递特性参数来控制启用哪些特性。如果没有给定要使用的特性,
则每个已选择的包都会自动使用default
特性。
--features
features- 传递以空格或者逗号分隔的列表,其中给出要启用的特性。工作区成员的特性可通过
包名/特性名
的语法启用。 此参数可多次给定,以分别启用给定的特性。 --all-features
- 为给定的包启用全部可用特性
--no-default-features
- 不启用给定包的
default
特性
Display Options
-v
--verbose
- 启用更加详细的输出。可两次使用来显示"非常详细"的输出,其中包含了诸如 依赖警告 以及 构建脚本输出 等额外的输出内容。
也可通过
term.verbose
指定。 config value. -q
--quiet
- 不输出Cargo的日志信息。也可通过
term.quiet
指定。 config value. --color
when- 控制输出内容的颜色。有效取值如下:
auto
(默认):自动检测终端是否支持带颜色的输出。always
:总显示带颜色的输出。never
:从不显示带颜色的输出。
也可通过
term.color
指定。 config value.
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 for more information about how toolchain overrides work. -h
--help
- Prints help information.
-Z
flag- Unstable (nightly-only) flags to Cargo. Run
cargo -Z help
for details.
环境
关于Cargo所读取的环境变量,可参见the reference
退出状态
0
: Cargo命令执行成功101
: Cargo命令未能完成.
EXAMPLES
-
Display the tree for the package in the current directory:
cargo tree
-
Display all the packages that depend on the
syn
package:cargo tree -i syn
-
Show the features enabled on each package:
cargo tree --format "{p} {f}"
-
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
-
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 onsyn
. 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 is included. For example, the "full" feature is added by the
rustversion
crate which is included frommyproject
(with the default features), andmyproject
is the package selected on the command-line. All of the othersyn
features are added by the "default" feature ("quote" is added by "printing" and "proc-macro", both of which are default features).If you're having difficulty cross-referencing the de-duplicated
(*)
entries, try with the--no-dedupe
flag to get the full output.