Skip to content

Commit 96ff434

Browse files
committed
Rename --dep-kinds to --edges and fold in --graph-features.
1 parent 5ccc5e0 commit 96ff434

File tree

8 files changed

+207
-115
lines changed

8 files changed

+207
-115
lines changed

src/bin/cargo/commands/tree.rs

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::command_prelude::*;
22
use anyhow::{bail, format_err};
33
use cargo::core::dependency::DepKind;
4-
use cargo::ops::tree;
4+
use cargo::ops::tree::{self, EdgeKind};
55
use cargo::util::CargoResult;
66
use std::collections::HashSet;
77
use std::str::FromStr;
@@ -32,12 +32,13 @@ pub fn cli() -> App {
3232
.hidden(true),
3333
)
3434
.arg(
35-
opt(
36-
"dep-kinds",
37-
"Dependency kinds to display \
38-
(normal, build, dev, no-dev, no-build, no-normal, all)",
35+
multi_opt(
36+
"edges",
37+
"KINDS",
38+
"The kinds of dependencies to display \
39+
(features, normal, build, dev, all, no-dev, no-build, no-normal)",
3940
)
40-
.value_name("KINDS"),
41+
.short("e"),
4142
)
4243
.arg(opt("invert", "Invert the tree direction").short("i"))
4344
.arg(Arg::with_name("no-indent").long("no-indent").hidden(true))
@@ -79,7 +80,6 @@ pub fn cli() -> App {
7980
.short("f")
8081
.default_value("{p}"),
8182
)
82-
.arg(opt("graph-features", "Include features in the tree"))
8383
}
8484

8585
pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
@@ -116,15 +116,8 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
116116
};
117117
let target = tree::Target::from_cli(target);
118118

119-
let dep_kinds = if args.is_present("no-dev-dependencies") {
120-
config
121-
.shell()
122-
.warn("the --no-dev-dependencies flag has changed to --dep-kinds=no-dev")?;
123-
Some("no-dev")
124-
} else {
125-
args.value_of("dep-kinds")
126-
};
127-
let dep_kinds = parse_dep_kinds(dep_kinds)?;
119+
let edge_kinds = parse_edge_kinds(config, args)?;
120+
let graph_features = edge_kinds.contains(&EdgeKind::Feature);
128121

129122
let ws = args.workspace(config)?;
130123
let charset = tree::Charset::from_str(args.value_of("charset").unwrap())
@@ -135,44 +128,57 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
135128
no_default_features: args.is_present("no-default-features"),
136129
packages: args.packages_from_flags()?,
137130
target,
138-
dep_kinds,
131+
edge_kinds,
139132
invert: args.is_present("invert"),
140133
prefix,
141134
no_dedupe: args.is_present("no-dedupe"),
142135
duplicates: args.is_present("duplicates"),
143136
charset,
144137
format: args.value_of("format").unwrap().to_string(),
145-
graph_features: args.is_present("graph-features"),
138+
graph_features,
146139
};
147140

148141
tree::build_and_print(&ws, &opts)?;
149142
Ok(())
150143
}
151144

