Skip to content

Commit 16b0978

Browse files
committed
Auto merge of #11375 - xxchan:xxchan/experienced-limpet, r=epage
improve error message for cargo add/remove ### What does this PR try to resolve? When I see the old error: ``` > cargo add paste error: 2 packages selected. Please specify one with `-p <PKGID>` ``` I was a little bit confused, and thought it says there are 2 packages called "paste". The new message is similar to `cargo run`
2 parents 26f4c03 + a858cc6 commit 16b0978

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

src/bin/cargo/commands/add.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use cargo::sources::CRATES_IO_REGISTRY;
2+
use cargo::util::print_available_packages;
23
use indexmap::IndexMap;
34
use indexmap::IndexSet;
45

@@ -73,14 +74,7 @@ Example uses:
7374
- Depend on crates with the same name from different registries"),
7475
])
7576
.arg_manifest_path()
76-
.args([
77-
clap::Arg::new("package")
78-
.short('p')
79-
.long("package")
80-
.action(ArgAction::Set)
81-
.value_name("SPEC")
82-
.help("Package to modify"),
83-
])
77+
.arg_package("Package to modify")
8478
.arg_quiet()
8579
.arg_dry_run("Don't actually write the manifest")
8680
.next_help_heading("Source")
@@ -161,20 +155,31 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
161155
let section = parse_section(args);
162156

163157
let ws = args.workspace(config)?;
158+
159+
if args.is_present_with_zero_values("package") {
160+
print_available_packages(&ws)?;
161+
}
162+
164163
let packages = args.packages_from_flags()?;
165164
let packages = packages.get_packages(&ws)?;
166165
let spec = match packages.len() {
167166
0 => {
168167
return Err(CliError::new(
169-
anyhow::format_err!("no packages selected. Please specify one with `-p <PKGID>`"),
168+
anyhow::format_err!(
169+
"no packages selected to modify. Please specify one with `-p <PKGID>`"
170+
),
170171
101,
171172
));
172173
}
173174
1 => packages[0],
174-
len => {
175+
_ => {
176+
let names = packages.iter().map(|p| p.name()).collect::<Vec<_>>();
175177
return Err(CliError::new(
176178
anyhow::format_err!(
177-
"{len} packages selected. Please specify one with `-p <PKGID>`",
179+
"`cargo add` could not determine which package to modify. \
180+
Use the `--package` option to specify a package. \n\
181+
available packages: {}",
182+
names.join(", ")
178183
),
179184
101,
180185
));

src/bin/cargo/commands/remove.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use cargo::ops::cargo_remove::remove;
44
use cargo::ops::cargo_remove::RemoveOptions;
55
use cargo::ops::resolve_ws;
66
use cargo::util::command_prelude::*;
7+
use cargo::util::print_available_packages;
78
use cargo::util::toml_mut::manifest::DepTable;
89
use cargo::util::toml_mut::manifest::LocalManifest;
910
use cargo::CargoResult;
@@ -50,20 +51,31 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
5051
let dry_run = args.dry_run();
5152

5253
let workspace = args.workspace(config)?;
54+
55+
if args.is_present_with_zero_values("package") {
56+
print_available_packages(&workspace)?;
57+
}
58+
5359
let packages = args.packages_from_flags()?;
5460
let packages = packages.get_packages(&workspace)?;
5561
let spec = match packages.len() {
5662
0 => {
5763
return Err(CliError::new(
58-
anyhow::format_err!("no packages selected. Please specify one with `-p <PKG_ID>`"),
64+
anyhow::format_err!(
65+
"no packages selected to modify. Please specify one with `-p <PKGID>`"
66+
),
5967
101,
6068
));
6169
}
6270
1 => packages[0],
63-
len => {
71+
_ => {
72+
let names = packages.iter().map(|p| p.name()).collect::<Vec<_>>();
6473
return Err(CliError::new(
6574
anyhow::format_err!(
66-
"{len} packages selected. Please specify one with `-p <PKG_ID>`",
75+
"`cargo remove` could not determine which package to modify. \
76+
Use the `--package` option to specify a package. \n\
77+
available packages: {}",
78+
names.join(", ")
6779
),
6880
101,
6981
));
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
error: 2 packages selected. Please specify one with `-p <PKG_ID>`
1+
error: `cargo remove` could not determine which package to modify. Use the `--package` option to specify a package.
2+
available packages: dep-a, dep-b

0 commit comments

Comments
 (0)