Skip to content

Commit 238a9fa

Browse files
committed
Auto merge of #9488 - weihanglo:issue-9165, r=ehuss
`cargo tree -e no-proc-macro` to hide procedural macro dependencies Probably resolves #9165 `cargo tree -e no-proc-macro` now hides procedural macro dependencies. Note that when passed with `--invert <spec>`, the spec's reverse proc-macro dependencies are also hidden. Is this desired result? Also, since I want `-p <spec>` and `-i <spec>` works with any valid package-id in the project, the filter logic is written in print stage instead of graph build stage
2 parents 2f3df16 + c2fd499 commit 238a9fa

File tree

7 files changed

+242
-31
lines changed

7 files changed

+242
-31
lines changed

src/bin/cargo/commands/tree.rs

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ pub fn cli() -> App {
4343
"edges",
4444
"KINDS",
4545
"The kinds of dependencies to display \
46-
(features, normal, build, dev, all, no-dev, no-build, no-normal)",
46+
(features, normal, build, dev, all, \
47+
no-normal, no-build, no-dev, no-proc-macro)",
4748
)
4849
.short("e"),
4950
)
@@ -147,7 +148,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
147148
};
148149
let target = tree::Target::from_cli(targets);
149150

150-
let edge_kinds = parse_edge_kinds(config, args)?;
151+
let (edge_kinds, no_proc_macro) = parse_edge_kinds(config, args)?;
151152
let graph_features = edge_kinds.contains(&EdgeKind::Feature);
152153

153154
let packages = args.packages_from_flags()?;
@@ -201,25 +202,51 @@ subtree of the package given to -p.\n\
201202
charset,
202203
format: args.value_of("format").unwrap().to_string(),
203204
graph_features,
205+
no_proc_macro,
204206
};
205207

208+
if opts.graph_features && opts.duplicates {
209+
return Err(format_err!("the `-e features` flag does not support `--duplicates`").into());
210+
}
211+
206212
tree::build_and_print(&ws, &opts)?;
207213
Ok(())
208214
}
209215

