Skip to content

Commit

Permalink
Auto merge of #9488 - weihanglo:issue-9165, r=ehuss
Browse files Browse the repository at this point in the history
`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
  • Loading branch information
bors committed May 28, 2021
2 parents 2f3df16 + c2fd499 commit 238a9fa
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 31 deletions.
70 changes: 51 additions & 19 deletions src/bin/cargo/commands/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ pub fn cli() -> App {
"edges",
"KINDS",
"The kinds of dependencies to display \
(features, normal, build, dev, all, no-dev, no-build, no-normal)",
(features, normal, build, dev, all, \
no-normal, no-build, no-dev, no-proc-macro)",
)
.short("e"),
)
Expand Down Expand Up @@ -147,7 +148,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
};
let target = tree::Target::from_cli(targets);

let edge_kinds = parse_edge_kinds(config, args)?;
let (edge_kinds, no_proc_macro) = parse_edge_kinds(config, args)?;
let graph_features = edge_kinds.contains(&EdgeKind::Feature);

let packages = args.packages_from_flags()?;
Expand Down Expand Up @@ -201,25 +202,51 @@ subtree of the package given to -p.\n\
charset,
format: args.value_of("format").unwrap().to_string(),
graph_features,
no_proc_macro,
};

if opts.graph_features && opts.duplicates {
return Err(format_err!("the `-e features` flag does not support `--duplicates`").into());
}

tree::build_and_print(&ws, &opts)?;
Ok(())
}

fn parse_edge_kinds(config: &Config, args: &ArgMatches<'_>) -> CargoResult<HashSet<EdgeKind>> {
let mut kinds: Vec<&str> = args
.values_of("edges")
.map_or_else(|| Vec::new(), |es| es.flat_map(|e| e.split(',')).collect());
if args.is_present("no-dev-dependencies") {
config
.shell()
.warn("the --no-dev-dependencies flag has changed to -e=no-dev")?;
kinds.push("no-dev");
}
if kinds.is_empty() {
kinds.extend(&["normal", "build", "dev"]);
}
/// Parses `--edges` option.
///
/// Returns a tuple of `EdgeKind` map and `no_proc_marco` flag.
fn parse_edge_kinds(
config: &Config,
args: &ArgMatches<'_>,
) -> CargoResult<(HashSet<EdgeKind>, bool)> {
let (kinds, no_proc_macro) = {
let mut no_proc_macro = false;
let mut kinds = args.values_of("edges").map_or_else(
|| Vec::new(),
|es| {
es.flat_map(|e| e.split(','))
.filter(|e| {
no_proc_macro = *e == "no-proc-macro";
!no_proc_macro
})
.collect()
},
);

if args.is_present("no-dev-dependencies") {
config
.shell()
.warn("the --no-dev-dependencies flag has changed to -e=no-dev")?;
kinds.push("no-dev");
}

if kinds.is_empty() {
kinds.extend(&["normal", "build", "dev"]);
}

(kinds, no_proc_macro)
};

let mut result = HashSet::new();
let insert_defaults = |result: &mut HashSet<EdgeKind>| {
Expand All @@ -231,7 +258,7 @@ fn parse_edge_kinds(config: &Config, args: &ArgMatches<'_>) -> CargoResult<HashS
bail!(
"unknown edge kind `{}`, valid values are \
\"normal\", \"build\", \"dev\", \
\"no-normal\", \"no-build\", \"no-dev\", \
\"no-normal\", \"no-build\", \"no-dev\", \"no-proc-macro\", \
\"features\", or \"all\"",
k
)
Expand All @@ -245,12 +272,17 @@ fn parse_edge_kinds(config: &Config, args: &ArgMatches<'_>) -> CargoResult<HashS
"no-dev" => result.remove(&EdgeKind::Dep(DepKind::Development)),
"features" => result.insert(EdgeKind::Feature),
"normal" | "build" | "dev" | "all" => {
bail!("`no-` dependency kinds cannot be mixed with other dependency kinds")
bail!(
"`{}` dependency kind cannot be mixed with \
\"no-normal\", \"no-build\", or \"no-dev\" \
dependency kinds",
kind
)
}
k => return unknown(k),
};
}
return Ok(result);
return Ok((result, no_proc_macro));
}
for kind in &kinds {
match *kind {
Expand All @@ -276,5 +308,5 @@ fn parse_edge_kinds(config: &Config, args: &ArgMatches<'_>) -> CargoResult<HashS
if kinds.len() == 1 && kinds[0] == "features" {
insert_defaults(&mut result);
}
Ok(result)
Ok((result, no_proc_macro))
}
30 changes: 25 additions & 5 deletions src/cargo/ops/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::core::{Package, PackageId, PackageIdSpec, Workspace};
use crate::ops::{self, Packages};
use crate::util::{CargoResult, Config};
use crate::{drop_print, drop_println};
use anyhow::{bail, Context};
use anyhow::Context;
use graph::Graph;
use std::collections::{HashMap, HashSet};
use std::str::FromStr;
Expand Down Expand Up @@ -43,6 +43,8 @@ pub struct TreeOptions {
pub format: String,
/// Includes features in the tree as separate nodes.
pub graph_features: bool,
/// Exculdes proc-macro dependencies.
pub no_proc_macro: bool,
}

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

/// Entry point for the `cargo tree` command.
pub fn build_and_print(ws: &Workspace<'_>, opts: &TreeOptions) -> CargoResult<()> {
if opts.graph_features && opts.duplicates {
bail!("the `-e features` flag does not support `--duplicates`");
}
let requested_targets = match &opts.target {
Target::All | Target::Host => Vec::new(),
Target::Specific(t) => t.clone(),
Expand Down Expand Up @@ -241,6 +240,7 @@ fn print(
symbols,
opts.prefix,
opts.no_dedupe,
opts.no_proc_macro,
&mut visited_deps,
&mut levels_continue,
&mut print_stack,
Expand All @@ -259,6 +259,7 @@ fn print_node<'a>(
symbols: &Symbols,
prefix: Prefix,
no_dedupe: bool,
no_proc_macro: bool,
visited_deps: &mut HashSet<usize>,
levels_continue: &mut Vec<bool>,
print_stack: &mut Vec<usize>,
Expand Down Expand Up @@ -316,6 +317,7 @@ fn print_node<'a>(
symbols,
prefix,
no_dedupe,
no_proc_macro,
visited_deps,
levels_continue,
print_stack,
Expand All @@ -334,6 +336,7 @@ fn print_dependencies<'a>(
symbols: &Symbols,
prefix: Prefix,
no_dedupe: bool,
no_proc_macro: bool,
visited_deps: &mut HashSet<usize>,
levels_continue: &mut Vec<bool>,
print_stack: &mut Vec<usize>,
Expand Down Expand Up @@ -362,7 +365,23 @@ fn print_dependencies<'a>(
}
}

let mut it = deps.iter().peekable();
let mut it = deps
.iter()
.filter(|dep| {
// Filter out proc-macro dependencies.
if no_proc_macro {
match graph.node(**dep) {
&Node::Package { package_id, .. } => {
!graph.package_for_id(package_id).proc_macro()
}
_ => true,
}
} else {
true
}
})
.peekable();

while let Some(dependency) = it.next() {
levels_continue.push(it.peek().is_some());
print_node(
Expand All @@ -373,6 +392,7 @@ fn print_dependencies<'a>(
symbols,
prefix,
no_dedupe,
no_proc_macro,
visited_deps,
levels_continue,
print_stack,
Expand Down
4 changes: 3 additions & 1 deletion src/doc/man/cargo-tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ The dependency kinds to display. Takes a comma separated list of values:
- `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 `no-` prefixed options cannot be mixed with the other dependency kinds.
The `normal`, `build`, `dev`, and `all` dependency kinds cannot be mixed with
`no-normal`, `no-build`, or `no-dev` dependency kinds.

