Skip to content

Commit

Permalink
Auto merge of #11375 - xxchan:xxchan/experienced-limpet, r=epage
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
bors committed Nov 14, 2022
2 parents 26f4c03 + a858cc6 commit 16b0978
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
27 changes: 16 additions & 11 deletions src/bin/cargo/commands/add.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cargo::sources::CRATES_IO_REGISTRY;
use cargo::util::print_available_packages;
use indexmap::IndexMap;
use indexmap::IndexSet;

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

let ws = args.workspace(config)?;

if args.is_present_with_zero_values("package") {
print_available_packages(&ws)?;
}

let packages = args.packages_from_flags()?;
let packages = packages.get_packages(&ws)?;
let spec = match packages.len() {
0 => {
return Err(CliError::new(
anyhow::format_err!("no packages selected. Please specify one with `-p <PKGID>`"),
anyhow::format_err!(
"no packages selected to modify. Please specify one with `-p <PKGID>`"
),
101,
));
}
1 => packages[0],
len => {
_ => {
let names = packages.iter().map(|p| p.name()).collect::<Vec<_>>();
return Err(CliError::new(
anyhow::format_err!(
"{len} packages selected. Please specify one with `-p <PKGID>`",
"`cargo add` could not determine which package to modify. \
Use the `--package` option to specify a package. \n\
available packages: {}",
names.join(", ")
),
101,
));
Expand Down
18 changes: 15 additions & 3 deletions src/bin/cargo/commands/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use cargo::ops::cargo_remove::remove;
use cargo::ops::cargo_remove::RemoveOptions;
use cargo::ops::resolve_ws;
use cargo::util::command_prelude::*;
use cargo::util::print_available_packages;
use cargo::util::toml_mut::manifest::DepTable;
use cargo::util::toml_mut::manifest::LocalManifest;
use cargo::CargoResult;
Expand Down Expand Up @@ -50,20 +51,31 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
let dry_run = args.dry_run();

let workspace = args.workspace(config)?;

if args.is_present_with_zero_values("package") {
print_available_packages(&workspace)?;
}

let packages = args.packages_from_flags()?;
let packages = packages.get_packages(&workspace)?;
let spec = match packages.len() {
0 => {
return Err(CliError::new(
anyhow::format_err!("no packages selected. Please specify one with `-p <PKG_ID>`"),
anyhow::format_err!(
"no packages selected to modify. Please specify one with `-p <PKGID>`"
),
101,
));
}
1 => packages[0],
len => {
_ => {
let names = packages.iter().map(|p| p.name()).collect::<Vec<_>>();
return Err(CliError::new(
anyhow::format_err!(
"{len} packages selected. Please specify one with `-p <PKG_ID>`",
"`cargo remove` could not determine which package to modify. \
Use the `--package` option to specify a package. \n\
available packages: {}",
names.join(", ")
),
101,
));
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
error: 2 packages selected. Please specify one with `-p <PKG_ID>`
error: `cargo remove` could not determine which package to modify. Use the `--package` option to specify a package.
available packages: dep-a, dep-b

0 comments on commit 16b0978

Please sign in to comment.