From 747a5924a09451007e5cbfe3628b5368354156d5 Mon Sep 17 00:00:00 2001 From: Jordan Rose Date: Thu, 27 Apr 2023 10:49:22 -0700 Subject: [PATCH 1/2] test: Add a test for `cargo tree --duplicates` with a proc-macro --- tests/testsuite/tree.rs | 60 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/tests/testsuite/tree.rs b/tests/testsuite/tree.rs index c3c1ca6d309..cdd9f1e2982 100644 --- a/tests/testsuite/tree.rs +++ b/tests/testsuite/tree.rs @@ -1085,6 +1085,66 @@ fn duplicates_with_target() { p.cargo("tree -d --target=all").with_stdout("").run(); } +#[cargo_test] +fn duplicates_with_proc_macro() { + Package::new("dupe-dep", "1.0.0").publish(); + Package::new("dupe-dep", "2.0.0").publish(); + Package::new("proc", "1.0.0") + .proc_macro(true) + .dep("dupe-dep", "1.0") + .publish(); + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + + [dependencies] + proc = "1.0" + dupe-dep = "2.0" + "#, + ) + .file("src/lib.rs", "") + .build(); + + p.cargo("tree") + .with_stdout( + "\ +foo v0.1.0 ([..]/foo) +├── dupe-dep v2.0.0 +└── proc v1.0.0 (proc-macro) + └── dupe-dep v1.0.0 +", + ) + .run(); + + p.cargo("tree --duplicates") + .with_stdout( + "\ +dupe-dep v1.0.0 +└── proc v1.0.0 (proc-macro) + └── foo v0.1.0 ([..]/foo) + +dupe-dep v2.0.0 +└── foo v0.1.0 ([..]/foo) +", + ) + .run(); + + p.cargo("tree --duplicates --edges no-proc-macro") + .with_stdout( + "\ +dupe-dep v1.0.0 + +dupe-dep v2.0.0 +└── foo v0.1.0 ([..]/foo) +", + ) + .run(); +} + #[cargo_test] fn charset() { let p = make_simple_proj(); From a0576d176756cd0c10c330a6fc7d34a0cd3b323d Mon Sep 17 00:00:00 2001 From: Jordan Rose Date: Thu, 27 Apr 2023 10:54:51 -0700 Subject: [PATCH 2/2] cargo-tree: Handle -e no-proc-macro when building the graph This is closer to how the edge-based filters work ([no-]build etc.), and results in a more useful behavior when combined with -i or -d. --- src/cargo/ops/tree/graph.rs | 4 ++++ src/cargo/ops/tree/mod.rs | 18 ------------------ tests/testsuite/tree.rs | 11 +---------- 3 files changed, 5 insertions(+), 28 deletions(-) diff --git a/src/cargo/ops/tree/graph.rs b/src/cargo/ops/tree/graph.rs index 20a9ca0b657..d01d07f1a1a 100644 --- a/src/cargo/ops/tree/graph.rs +++ b/src/cargo/ops/tree/graph.rs @@ -362,6 +362,10 @@ fn add_pkg( if !opts.edge_kinds.contains(&EdgeKind::Dep(dep.kind())) { return false; } + // Filter out proc-macrcos if requested. + if opts.no_proc_macro && graph.package_for_id(dep_id).proc_macro() { + return false; + } if dep.is_optional() { // If the new feature resolver does not enable this // optional dep, then don't use it. diff --git a/src/cargo/ops/tree/mod.rs b/src/cargo/ops/tree/mod.rs index 02459f78f43..f397b95af6f 100644 --- a/src/cargo/ops/tree/mod.rs +++ b/src/cargo/ops/tree/mod.rs @@ -267,7 +267,6 @@ fn print( opts.prefix, opts.no_dedupe, opts.max_display_depth, - opts.no_proc_macro, &mut visited_deps, &mut levels_continue, &mut print_stack, @@ -288,7 +287,6 @@ fn print_node<'a>( prefix: Prefix, no_dedupe: bool, max_display_depth: u32, - no_proc_macro: bool, visited_deps: &mut HashSet, levels_continue: &mut Vec, print_stack: &mut Vec, @@ -348,7 +346,6 @@ fn print_node<'a>( prefix, no_dedupe, max_display_depth, - no_proc_macro, visited_deps, levels_continue, print_stack, @@ -369,7 +366,6 @@ fn print_dependencies<'a>( prefix: Prefix, no_dedupe: bool, max_display_depth: u32, - no_proc_macro: bool, visited_deps: &mut HashSet, levels_continue: &mut Vec, print_stack: &mut Vec, @@ -405,19 +401,6 @@ fn print_dependencies<'a>( 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 - } - }) .filter(|dep| { // Filter out packages to prune. match graph.node(**dep) { @@ -441,7 +424,6 @@ fn print_dependencies<'a>( prefix, no_dedupe, max_display_depth, - no_proc_macro, visited_deps, levels_continue, print_stack, diff --git a/tests/testsuite/tree.rs b/tests/testsuite/tree.rs index cdd9f1e2982..e2e74c4f954 100644 --- a/tests/testsuite/tree.rs +++ b/tests/testsuite/tree.rs @@ -1134,14 +1134,7 @@ dupe-dep v2.0.0 .run(); p.cargo("tree --duplicates --edges no-proc-macro") - .with_stdout( - "\ -dupe-dep v1.0.0 - -dupe-dep v2.0.0 -└── foo v0.1.0 ([..]/foo) -", - ) + .with_stdout("") .run(); } @@ -1600,8 +1593,6 @@ somedep v1.0.0 "\ somedep v1.0.0 └── foo v0.1.0 ([..]/foo) - -somedep v1.0.0 ", ) .run();