The default is `normal,build,dev`.
{{/option}}
Expand Down
6 changes: 4 additions & 2 deletions src/doc/man/generated_txt/cargo-tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ OPTIONS

o no-dev — Do not include development dependencies.

The no- prefixed options cannot be mixed with the other dependency
kinds.
o no-proc-macro — Do not include procedural macro dependencies.

The normal, build, dev, and all dependency kinds cannot be mixed
with no-normal, no-build, or no-dev dependency kinds.

The default is normal,build,dev.

Expand Down
4 changes: 3 additions & 1 deletion src/doc/src/commands/cargo-tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ kind given, then it will automatically include the other dependency kinds.</li>
<li><code>no-normal</code> — Do not include normal dependencies.</li>
<li><code>no-build</code> — Do not include build dependencies.</li>
<li><code>no-dev</code> — Do not include development dependencies.</li>
<li><code>no-proc-macro</code> — Do not include procedural macro dependencies.</li>
</ul>
<p>The <code>no-</code> prefixed options cannot be mixed with the other dependency kinds.</p>
<p>The <code>normal</code>, <code>build</code>, <code>dev</code>, and <code>all</code> dependency kinds cannot be mixed with
<code>no-normal</code>, <code>no-build</code>, or <code>no-dev</code> dependency kinds.</p>
<p>The default is <code>normal,build,dev</code>.</dd>


Expand Down
7 changes: 6 additions & 1 deletion src/etc/man/cargo-tree.1
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ kind given, then it will automatically include the other dependency kinds.
\h'-04'\(bu\h'+02'\fBno\-dev\fR \[em] Do not include development dependencies.
.RE
.sp
The \fBno\-\fR prefixed options cannot be mixed with the other dependency kinds.
.RS 4
\h'-04'\(bu\h'+02'\fBno\-proc\-macro\fR \[em] Do not include procedural macro dependencies.
.RE
.sp
The \fBnormal\fR, \fBbuild\fR, \fBdev\fR, and \fBall\fR dependency kinds cannot be mixed with
\fBno\-normal\fR, \fBno\-build\fR, or \fBno\-dev\fR dependency kinds.
.sp
The default is \fBnormal,build,dev\fR\&.
.RE
Expand Down
Loading

0 comments on commit 238a9fa

Please sign in to comment.