Skip to content

Commit

Permalink
feat(workspace): Printing package name when listing available targets
Browse files Browse the repository at this point in the history
  • Loading branch information
linyihai committed Feb 20, 2025
1 parent 01c9df9 commit 7389aff
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 45 deletions.
33 changes: 23 additions & 10 deletions src/cargo/util/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,41 @@ use crate::util::CargoResult;
use anyhow::bail;
use cargo_util::paths::normalize_path;
use cargo_util::ProcessBuilder;
use std::collections::BTreeMap;
use std::fmt::Write;
use std::path::PathBuf;

fn get_available_targets<'a>(
filter_fn: fn(&Target) -> bool,
ws: &'a Workspace<'_>,
options: &'a CompileOptions,
) -> CargoResult<Vec<&'a str>> {
) -> CargoResult<BTreeMap<String, Vec<String>>> {
let packages = if matches!(options.spec, Packages::Default) {
Packages::All(Vec::new()).get_packages(ws)?
} else {
options.spec.get_packages(ws)?
};

let mut targets: Vec<_> = packages
let targets: BTreeMap<String, Vec<String>> = packages
.into_iter()
.flat_map(|pkg| {
pkg.manifest()
.filter_map(|pkg| {
let mut targets = pkg
.manifest()
.targets()
.iter()
.filter(|target| filter_fn(target))
.map(|t| t.name().to_owned())
.collect::<Vec<String>>();

if targets.is_empty() {
return None;
}
targets.sort();

Some((pkg.name().to_string(), targets))
})
.map(Target::name)
.collect();

targets.sort();

Ok(targets)
}

Expand All @@ -52,9 +60,14 @@ fn print_available_targets(
if targets.is_empty() {
writeln!(output, "No {} available.", plural_name)?;
} else {
writeln!(output, "Available {}:", plural_name)?;
for target in targets {
writeln!(output, " {}", target)?;
for (package, targets) in targets {
if targets.is_empty() {
continue;
}
writeln!(output, "Available {} in {}:", plural_name, package)?;
for target in targets {
writeln!(output, " {}", target)?;
}
}
}
bail!("{}", output)
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ fn install_relative_path_outside_current_ws() {
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] "--bin" takes one argument.
Available binaries:
Available binaries in foo:
foo
Expand Down
66 changes: 33 additions & 33 deletions tests/testsuite/list_availables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ fn build_list_availables() {
SnapshotsBuilder::new()
.with_example(str![[r#"
[ERROR] "--example" takes one argument.
Available examples:
Available examples in foo:
a
b
Expand All @@ -185,7 +185,7 @@ No examples available.
"#]])
.with_bin(str![[r#"
[ERROR] "--bin" takes one argument.
Available binaries:
Available binaries in foo:
foo
Expand All @@ -197,7 +197,7 @@ No binaries available.
"#]])
.with_test(str![[r#"
[ERROR] "--test" takes one argument.
Available test targets:
Available test targets in foo:
test1
test2
Expand All @@ -210,7 +210,7 @@ No test targets available.
"#]])
.with_bench(str![[r#"
[ERROR] "--bench" takes one argument.
Available bench targets:
Available bench targets in foo:
bench1
bench2
Expand Down Expand Up @@ -247,7 +247,7 @@ fn check_list_availables() {
SnapshotsBuilder::new()
.with_example(str![[r#"
[ERROR] "--example" takes one argument.
Available examples:
Available examples in foo:
a
b
Expand All @@ -260,7 +260,7 @@ No examples available.
"#]])
.with_bin(str![[r#"
[ERROR] "--bin" takes one argument.
Available binaries:
Available binaries in foo:
foo
Expand All @@ -272,7 +272,7 @@ No binaries available.
"#]])
.with_test(str![[r#"
[ERROR] "--test" takes one argument.
Available test targets:
Available test targets in foo:
test1
test2
Expand All @@ -285,7 +285,7 @@ No test targets available.
"#]])
.with_bench(str![[r#"
[ERROR] "--bench" takes one argument.
Available bench targets:
Available bench targets in foo:
bench1
bench2
Expand Down Expand Up @@ -322,7 +322,7 @@ fn doc_list_availables() {
SnapshotsBuilder::new()
.with_bin(str![[r#"
[ERROR] "--bin" takes one argument.
Available binaries:
Available binaries in foo:
foo
Expand Down Expand Up @@ -358,7 +358,7 @@ fn fix_list_availables() {
SnapshotsBuilder::new()
.with_example(str![[r#"
[ERROR] "--example" takes one argument.
Available examples:
Available examples in foo:
a
b
Expand All @@ -371,7 +371,7 @@ No examples available.
"#]])
.with_bin(str![[r#"
[ERROR] "--bin" takes one argument.
Available binaries:
Available binaries in foo:
foo
Expand All @@ -383,7 +383,7 @@ No binaries available.
"#]])
.with_test(str![[r#"
[ERROR] "--test" takes one argument.
Available test targets:
Available test targets in foo:
test1
test2
Expand All @@ -396,7 +396,7 @@ No test targets available.
"#]])
.with_bench(str![[r#"
[ERROR] "--bench" takes one argument.
Available bench targets:
Available bench targets in foo:
bench1
bench2
Expand Down Expand Up @@ -433,7 +433,7 @@ fn run_list_availables() {
SnapshotsBuilder::new()
.with_example(str![[r#"
[ERROR] "--example" takes one argument.
Available examples:
Available examples in foo:
a
b
Expand All @@ -446,7 +446,7 @@ No examples available.
"#]])
.with_bin(str![[r#"
[ERROR] "--bin" takes one argument.
Available binaries:
Available binaries in foo:
foo
Expand Down Expand Up @@ -482,7 +482,7 @@ fn test_list_availables() {
SnapshotsBuilder::new()
.with_example(str![[r#"
[ERROR] "--example" takes one argument.
Available examples:
Available examples in foo:
a
b
Expand All @@ -495,7 +495,7 @@ No examples available.
"#]])
.with_bin(str![[r#"
[ERROR] "--bin" takes one argument.
Available binaries:
Available binaries in foo:
foo
Expand All @@ -507,7 +507,7 @@ No binaries available.
"#]])
.with_test(str![[r#"
[ERROR] "--test" takes one argument.
Available test targets:
Available test targets in foo:
test1
test2
Expand All @@ -520,7 +520,7 @@ No test targets available.
"#]])
.with_bench(str![[r#"
[ERROR] "--bench" takes one argument.
Available bench targets:
Available bench targets in foo:
bench1
bench2
Expand Down Expand Up @@ -557,7 +557,7 @@ fn bench_list_availables() {
SnapshotsBuilder::new()
.with_example(str![[r#"
[ERROR] "--example" takes one argument.
Available examples:
Available examples in foo:
a
b
Expand All @@ -570,7 +570,7 @@ No examples available.
"#]])
.with_bin(str![[r#"
[ERROR] "--bin" takes one argument.
Available binaries:
Available binaries in foo:
foo
Expand All @@ -582,7 +582,7 @@ No binaries available.
"#]])
.with_test(str![[r#"
[ERROR] "--test" takes one argument.
Available test targets:
Available test targets in foo:
test1
test2
Expand All @@ -595,7 +595,7 @@ No test targets available.
"#]])
.with_bench(str![[r#"
[ERROR] "--bench" takes one argument.
Available bench targets:
Available bench targets in foo:
bench1
bench2
Expand Down Expand Up @@ -633,7 +633,7 @@ fn install_list_availables() {
.with_example(
str![[r#"
[ERROR] "--example" takes one argument.
Available examples:
Available examples in foo:
a
b
Expand All @@ -649,7 +649,7 @@ No examples available.
.with_bin(
str![[r#"
[ERROR] "--bin" takes one argument.
Available binaries:
Available binaries in foo:
foo
Expand Down Expand Up @@ -678,7 +678,7 @@ fn rustdoc_list_availables() {
SnapshotsBuilder::new()
.with_example(str![[r#"
[ERROR] "--example" takes one argument.
Available examples:
Available examples in foo:
a
b
Expand All @@ -691,7 +691,7 @@ No examples available.
"#]])
.with_bin(str![[r#"
[ERROR] "--bin" takes one argument.
Available binaries:
Available binaries in foo:
foo
Expand All @@ -703,7 +703,7 @@ No binaries available.
"#]])
.with_test(str![[r#"
[ERROR] "--test" takes one argument.
Available test targets:
Available test targets in foo:
test1
test2
Expand All @@ -716,7 +716,7 @@ No test targets available.
"#]])
.with_bench(str![[r#"
[ERROR] "--bench" takes one argument.
Available bench targets:
Available bench targets in foo:
bench1
bench2
Expand Down Expand Up @@ -753,7 +753,7 @@ fn rustc_list_availables() {
SnapshotsBuilder::new()
.with_example(str![[r#"
[ERROR] "--example" takes one argument.
Available examples:
Available examples in foo:
a
b
Expand All @@ -766,7 +766,7 @@ No examples available.
"#]])
.with_bin(str![[r#"
[ERROR] "--bin" takes one argument.
Available binaries:
Available binaries in foo:
foo
Expand All @@ -778,7 +778,7 @@ No binaries available.
"#]])
.with_test(str![[r#"
[ERROR] "--test" takes one argument.
Available test targets:
Available test targets in foo:
test1
test2
Expand All @@ -791,7 +791,7 @@ No test targets available.
"#]])
.with_bench(str![[r#"
[ERROR] "--bench" takes one argument.
Available bench targets:
Available bench targets in foo:
bench1
bench2
Expand Down
3 changes: 2 additions & 1 deletion tests/testsuite/workspaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2767,8 +2767,9 @@ fn print_available_targets_within_virtual_workspace() {
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] "--bin" takes one argument.
Available binaries:
Available binaries in crate1:
crate1
Available binaries in crate2:
crate2
Expand Down

0 comments on commit 7389aff

Please sign in to comment.