Skip to content

Commit a390202

Browse files
authored
fix(tree): Switch from --depth public to --edges public (#16081)
### What does this PR try to resolve? `public` is describing the edges, rather than the nodes, unlike `workspace`, and is a better fit for `--edges`. It's like `no-proc-macro` in being a filter over other edge kinds. See also #6129 (comment) Note that this drops some `(*)`. That gets added if a node is seen twice and there are dependencies. The "are there dependencies" check happens after filtering for `--edges` but before filtering for `--depth`, causing inconsistent behavior. ### How to test and review this PR?
2 parents d6b7737 + 9f432ed commit a390202

File tree

5 files changed

+56
-57
lines changed

5 files changed

+56
-57
lines changed

src/bin/cargo/commands/tree.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
157157
};
158158
let target = tree::Target::from_cli(targets);
159159

160-
let (edge_kinds, no_proc_macro) = parse_edge_kinds(gctx, args)?;
160+
let (edge_kinds, no_proc_macro, public) = parse_edge_kinds(gctx, args)?;
161161
let graph_features = edge_kinds.contains(&EdgeKind::Feature);
162162

163163
let pkgs_to_prune = args._values_of("prune");
@@ -230,6 +230,7 @@ subtree of the package given to -p.\n\
230230
graph_features,
231231
display_depth,
232232
no_proc_macro,
233+
public,
233234
};
234235