152-
fn parse_dep_kinds(kinds: Option<&str>) -> CargoResult<HashSet<DepKind>> {
153-
let kinds: Vec<&str> = kinds.unwrap_or("all").split(',').collect();
145+
fn parse_edge_kinds(config: &Config, args: &ArgMatches<'_>) -> CargoResult<HashSet<EdgeKind>> {
146+
let mut kinds: Vec<&str> = args
147+
.values_of("edges")
148+
.map_or_else(|| Vec::new(), |es| es.flat_map(|e| e.split(',')).collect());
149+
if args.is_present("no-dev-dependencies") {
150+
config
151+
.shell()
152+
.warn("the --no-dev-dependencies flag has changed to -e=no-dev")?;
153+
kinds.push("no-dev");
154+
}
155+
if kinds.len() == 0 {
156+
kinds.extend(&["normal", "build", "dev"]);
157+
}
158+
154159
let mut result = HashSet::new();
155-
let insert_all = |result: &mut HashSet<DepKind>| {
156-
result.insert(DepKind::Normal);
157-
result.insert(DepKind::Build);
158-
result.insert(DepKind::Development);
160+
let insert_defaults = |result: &mut HashSet<EdgeKind>| {
161+
result.insert(EdgeKind::Dep(DepKind::Normal));
162+
result.insert(EdgeKind::Dep(DepKind::Build));
163+
result.insert(EdgeKind::Dep(DepKind::Development));
159164
};
160165
let unknown = |k| {
161166
bail!(
162-
"unknown dependency kind `{}`, valid values are \
167+
"unknown edge kind `{}`, valid values are \
163168
\"normal\", \"build\", \"dev\", \
164169
\"no-normal\", \"no-build\", \"no-dev\", \
165-
or \"all\"",
170+
\"features\", or \"all\"",
166171
k
167172
)
168173
};
169174
if kinds.iter().any(|k| k.starts_with("no-")) {
170-
insert_all(&mut result);
175+
insert_defaults(&mut result);
171176
for kind in &kinds {
172177
match *kind {
173-
"no-normal" => result.remove(&DepKind::Normal),
174-
"no-build" => result.remove(&DepKind::Build),
175-
"no-dev" => result.remove(&DepKind::Development),
178+
"no-normal" => result.remove(&EdgeKind::Dep(DepKind::Normal)),
179+
"no-build" => result.remove(&EdgeKind::Dep(DepKind::Build)),
180+
"no-dev" => result.remove(&EdgeKind::Dep(DepKind::Development)),
181+
"features" => result.insert(EdgeKind::Feature),
176182
"normal" | "build" | "dev" | "all" => {
177183
bail!("`no-` dependency kinds cannot be mixed with other dependency kinds")
178184
}
@@ -181,20 +187,29 @@ fn parse_dep_kinds(kinds: Option<&str>) -> CargoResult<HashSet<DepKind>> {
181187
}
182188
return Ok(result);
183189
}
184-
for kind in kinds {
185-
match kind {
186-
"all" => insert_all(&mut result),
190+
for kind in &kinds {
191+
match *kind {
192+
"all" => {
193+
insert_defaults(&mut result);
194+
result.insert(EdgeKind::Feature);
195+
}
196+
"features" => {
197+
result.insert(EdgeKind::Feature);
198+
}
187199
"normal" => {
188-
result.insert(DepKind::Normal);
200+
result.insert(EdgeKind::Dep(DepKind::Normal));
189201
}
190202
"build" => {
191-
result.insert(DepKind::Build);
203+
result.insert(EdgeKind::Dep(DepKind::Build));
192204
}
193205
"dev" => {
194-
result.insert(DepKind::Development);
206+
result.insert(EdgeKind::Dep(DepKind::Development));
195207
}
196208
k => return unknown(k),
197209
}
198210
}
211+
if kinds.len() == 1 && kinds[0] == "features" {
212+
insert_defaults(&mut result);
213+
}
199214
Ok(result)
200215
}

src/cargo/ops/tree/graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ fn add_pkg(
333333
return false;
334334
}
335335
// Filter out dev-dependencies if requested.
336-
if !opts.dep_kinds.contains(&dep.kind()) {
336+
if !opts.edge_kinds.contains(&EdgeKind::Dep(dep.kind())) {
337337
return false;
338338
}
339339
if dep.is_optional() {

src/cargo/ops/tree/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct TreeOptions {
2626
/// The platform to filter for.
2727
pub target: Target,
2828
/// The dependency kinds to display.
29-
pub dep_kinds: HashSet<DepKind>,
29+
pub edge_kinds: HashSet<EdgeKind>,
3030
pub invert: bool,
3131
/// The style of prefix for each line.
3232
pub prefix: Prefix,
@@ -124,7 +124,7 @@ static ASCII_SYMBOLS: Symbols = Symbols {
124124
/// Entry point for the `cargo tree` command.
125125
pub fn build_and_print(ws: &Workspace<'_>, opts: &TreeOptions) -> CargoResult<()> {
126126
if opts.graph_features && opts.duplicates {
127-
bail!("the `--graph-features` flag does not support `--duplicates`");
127+
bail!("the `-e features` flag does not support `--duplicates`");
128128
}
129129
let requested_target = match &opts.target {
130130
Target::All | Target::Host => None,
@@ -141,7 +141,10 @@ pub fn build_and_print(ws: &Workspace<'_>, opts: &TreeOptions) -> CargoResult<()
141141
opts.all_features,
142142
!opts.no_default_features,
143143
);
144-
let has_dev = if opts.dep_kinds.contains(&DepKind::Development) {
144+
let has_dev = if opts
145+
.edge_kinds
146+
.contains(&EdgeKind::Dep(DepKind::Development))
147+
{
145148
HasDevUnits::Yes
146149
} else {
147150
HasDevUnits::No

src/doc/man/cargo-tree.adoc

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ cargo-tree - Display a tree visualization of a dependency graph
1414

1515
== DESCRIPTION
1616

17-
This command will display a tree of dependencies to the terminal. An example of a simple project that depends on the "rand" package:
17+
This command will display a tree of dependencies to the terminal. An example
18+
of a simple project that depends on the "rand" package:
1819

1920
----
2021
myproject v0.1.0 (/myproject)
@@ -36,6 +37,24 @@ Packages marked with `(*)` have been "de-duplicated". The dependencies for the
3637
package have already been shown elswhere in the graph, and so are not
3738
repeated. Use the `--no-dedupe` option to repeat the duplicates.
3839

40+
The `-e` flag can be used to select the dependency kinds to display. The
41+
"features" kind changes the output to display the features enabled by
42+
each dependency. For example, `cargo tree -e features`:
43+
44+
----
45+
myproject v0.1.0 (/myproject)
46+
└── log feature "serde"
47+
└── log v0.4.8
48+
├── serde v1.0.106
49+
└── cfg-if feature "default"
50+
└── cfg-if v0.1.10
51+
----
52+
53+
In this tree, `myproject` depends on `log` with the `serde` feature. `log` in
54+
turn depends on `cfg-if` with "default" features. When using `-e features` it
55+
can be helpful to use `-i` and `-p` flags to show how the features flow into a
56+
package. See the examples below for more detail.
57+
3958
== OPTIONS
4059

4160
=== Tree Options
@@ -63,31 +82,30 @@ packages. You can then investigate if the package that depends on the
6382
duplicate with the older version can be updated to the newer version so that
6483
only one instance is built.
6584

66-
*--dep-kinds* KINDS::
85+
*-e* _KINDS_::
86+
*--edges* _KINDS_::
6787
The dependency kinds to display. Takes a comma separated list of values:
68-
69-
- `all` (default) — Show all dependency kinds.
88+
+
89+
- `all` — Show all edge kinds.
7090
- `normal` — Show normal dependencies.
7191
- `build` — Show build dependencies.
7292
- `dev` — Show development dependencies.
93+
- `features` — Show features enabled by each dependency. If this is
94+
the only kind given, then it will automatically include the other
95+
dependency kinds.
7396
- `no-normal` — Do not include normal dependencies.
7497
- `no-build` — Do not include build dependencies.
7598
- `no-dev` — Do not include development dependencies.
7699
+
77-
The `no-` prefixed options cannot be mixed with the other kinds.
100+
The `no-` prefixed options cannot be mixed with the other dependency kinds.
101+
+
102+
The default is `normal,build,dev`.
78103

79104
*--target* _TRIPLE_::
80105
Filter dependencies matching the given target-triple.
81106
The default is the host platform. Use the value `all` to include *all*
82107
targets.
83108

84-
*--graph-features*::
85-
Runs in a special mode where features are included as individual nodes.
86-
This is intended to be used to help explain why a feature is enabled on
87-
any particular package. It is recommended to use with the `-p` and `-i`
88-
flags to show how the features flow into the package. See the examples
89-
below for more detail.
90-
91109
=== Tree Formatting Options
92110

93111
*--charset* _CHARSET_::
@@ -158,9 +176,11 @@ include::section-exit-status.adoc[]
158176

159177
. Explain why features are enabled for the given package:
160178

161-
cargo tree --graph-features -i -p syn
179+
cargo tree -e features -i -p syn
162180
+
163-
An example of what this would display:
181+
The `-e features` flag is used to show features. The `-i` flag is used to
182+
invert the graph so that it displays the packages that depend on `syn` (not
183+
what `syn` depends on). An example of what this would display:
164184
+
165185
----
166186
syn v1.0.17
@@ -187,9 +207,9 @@ syn v1.0.17
187207
----
188208
+
189209
To read this graph, you can follow the chain for each feature from the root to
190-
see why it was included. For example, the "full" feature was added by the
191-
`rustversion` crate which was included from `myproject` (with the default
192-
features), and `myproject` was the package selected on the command-line. All
210+
see why it is included. For example, the "full" feature is added by the
211+
`rustversion` crate which is included from `myproject` (with the default
212+
features), and `myproject` is the package selected on the command-line. All
193213
of the other `syn` features are added by the "default" feature ("quote" is
194214
added by "printing" and "proc-macro", both of which are default features).
195215
+

0 commit comments

Comments
 (0)