210-
fn parse_edge_kinds(config: &Config, args: &ArgMatches<'_>) -> CargoResult<HashSet<EdgeKind>> {
211-
let mut kinds: Vec<&str> = args
212-
.values_of("edges")
213-
.map_or_else(|| Vec::new(), |es| es.flat_map(|e| e.split(',')).collect());
214-
if args.is_present("no-dev-dependencies") {
215-
config
216-
.shell()
217-
.warn("the --no-dev-dependencies flag has changed to -e=no-dev")?;
218-
kinds.push("no-dev");
219-
}
220-
if kinds.is_empty() {
221-
kinds.extend(&["normal", "build", "dev"]);
222-
}
216+
/// Parses `--edges` option.
217+
///
218+
/// Returns a tuple of `EdgeKind` map and `no_proc_marco` flag.
219+
fn parse_edge_kinds(
220+
config: &Config,
221+
args: &ArgMatches<'_>,
222+
) -> CargoResult<(HashSet<EdgeKind>, bool)> {
223+
let (kinds, no_proc_macro) = {
224+
let mut no_proc_macro = false;
225+
let mut kinds = args.values_of("edges").map_or_else(
226+
|| Vec::new(),
227+
|es| {
228+
es.flat_map(|e| e.split(','))
229+
.filter(|e| {
230+
no_proc_macro = *e == "no-proc-macro";
231+
!no_proc_macro
232+
})
233+
.collect()
234+
},
235+
);
236+
237+
if args.is_present("no-dev-dependencies") {
238+
config
239+
.shell()
240+
.warn("the --no-dev-dependencies flag has changed to -e=no-dev")?;
241+
kinds.push("no-dev");
242+
}
243+
244+
if kinds.is_empty() {
245+
kinds.extend(&["normal", "build", "dev"]);
246+
}
247+
248+
(kinds, no_proc_macro)
249+
};
223250

224251
let mut result = HashSet::new();
225252
let insert_defaults = |result: &mut HashSet<EdgeKind>| {
@@ -231,7 +258,7 @@ fn parse_edge_kinds(config: &Config, args: &ArgMatches<'_>) -> CargoResult<HashS
231258
bail!(
232259
"unknown edge kind `{}`, valid values are \
233260
\"normal\", \"build\", \"dev\", \
234-
\"no-normal\", \"no-build\", \"no-dev\", \
261+
\"no-normal\", \"no-build\", \"no-dev\", \"no-proc-macro\", \
235262
\"features\", or \"all\"",
236263
k
237264
)
@@ -245,12 +272,17 @@ fn parse_edge_kinds(config: &Config, args: &ArgMatches<'_>) -> CargoResult<HashS
245272
"no-dev" => result.remove(&EdgeKind::Dep(DepKind::Development)),
246273
"features" => result.insert(EdgeKind::Feature),
247274
"normal" | "build" | "dev" | "all" => {
248-
bail!("`no-` dependency kinds cannot be mixed with other dependency kinds")
275+
bail!(
276+
"`{}` dependency kind cannot be mixed with \
277+
\"no-normal\", \"no-build\", or \"no-dev\" \
278+
dependency kinds",
279+
kind
280+
)
249281
}
250282
k => return unknown(k),
251283
};
252284
}
253-
return Ok(result);
285+
return Ok((result, no_proc_macro));
254286
}
255287
for kind in &kinds {
256288
match *kind {
@@ -276,5 +308,5 @@ fn parse_edge_kinds(config: &Config, args: &ArgMatches<'_>) -> CargoResult<HashS
276308
if kinds.len() == 1 && kinds[0] == "features" {
277309
insert_defaults(&mut result);
278310
}
279-
Ok(result)
311+
Ok((result, no_proc_macro))
280312
}

src/cargo/ops/tree/mod.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::core::{Package, PackageId, PackageIdSpec, Workspace};
88
use crate::ops::{self, Packages};
99
use crate::util::{CargoResult, Config};
1010
use crate::{drop_print, drop_println};
11-
use anyhow::{bail, Context};
11+
use anyhow::Context;
1212
use graph::Graph;
1313
use std::collections::{HashMap, HashSet};
1414
use std::str::FromStr;
@@ -43,6 +43,8 @@ pub struct TreeOptions {
4343
pub format: String,
4444
/// Includes features in the tree as separate nodes.
4545
pub graph_features: bool,
46+
/// Exculdes proc-macro dependencies.
47+
pub no_proc_macro: bool,
4648
}
4749

4850
#[derive(PartialEq)]
@@ -122,9 +124,6 @@ static ASCII_SYMBOLS: Symbols = Symbols {
122124

123125
/// Entry point for the `cargo tree` command.
124126
pub fn build_and_print(ws: &Workspace<'_>, opts: &TreeOptions) -> CargoResult<()> {
125-
if opts.graph_features && opts.duplicates {
126-
bail!("the `-e features` flag does not support `--duplicates`");
127-
}
128127
let requested_targets = match &opts.target {
129128
Target::All | Target::Host => Vec::new(),
130129
Target::Specific(t) => t.clone(),
@@ -241,6 +240,7 @@ fn print(
241240
symbols,
242241
opts.prefix,
243242
opts.no_dedupe,
243+
opts.no_proc_macro,
244244
&mut visited_deps,
245245
&mut levels_continue,
246246
&mut print_stack,
@@ -259,6 +259,7 @@ fn print_node<'a>(
259259
symbols: &Symbols,
260260
prefix: Prefix,
261261
no_dedupe: bool,
262+
no_proc_macro: bool,
262263
visited_deps: &mut HashSet<usize>,
263264
levels_continue: &mut Vec<bool>,
264265
print_stack: &mut Vec<usize>,
@@ -316,6 +317,7 @@ fn print_node<'a>(
316317
symbols,
317318
prefix,
318319
no_dedupe,
320+
no_proc_macro,
319321
visited_deps,
320322
levels_continue,
321323
print_stack,
@@ -334,6 +336,7 @@ fn print_dependencies<'a>(
334336
symbols: &Symbols,
335337
prefix: Prefix,
336338
no_dedupe: bool,
339+
no_proc_macro: bool,
337340
visited_deps: &mut HashSet<usize>,
338341
levels_continue: &mut Vec<bool>,
339342
print_stack: &mut Vec<usize>,
@@ -362,7 +365,23 @@ fn print_dependencies<'a>(
362365
}
363366
}
364367

365-
let mut it = deps.iter().peekable();
368+
let mut it = deps
369+
.iter()
370+
.filter(|dep| {
371+
// Filter out proc-macro dependencies.
372+
if no_proc_macro {
373+
match graph.node(**dep) {
374+
&Node::Package { package_id, .. } => {
375+
!graph.package_for_id(package_id).proc_macro()
376+
}
377+
_ => true,
378+
}
379+
} else {
380+
true
381+
}
382+
})
383+
.peekable();
384+
366385
while let Some(dependency) = it.next() {
367386
levels_continue.push(it.peek().is_some());
368387
print_node(
@@ -373,6 +392,7 @@ fn print_dependencies<'a>(
373392
symbols,
374393
prefix,
375394
no_dedupe,
395+
no_proc_macro,
376396
visited_deps,
377397
levels_continue,
378398
print_stack,

src/doc/man/cargo-tree.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,10 @@ The dependency kinds to display. Takes a comma separated list of values:
102102
- `no-normal` — Do not include normal dependencies.
103103
- `no-build` — Do not include build dependencies.
104104
- `no-dev` — Do not include development dependencies.
105+
- `no-proc-macro` — Do not include procedural macro dependencies.
105106

106-
The `no-` prefixed options cannot be mixed with the other dependency kinds.
107+
The `normal`, `build`, `dev`, and `all` dependency kinds cannot be mixed with
108+
`no-normal`, `no-build`, or `no-dev` dependency kinds.
107109

108110
The default is `normal,build,dev`.
109111
{{/option}}

src/doc/man/generated_txt/cargo-tree.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,10 @@ OPTIONS
9999

100100
o no-dev — Do not include development dependencies.
101101

102-
The no- prefixed options cannot be mixed with the other dependency
103-
kinds.
102+
o no-proc-macro — Do not include procedural macro dependencies.
103+
104+
The normal, build, dev, and all dependency kinds cannot be mixed
105+
with no-normal, no-build, or no-dev dependency kinds.
104106

105107
The default is normal,build,dev.
106108

src/doc/src/commands/cargo-tree.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ kind given, then it will automatically include the other dependency kinds.</li>
103103
<li><code>no-normal</code> — Do not include normal dependencies.</li>
104104
<li><code>no-build</code> — Do not include build dependencies.</li>
105105
<li><code>no-dev</code> — Do not include development dependencies.</li>
106+
<li><code>no-proc-macro</code> — Do not include procedural macro dependencies.</li>
106107
</ul>
107-
<p>The <code>no-</code> prefixed options cannot be mixed with the other dependency kinds.</p>
108+
<p>The <code>normal</code>, <code>build</code>, <code>dev</code>, and <code>all</code> dependency kinds cannot be mixed with
109+
<code>no-normal</code>, <code>no-build</code>, or <code>no-dev</code> dependency kinds.</p>
108110
<p>The default is <code>normal,build,dev</code>.</dd>
109111

110112

src/etc/man/cargo-tree.1

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,12 @@ kind given, then it will automatically include the other dependency kinds.
129129
\h'-04'\(bu\h'+02'\fBno\-dev\fR \[em] Do not include development dependencies.
130130
.RE
131131
.sp
132-
The \fBno\-\fR prefixed options cannot be mixed with the other dependency kinds.
132+
.RS 4
133+
\h'-04'\(bu\h'+02'\fBno\-proc\-macro\fR \[em] Do not include procedural macro dependencies.
134+
.RE
135+
.sp
136+
The \fBnormal\fR, \fBbuild\fR, \fBdev\fR, and \fBall\fR dependency kinds cannot be mixed with
137+
\fBno\-normal\fR, \fBno\-build\fR, or \fBno\-dev\fR dependency kinds.
133138
.sp
134139
The default is \fBnormal,build,dev\fR\&.
135140
.RE

0 commit comments

Comments
 (0)