Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve error message for cargo add/remove #11375

Merged
merged 1 commit into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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