Skip to content

Commit 63fdd75

Browse files
committed
Auto merge of #11377 - hi-rustin:rustin-patch-warning-tree, r=weihanglo
Add warning when `cargo tree -i <spec>` can not find packages ### What does this PR try to resolve? close #11315 Add warning when `cargo tree -i <spec>` can not find packages. ### How should we test and review this PR? Please run the unit test.
2 parents 43689d3 + c5eb618 commit 63fdd75

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/cargo/ops/tree/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,15 @@ pub fn build_and_print(ws: &Workspace<'_>, opts: &TreeOptions) -> CargoResult<()
213213
})
214214
.collect::<CargoResult<Vec<PackageIdSpec>>>()?;
215215

216-
print(ws.config(), opts, root_indexes, &pkgs_to_prune, &graph)?;
216+
if root_indexes.len() == 0 {
217+
ws.config().shell().warn(
218+
"nothing to print.\n\n\
219+
To find dependencies that require specific target platforms, \
220+
try use option `--target all` first, and then narrow your search scope accordingly.",
221+
)?;
222+
} else {
223+
print(ws.config(), opts, root_indexes, &pkgs_to_prune, &graph)?;
224+
}
217225
Ok(())
218226
}
219227

tests/testsuite/tree.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,62 @@ foo v0.1.0 ([..]/foo)
489489
.run();
490490
}
491491

492+
#[cargo_test]
493+
fn no_selected_target_dependency() {
494+
// --target flag
495+
if cross_compile::disabled() {
496+
return;
497+
}
498+
Package::new("targetdep", "1.0.0").publish();
499+
500+
let p = project()
501+
.file(
502+
"Cargo.toml",
503+
&format!(
504+
r#"
505+
[package]
506+
name = "foo"
507+
version = "0.1.0"
508+
509+
[target.'{alt}'.dependencies]
510+
targetdep = "1.0"
511+
512+
"#,
513+
alt = alternate(),
514+
),
515+
)
516+
.file("src/lib.rs", "")
517+
.file("build.rs", "fn main() {}")
518+
.build();
519+
520+
p.cargo("tree")
521+
.with_stdout(
522+
"\
523+
foo v0.1.0 ([..]/foo)
524+
",
525+
)
526+
.run();
527+
528+
p.cargo("tree -i targetdep")
529+
.with_stderr(
530+
"\
531+
[WARNING] nothing to print.
532+
533+
To find dependencies that require specific target platforms, \
534+
try use option `--target all` first, and then narrow your search scope accordingly.
535+
",
536+
)
537+
.run();
538+
p.cargo("tree -i targetdep --target all")
539+
.with_stdout(
540+
"\
541+
targetdep v1.0.0
542+
└── foo v0.1.0 ([..]/foo)
543+
",
544+
)
545+
.run();
546+
}
547+
492548
#[cargo_test]
493549
fn dep_kinds() {
494550
Package::new("inner-devdep", "1.0.0").publish();

0 commit comments

Comments
 (0)