From a4f89eab03e49512f62272cd03b91b38da6110d7 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Tue, 15 Nov 2022 09:41:39 +0800 Subject: [PATCH 1/2] Add warning when `cargo tree -i ` can not find packages Signed-off-by: hi-rustin --- src/cargo/ops/tree/mod.rs | 8 +++++- tests/testsuite/tree.rs | 56 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/cargo/ops/tree/mod.rs b/src/cargo/ops/tree/mod.rs index 134670cf988..5097f3e46e7 100644 --- a/src/cargo/ops/tree/mod.rs +++ b/src/cargo/ops/tree/mod.rs @@ -213,7 +213,13 @@ pub fn build_and_print(ws: &Workspace<'_>, opts: &TreeOptions) -> CargoResult<() }) .collect::>>()?; - print(ws.config(), opts, root_indexes, &pkgs_to_prune, &graph)?; + if root_indexes.len() == 0 { + ws.config().shell().warn("nothing to print.\n\n\ + To find dependencies that require specific features or target platforms, \ + try use options `--all-features` or `--target all` first, and then narrow your search scope accordingly.")?; + } else { + print(ws.config(), opts, root_indexes, &pkgs_to_prune, &graph)?; + } Ok(()) } diff --git a/tests/testsuite/tree.rs b/tests/testsuite/tree.rs index d053c473176..d6002db905c 100644 --- a/tests/testsuite/tree.rs +++ b/tests/testsuite/tree.rs @@ -489,6 +489,62 @@ foo v0.1.0 ([..]/foo) .run(); } +#[cargo_test] +fn no_selected_target_dependency() { + // --target flag + if cross_compile::disabled() { + return; + } + Package::new("targetdep", "1.0.0").publish(); + + let p = project() + .file( + "Cargo.toml", + &format!( + r#" + [package] + name = "foo" + version = "0.1.0" + + [target.'{alt}'.dependencies] + targetdep = "1.0" + + "#, + alt = alternate(), + ), + ) + .file("src/lib.rs", "") + .file("build.rs", "fn main() {}") + .build(); + + p.cargo("tree") + .with_stdout( + "\ +foo v0.1.0 ([..]/foo) +", + ) + .run(); + + p.cargo("tree -i targetdep") + .with_stderr( + "\ +[WARNING] nothing to print. + +To find dependencies that require specific features or target platforms, \ +try use options `--all-features` or `--target all` first, and then narrow your search scope accordingly. +", + ) + .run(); + p.cargo("tree -i targetdep --target all") + .with_stdout( + "\ +targetdep v1.0.0 +└── foo v0.1.0 ([..]/foo) +", + ) + .run(); +} + #[cargo_test] fn dep_kinds() { Package::new("inner-devdep", "1.0.0").publish(); From c5eb61823cf22bc5735b93e3e0717e44a0bede17 Mon Sep 17 00:00:00 2001 From: hi-rustin Date: Thu, 17 Nov 2022 08:59:08 +0800 Subject: [PATCH 2/2] Remove the `--all-features` tip Signed-off-by: hi-rustin --- src/cargo/ops/tree/mod.rs | 8 +++++--- tests/testsuite/tree.rs | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/cargo/ops/tree/mod.rs b/src/cargo/ops/tree/mod.rs index 5097f3e46e7..14c5509b313 100644 --- a/src/cargo/ops/tree/mod.rs +++ b/src/cargo/ops/tree/mod.rs @@ -214,9 +214,11 @@ pub fn build_and_print(ws: &Workspace<'_>, opts: &TreeOptions) -> CargoResult<() .collect::>>()?; if root_indexes.len() == 0 { - ws.config().shell().warn("nothing to print.\n\n\ - To find dependencies that require specific features or target platforms, \ - try use options `--all-features` or `--target all` first, and then narrow your search scope accordingly.")?; + ws.config().shell().warn( + "nothing to print.\n\n\ + To find dependencies that require specific target platforms, \ + try use option `--target all` first, and then narrow your search scope accordingly.", + )?; } else { print(ws.config(), opts, root_indexes, &pkgs_to_prune, &graph)?; } diff --git a/tests/testsuite/tree.rs b/tests/testsuite/tree.rs index d6002db905c..4d3aee4fcb9 100644 --- a/tests/testsuite/tree.rs +++ b/tests/testsuite/tree.rs @@ -530,8 +530,8 @@ foo v0.1.0 ([..]/foo) "\ [WARNING] nothing to print. -To find dependencies that require specific features or target platforms, \ -try use options `--all-features` or `--target all` first, and then narrow your search scope accordingly. +To find dependencies that require specific target platforms, \ +try use option `--target all` first, and then narrow your search scope accordingly. ", ) .run();