Skip to content

Commit fa522cc

Browse files
committed
fix: more information when -p value is absent
1 parent 4d5a56d commit fa522cc

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

src/cargo/util/command_prelude.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,9 @@ pub trait ArgMatchesExt {
502502
// As for cargo 0.50.0, this won't occur but if someone sneaks in
503503
// we can still provide this informative message for them.
504504
anyhow::bail!(
505-
"\"--package <SPEC>\" requires a SPEC format value.\n\
506-
Run `cargo help pkgid` for more infomation about SPEC format."
505+
"\"--package <SPEC>\" requires a SPEC format value, \n\
506+
which can be any package ID specifier in the dependency graph.\n\
507+
Run `cargo help pkgid` for more information about SPEC format."
507508
)
508509
}
509510

src/cargo/util/workspace.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,12 @@ fn print_available_targets(
3939
let mut output = String::new();
4040
writeln!(output, "\"{}\" takes one argument.", option_name)?;
4141

42-
print_availables(output, &targets, plural_name)
43-
}
44-
45-
fn print_availables(output: String, availables: &[&str], plural_name: &str) -> CargoResult<()> {
46-
let mut output = output;
47-
if availables.is_empty() {
42+
if targets.is_empty() {
4843
writeln!(output, "No {} available.", plural_name)?;
4944
} else {
5045
writeln!(output, "Available {}:", plural_name)?;
51-
for available in availables {
52-
writeln!(output, " {}", available)?;
46+
for target in targets {
47+
writeln!(output, " {}", target)?;
5348
}
5449
}
5550
bail!("{}", output)
@@ -61,16 +56,25 @@ pub fn print_available_packages(ws: &Workspace<'_>) -> CargoResult<()> {
6156
.map(|pkg| pkg.name().as_str())
6257
.collect::<Vec<_>>();
6358

64-
let mut output = String::new();
65-
writeln!(
66-
output,
67-
"\"--package <SPEC>\" requires a SPEC format value.\n\
68-
Run `cargo help pkgid` for more infomation about SPEC format."
69-
)?;
59+
let mut output = "\"--package <SPEC>\" requires a SPEC format value, \
60+
which can be any package ID specifier in the dependency graph.\n\
61+
Run `cargo help pkgid` for more information about SPEC format.\n\n"
62+
.to_string();
7063

71-
print_availables(output, &packages, "packages")
64+
if packages.is_empty() {
65+
// This would never happen.
66+
// Just in case something regresses we covers it here.
67+
writeln!(output, "No packages available.")?;
68+
} else {
69+
writeln!(output, "Possible packages/workspace members:")?;
70+
for package in packages {
71+
writeln!(output, " {}", package)?;
72+
}
73+
}
74+
bail!("{}", output)
7275
}
7376

77+
7478
pub fn print_available_examples(ws: &Workspace<'_>, options: &CompileOptions) -> CargoResult<()> {
7579
print_available_targets(Target::is_example, ws, options, "--example", "examples")
7680
}

tests/testsuite/list_availables.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ Available tests:
8888
.cargo(&format!("{} -p", command))
8989
.with_stderr(
9090
"\
91-
[ERROR] \"--package <SPEC>\" requires a SPEC format value.
92-
Run `cargo help pkgid` for more infomation about SPEC format.
93-
Available packages:
91+
[ERROR] \"--package <SPEC>\" requires a SPEC format value, \
92+
which can be any package ID specifier in the dependency graph.
93+
Run `cargo help pkgid` for more information about SPEC format.
94+
95+
Possible packages/workspace members:
9496
foo
9597
9698
",

0 commit comments

Comments
 (0)