Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/bin/cargo/commands/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
};
let target = tree::Target::from_cli(targets);

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

let pkgs_to_prune = args._values_of("prune");
Expand Down Expand Up @@ -230,6 +230,7 @@ subtree of the package given to -p.\n\
graph_features,
display_depth,
no_proc_macro,
public,
};

if opts.graph_features && opts.duplicates {
Expand All @@ -246,9 +247,10 @@ subtree of the package given to -p.\n\
fn parse_edge_kinds(
gctx: &GlobalContext,
args: &ArgMatches,
) -> CargoResult<(HashSet<EdgeKind>, bool)> {
let (kinds, no_proc_macro) = {
) -> CargoResult<(HashSet<EdgeKind>, bool, bool)> {
let (kinds, no_proc_macro, public) = {
let mut no_proc_macro = false;
let mut public = false;
let mut kinds = args.get_many::<String>("edges").map_or_else(
|| Vec::new(),
|es| {
Expand All @@ -257,6 +259,9 @@ fn parse_edge_kinds(
if *e == "no-proc-macro" {
no_proc_macro = true;
false
} else if *e == "public" {
public = true;
false
} else {
true
}
Expand All @@ -275,7 +280,11 @@ fn parse_edge_kinds(
kinds.extend(&["normal", "build", "dev"]);
}

(kinds, no_proc_macro)
if public && !gctx.cli_unstable().unstable_options {
anyhow::bail!("`--edges public` requires `-Zunstable-options`");
}

(kinds, no_proc_macro, public)
};

let mut result = HashSet::new();
Expand Down Expand Up @@ -312,7 +321,7 @@ fn parse_edge_kinds(
k => return unknown(k),
};
}
return Ok((result, no_proc_macro));
return Ok((result, no_proc_macro, public));
}
for kind in &kinds {
match *kind {
Expand All @@ -338,5 +347,5 @@ fn parse_edge_kinds(
if kinds.len() == 1 && kinds[0] == "features" {
insert_defaults(&mut result);
}
Ok((result, no_proc_macro))
Ok((result, no_proc_macro, public))
}
4 changes: 4 additions & 0 deletions src/cargo/ops/tree/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,10 @@ fn add_pkg(
if opts.no_proc_macro && graph.package_for_id(dep_id).proc_macro() {
return false;
}
// Filter out private dependencies if requested.
if opts.public && !dep.is_public() {
return false;
}
if dep.is_optional() {
// If the new feature resolver does not enable this
// optional dep, then don't use it.
Expand Down
26 changes: 6 additions & 20 deletions src/cargo/ops/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub struct TreeOptions {
pub display_depth: DisplayDepth,
/// Excludes proc-macro dependencies.
pub no_proc_macro: bool,
/// Include only public dependencies.
pub public: bool,
}

#[derive(PartialEq)]
Expand Down Expand Up @@ -92,7 +94,6 @@ impl FromStr for Prefix {
#[derive(Clone, Copy)]
pub enum DisplayDepth {
MaxDisplayDepth(u32),
Public,
Workspace,
}

Expand All @@ -102,7 +103,6 @@ impl FromStr for DisplayDepth {
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"workspace" => Ok(Self::Workspace),
"public" => Ok(Self::Public),
s => s.parse().map(Self::MaxDisplayDepth).map_err(|_| {
clap::Error::raw(
clap::error::ErrorKind::ValueValidation,
Expand Down Expand Up @@ -426,15 +426,9 @@ fn print_dependencies<'a>(
}
}

let (max_display_depth, filter_non_workspace_member, filter_private) = match display_depth {
DisplayDepth::MaxDisplayDepth(max) => (max, false, false),
DisplayDepth::Workspace => (u32::MAX, true, false),
DisplayDepth::Public => {
if !ws.gctx().cli_unstable().unstable_options {
anyhow::bail!("`--depth public` requires `-Zunstable-options`")
}
(u32::MAX, false, true)
}
let (max_display_depth, filter_non_workspace_member) = match display_depth {
DisplayDepth::MaxDisplayDepth(max) => (max, false),
DisplayDepth::Workspace => (u32::MAX, true),
};

// Current level exceeds maximum display depth. Skip.
Expand All @@ -451,17 +445,9 @@ fn print_dependencies<'a>(
if filter_non_workspace_member && !ws.is_member_id(*package_id) {
return false;
}
if filter_private && !dep.public() {
return false;
}
!pkgs_to_prune.iter().any(|spec| spec.matches(*package_id))
}
Node::Feature { .. } => {
if filter_private && !dep.public() {
return false;
}
true
}
Node::Feature { .. } => true,
}
})
.peekable();
Expand Down
30 changes: 15 additions & 15 deletions tests/testsuite/cargo_tree/deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1903,7 +1903,7 @@ c v0.1.0 ([ROOT]/foo/c) (*)
}

#[cargo_test(nightly, reason = "exported_private_dependencies lint is unstable")]
fn depth_public() {
fn edge_public() {
let p = project()
.file(
"Cargo.toml",
Expand Down Expand Up @@ -1964,37 +1964,37 @@ fn depth_public() {
.file("dep/src/lib.rs", "")
.build();

p.cargo("tree --depth public")
.masquerade_as_nightly_cargo(&["public-dependency", "depth-public"])
p.cargo("tree --edges public")
.masquerade_as_nightly_cargo(&["public-dependency", "edge-public"])
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] `--depth public` requires `-Zunstable-options`
[ERROR] `--edges public` requires `-Zunstable-options`

"#]])
.run();

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

"#]])
.run();

p.cargo("tree --depth public -p right-priv")
p.cargo("tree --edges public -p right-priv")
.arg("-Zunstable-options")
.masquerade_as_nightly_cargo(&["public-dependency", "depth-public"])
.masquerade_as_nightly_cargo(&["public-dependency", "edge-public"])
.with_stdout_data(str![[r#"
right-priv v0.1.0 ([ROOT]/foo/right-priv)

"#]])
.run();

p.cargo("tree --depth public -p diamond")
p.cargo("tree --edges public -p diamond")
.arg("-Zunstable-options")
.masquerade_as_nightly_cargo(&["public-dependency", "depth-public"])
.masquerade_as_nightly_cargo(&["public-dependency", "edge-public"])
.with_stdout_data(str![[r#"
diamond v0.1.0 ([ROOT]/foo/diamond)
├── left-pub v0.1.0 ([ROOT]/foo/left-pub)
Expand All @@ -2004,9 +2004,9 @@ diamond v0.1.0 ([ROOT]/foo/diamond)
"#]])
.run();

p.cargo("tree --depth public")
p.cargo("tree --edges public")
.arg("-Zunstable-options")
.masquerade_as_nightly_cargo(&["public-dependency", "depth-public"])
.masquerade_as_nightly_cargo(&["public-dependency", "edge-public"])
.with_stdout_data(str![[r#"
dep v0.1.0 ([ROOT]/foo/dep)

Expand All @@ -2017,14 +2017,14 @@ diamond v0.1.0 ([ROOT]/foo/diamond)

left-pub v0.1.0 ([ROOT]/foo/left-pub) (*)

right-priv v0.1.0 ([ROOT]/foo/right-priv) (*)
right-priv v0.1.0 ([ROOT]/foo/right-priv)

"#]])
.run();

p.cargo("tree --depth public --invert dep")
p.cargo("tree --edges public --invert dep")
.arg("-Zunstable-options")
.masquerade_as_nightly_cargo(&["public-dependency", "depth-public"])
.masquerade_as_nightly_cargo(&["public-dependency", "edge-public"])
.with_stdout_data(str![[r#"
dep v0.1.0 ([ROOT]/foo/dep)
└── left-pub v0.1.0 ([ROOT]/foo/left-pub)
Expand Down
32 changes: 16 additions & 16 deletions tests/testsuite/cargo_tree/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ foo v0.1.0 ([ROOT]/foo)
}

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

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

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

#[cargo_test(nightly, reason = "exported_private_dependencies lint is unstable")]
fn depth_public_transitive_features() {
fn edge_public_transitive_features() {
Package::new("pub-defaultdep", "1.0.0")
.feature("default", &["f1"])
.feature("f1", &["f2"])
Expand Down Expand Up @@ -423,19 +423,19 @@ fn depth_public_transitive_features() {
.file("src/lib.rs", "")
.build();

p.cargo("tree -e features --depth public")
p.cargo("tree -e features --edges public")
.arg("-Zunstable-options")
.masquerade_as_nightly_cargo(&["public-dependency", "depth-public"])
.masquerade_as_nightly_cargo(&["public-dependency", "edge-public"])
.with_stdout_data(str![[r#"
foo v0.1.0 ([ROOT]/foo)
├── priv-defaultdep feature "default"
│ ├── priv-defaultdep v1.0.0
│ └── priv-defaultdep feature "f1"
│ ├── priv-defaultdep v1.0.0 (*)
│ ├── priv-defaultdep v1.0.0
│ └── priv-defaultdep feature "f2"
│ ├── priv-defaultdep v1.0.0 (*)
│ ├── priv-defaultdep v1.0.0
│ └── priv-defaultdep feature "optdep"
│ └── priv-defaultdep v1.0.0 (*)
│ └── priv-defaultdep v1.0.0
└── pub-defaultdep feature "default"
├── pub-defaultdep v1.0.0
│ └── optdep feature "default"
Expand All @@ -454,7 +454,7 @@ foo v0.1.0 ([ROOT]/foo)
}

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

Expand Down Expand Up @@ -482,18 +482,18 @@ fn depth_public_cli() {
.file("src/lib.rs", "")
.build();

p.cargo("tree -e features --depth public")
p.cargo("tree -e features --edges public")
.arg("-Zunstable-options")
.masquerade_as_nightly_cargo(&["public-dependency", "depth-public"])
.masquerade_as_nightly_cargo(&["public-dependency", "edge-public"])
.with_stdout_data(str![[r#"
foo v0.1.0 ([ROOT]/foo)

"#]])
.run();

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

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

Expand Down