Skip to content

Commit a4f89ea

Browse files
committed
Add warning when cargo tree -i <spec> can not find packages
Signed-off-by: hi-rustin <[email protected]>
1 parent 16b0978 commit a4f89ea

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

src/cargo/ops/tree/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,13 @@ 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("nothing to print.\n\n\
218+
To find dependencies that require specific features or target platforms, \
219+
try use options `--all-features` or `--target all` first, and then narrow your search scope accordingly.")?;
220+
} else {
221+
print(ws.config(), opts, root_indexes, &pkgs_to_prune, &graph)?;
222+
}
217223
Ok(())
218224
}
219225

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 features or target platforms, \
534+
try use options `--all-features` or `--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)