235236
if opts.graph_features && opts.duplicates {
@@ -246,9 +247,10 @@ subtree of the package given to -p.\n\
246247
fn parse_edge_kinds(
247248
gctx: &GlobalContext,
248249
args: &ArgMatches,
249-
) -> CargoResult<(HashSet<EdgeKind>, bool)> {
250-
let (kinds, no_proc_macro) = {
250+
) -> CargoResult<(HashSet<EdgeKind>, bool, bool)> {
251+
let (kinds, no_proc_macro, public) = {
251252
let mut no_proc_macro = false;
253+
let mut public = false;
252254
let mut kinds = args.get_many::<String>("edges").map_or_else(
253255
|| Vec::new(),
254256
|es| {
@@ -257,6 +259,9 @@ fn parse_edge_kinds(
257259
if *e == "no-proc-macro" {
258260
no_proc_macro = true;
259261
false
262+
} else if *e == "public" {
263+
public = true;
264+
false
260265
} else {
261266
true
262267
}
@@ -275,7 +280,11 @@ fn parse_edge_kinds(
275280
kinds.extend(&["normal", "build", "dev"]);
276281
}
277282

278-
(kinds, no_proc_macro)
283+
if public && !gctx.cli_unstable().unstable_options {
284+
anyhow::bail!("`--edges public` requires `-Zunstable-options`");
285+
}
286+
287+
(kinds, no_proc_macro, public)
279288
};
280289

281290
let mut result = HashSet::new();
@@ -312,7 +321,7 @@ fn parse_edge_kinds(
312321
k => return unknown(k),
313322
};
314323
}
315-
return Ok((result, no_proc_macro));
324+
return Ok((result, no_proc_macro, public));
316325
}
317326
for kind in &kinds {
318327
match *kind {
@@ -338,5 +347,5 @@ fn parse_edge_kinds(
338347
if kinds.len() == 1 && kinds[0] == "features" {
339348
insert_defaults(&mut result);
340349
}
341-
Ok((result, no_proc_macro))
350+
Ok((result, no_proc_macro, public))
342351
}

src/cargo/ops/tree/graph.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,10 @@ fn add_pkg(
457457
if opts.no_proc_macro && graph.package_for_id(dep_id).proc_macro() {
458458
return false;
459459
}
460+
// Filter out private dependencies if requested.
461+
if opts.public && !dep.is_public() {
462+
return false;
463+
}
460464
if dep.is_optional() {
461465
// If the new feature resolver does not enable this
462466
// optional dep, then don't use it.

src/cargo/ops/tree/mod.rs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ pub struct TreeOptions {
5050
pub display_depth: DisplayDepth,
5151
/// Excludes proc-macro dependencies.
5252
pub no_proc_macro: bool,
53+
/// Include only public dependencies.
54+
pub public: bool,
5355
}
5456

5557
#[derive(PartialEq)]
@@ -92,7 +94,6 @@ impl FromStr for Prefix {
9294
#[derive(Clone, Copy)]
9395
pub enum DisplayDepth {
9496
MaxDisplayDepth(u32),
95-
Public,
9697
Workspace,
9798
}
9899

@@ -102,7 +103,6 @@ impl FromStr for DisplayDepth {
102103
fn from_str(s: &str) -> Result<Self, Self::Err> {
103104
match s {
104105
"workspace" => Ok(Self::Workspace),
105-
"public" => Ok(Self::Public),
106106
s => s.parse().map(Self::MaxDisplayDepth).map_err(|_| {
107107
clap::Error::raw(
108108
clap::error::ErrorKind::ValueValidation,
@@ -426,15 +426,9 @@ fn print_dependencies<'a>(
426426
}
427427
}
428428

429-
let (max_display_depth, filter_non_workspace_member, filter_private) = match display_depth {
430-
DisplayDepth::MaxDisplayDepth(max) => (max, false, false),
431-
DisplayDepth::Workspace => (u32::MAX, true, false),
432-
DisplayDepth::Public => {
433-
if !ws.gctx().cli_unstable().unstable_options {
434-
anyhow::bail!("`--depth public` requires `-Zunstable-options`")
435-
}
436-
(u32::MAX, false, true)
437-
}
429+
let (max_display_depth, filter_non_workspace_member) = match display_depth {
430+
DisplayDepth::MaxDisplayDepth(max) => (max, false),
431+
DisplayDepth::Workspace => (u32::MAX, true),
438432
};
439433

440434
// Current level exceeds maximum display depth. Skip.
@@ -451,17 +445,9 @@ fn print_dependencies<'a>(
451445
if filter_non_workspace_member && !ws.is_member_id(*package_id) {
452446
return false;
453447
}
454-
if filter_private && !dep.public() {
455-
return false;
456-
}
457448
!pkgs_to_prune.iter().any(|spec| spec.matches(*package_id))
458449
}
459-
Node::Feature { .. } => {
460-
if filter_private && !dep.public() {
461-
return false;
462-
}
463-
true
464-
}
450+
Node::Feature { .. } => true,
465451
}
466452
})
467453
.peekable();

tests/testsuite/cargo_tree/deps.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,7 +1903,7 @@ c v0.1.0 ([ROOT]/foo/c) (*)
19031903
}
19041904

19051905
#[cargo_test(nightly, reason = "exported_private_dependencies lint is unstable")]
1906-
fn depth_public() {
1906+
fn edge_public() {
19071907
let p = project()
19081908
.file(
19091909
"Cargo.toml",
@@ -1964,37 +1964,37 @@ fn depth_public() {
19641964
.file("dep/src/lib.rs", "")
19651965
.build();
19661966

1967-
p.cargo("tree --depth public")
1968-
.masquerade_as_nightly_cargo(&["public-dependency", "depth-public"])
1967+
p.cargo("tree --edges public")
1968+
.masquerade_as_nightly_cargo(&["public-dependency", "edge-public"])
19691969
.with_status(101)
19701970
.with_stderr_data(str![[r#"
1971-
[ERROR] `--depth public` requires `-Zunstable-options`
1971+
[ERROR] `--edges public` requires `-Zunstable-options`
19721972
19731973
"#]])
19741974
.run();
19751975

1976-
p.cargo("tree --depth public -p left-pub")
1976+
p.cargo("tree --edges public -p left-pub")
19771977
.arg("-Zunstable-options")
1978-
.masquerade_as_nightly_cargo(&["public-dependency", "depth-public"])
1978+
.masquerade_as_nightly_cargo(&["public-dependency", "edge-public"])
19791979
.with_stdout_data(str![[r#"
19801980
left-pub v0.1.0 ([ROOT]/foo/left-pub)
19811981
└── dep v0.1.0 ([ROOT]/foo/dep)
19821982
19831983
"#]])
19841984
.run();
19851985

1986-
p.cargo("tree --depth public -p right-priv")
1986+
p.cargo("tree --edges public -p right-priv")
19871987
.arg("-Zunstable-options")
1988-
.masquerade_as_nightly_cargo(&["public-dependency", "depth-public"])
1988+
.masquerade_as_nightly_cargo(&["public-dependency", "edge-public"])
19891989
.with_stdout_data(str![[r#"
19901990
right-priv v0.1.0 ([ROOT]/foo/right-priv)
19911991
19921992
"#]])
19931993
.run();
19941994

1995-
p.cargo("tree --depth public -p diamond")
1995+
p.cargo("tree --edges public -p diamond")
19961996
.arg("-Zunstable-options")
1997-
.masquerade_as_nightly_cargo(&["public-dependency", "depth-public"])
1997+
.masquerade_as_nightly_cargo(&["public-dependency", "edge-public"])
19981998
.with_stdout_data(str![[r#"
19991999
diamond v0.1.0 ([ROOT]/foo/diamond)
20002000
├── left-pub v0.1.0 ([ROOT]/foo/left-pub)
@@ -2004,9 +2004,9 @@ diamond v0.1.0 ([ROOT]/foo/diamond)
20042004
"#]])
20052005
.run();
20062006

2007-
p.cargo("tree --depth public")
2007+
p.cargo("tree --edges public")
20082008
.arg("-Zunstable-options")
2009-
.masquerade_as_nightly_cargo(&["public-dependency", "depth-public"])
2009+
.masquerade_as_nightly_cargo(&["public-dependency", "edge-public"])
20102010
.with_stdout_data(str![[r#"
20112011
dep v0.1.0 ([ROOT]/foo/dep)
20122012
@@ -2017,14 +2017,14 @@ diamond v0.1.0 ([ROOT]/foo/diamond)
20172017
20182018
left-pub v0.1.0 ([ROOT]/foo/left-pub) (*)
20192019
2020-
right-priv v0.1.0 ([ROOT]/foo/right-priv) (*)
2020+
right-priv v0.1.0 ([ROOT]/foo/right-priv)
20212021
20222022
"#]])
20232023
.run();
20242024

2025-
p.cargo("tree --depth public --invert dep")
2025+
p.cargo("tree --edges public --invert dep")
20262026
.arg("-Zunstable-options")
2027-
.masquerade_as_nightly_cargo(&["public-dependency", "depth-public"])
2027+
.masquerade_as_nightly_cargo(&["public-dependency", "edge-public"])
20282028
.with_stdout_data(str![[r#"
20292029
dep v0.1.0 ([ROOT]/foo/dep)
20302030
└── left-pub v0.1.0 ([ROOT]/foo/left-pub)

tests/testsuite/cargo_tree/features.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ foo v0.1.0 ([ROOT]/foo)
352352
}
353353

354354
#[cargo_test(nightly, reason = "exported_private_dependencies lint is unstable")]
355-
fn depth_public_no_features() {
355+
fn edge_public_no_features() {
356356
Package::new("pub-defaultdep", "1.0.0").publish();
357357
Package::new("priv-defaultdep", "1.0.0").publish();
358358

@@ -374,9 +374,9 @@ fn depth_public_no_features() {
374374
.file("src/lib.rs", "")
375375
.build();
376376

377-
p.cargo("tree -e features --depth public")
377+
p.cargo("tree -e features --edges public")
378378
.arg("-Zunstable-options")
379-
.masquerade_as_nightly_cargo(&["public-dependency", "depth-public"])
379+
.masquerade_as_nightly_cargo(&["public-dependency", "edge-public"])
380380
.with_stdout_data(str![[r#"
381381
foo v0.1.0 ([ROOT]/foo)
382382
└── pub-defaultdep feature "default"
@@ -387,7 +387,7 @@ foo v0.1.0 ([ROOT]/foo)
387387
}
388388

389389
#[cargo_test(nightly, reason = "exported_private_dependencies lint is unstable")]
390-
fn depth_public_transitive_features() {
390+
fn edge_public_transitive_features() {
391391
Package::new("pub-defaultdep", "1.0.0")
392392
.feature("default", &["f1"])
393393
.feature("f1", &["f2"])
@@ -423,19 +423,19 @@ fn depth_public_transitive_features() {
423423
.file("src/lib.rs", "")
424424
.build();
425425

426-
p.cargo("tree -e features --depth public")
426+
p.cargo("tree -e features --edges public")
427427
.arg("-Zunstable-options")
428-
.masquerade_as_nightly_cargo(&["public-dependency", "depth-public"])
428+
.masquerade_as_nightly_cargo(&["public-dependency", "edge-public"])
429429
.with_stdout_data(str![[r#"
430430
foo v0.1.0 ([ROOT]/foo)
431431
├── priv-defaultdep feature "default"
432432
│ ├── priv-defaultdep v1.0.0
433433
│ └── priv-defaultdep feature "f1"
434-
│ ├── priv-defaultdep v1.0.0 (*)
434+
│ ├── priv-defaultdep v1.0.0
435435
│ └── priv-defaultdep feature "f2"
436-
│ ├── priv-defaultdep v1.0.0 (*)
436+
│ ├── priv-defaultdep v1.0.0
437437
│ └── priv-defaultdep feature "optdep"
438-
│ └── priv-defaultdep v1.0.0 (*)
438+
│ └── priv-defaultdep v1.0.0
439439
└── pub-defaultdep feature "default"
440440
├── pub-defaultdep v1.0.0
441441
│ └── optdep feature "default"
@@ -454,7 +454,7 @@ foo v0.1.0 ([ROOT]/foo)
454454
}
455455

456456
#[cargo_test(nightly, reason = "exported_private_dependencies lint is unstable")]
457-
fn depth_public_cli() {
457+
fn edge_public_cli() {
458458
Package::new("priv", "1.0.0").feature("f", &[]).publish();
459459
Package::new("pub", "1.0.0").feature("f", &[]).publish();
460460

@@ -482,18 +482,18 @@ fn depth_public_cli() {
482482
.file("src/lib.rs", "")
483483
.build();
484484

485-
p.cargo("tree -e features --depth public")
485+
p.cargo("tree -e features --edges public")
486486
.arg("-Zunstable-options")
487-
.masquerade_as_nightly_cargo(&["public-dependency", "depth-public"])
487+
.masquerade_as_nightly_cargo(&["public-dependency", "edge-public"])
488488
.with_stdout_data(str![[r#"
489489
foo v0.1.0 ([ROOT]/foo)
490490
491491
"#]])
492492
.run();
493493

494-
p.cargo("tree -e features --depth public --features pub-indirect")
494+
p.cargo("tree -e features --edges public --features pub-indirect")
495495
.arg("-Zunstable-options")
496-
.masquerade_as_nightly_cargo(&["public-dependency", "depth-public"])
496+
.masquerade_as_nightly_cargo(&["public-dependency", "edge-public"])
497497
.with_stdout_data(str![[r#"
498498
foo v0.1.0 ([ROOT]/foo)
499499
└── pub feature "default"
@@ -502,9 +502,9 @@ foo v0.1.0 ([ROOT]/foo)
502502
"#]])
503503
.run();
504504

505-
p.cargo("tree -e features --depth public --features priv-indirect")
505+
p.cargo("tree -e features --edges public --features priv-indirect")
506506
.arg("-Zunstable-options")
507-
.masquerade_as_nightly_cargo(&["public-dependency", "depth-public"])
507+
.masquerade_as_nightly_cargo(&["public-dependency", "edge-public"])
508508
.with_stdout_data(str![[r#"
509509
foo v0.1.0 ([ROOT]/foo)
510510

0 commit comments

Comments
 (0)