From 574064bfbc815146bc70252b7a28de1dae5ce007 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 23 Aug 2023 14:44:13 -0500 Subject: [PATCH] fix(update): Improve error on bad argument order This is inspired by `cargo install` --- src/bin/cargo/commands/update.rs | 13 ++++++++++++- tests/testsuite/cargo_update/mod.rs | 1 + .../toolchain_pkgname/in/Cargo.toml | 5 +++++ .../toolchain_pkgname/in/src/main.rs | 3 +++ .../cargo_update/toolchain_pkgname/mod.rs | 19 +++++++++++++++++++ .../cargo_update/toolchain_pkgname/stderr.log | 2 ++ .../cargo_update/toolchain_pkgname/stdout.log | 0 7 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tests/testsuite/cargo_update/toolchain_pkgname/in/Cargo.toml create mode 100644 tests/testsuite/cargo_update/toolchain_pkgname/in/src/main.rs create mode 100644 tests/testsuite/cargo_update/toolchain_pkgname/mod.rs create mode 100644 tests/testsuite/cargo_update/toolchain_pkgname/stderr.log create mode 100644 tests/testsuite/cargo_update/toolchain_pkgname/stdout.log diff --git a/src/bin/cargo/commands/update.rs b/src/bin/cargo/commands/update.rs index 0ceeb7071e00..4871b2cb430e 100644 --- a/src/bin/cargo/commands/update.rs +++ b/src/bin/cargo/commands/update.rs @@ -1,5 +1,6 @@ use crate::command_prelude::*; +use anyhow::anyhow; use cargo::ops::{self, UpdateOptions}; use cargo::util::print_available_packages; @@ -58,11 +59,21 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { } else { "package2" }; + let to_update = values(args, to_update); + for crate_name in to_update.iter() { + if let Some(toolchain) = crate_name.strip_prefix("+") { + return Err(anyhow!( + "invalid character `+` in package name: `+{toolchain}` + Use `cargo +{toolchain} update` if you meant to use the `{toolchain}` toolchain." + ) + .into()); + } + } let update_opts = UpdateOptions { aggressive: args.flag("aggressive"), precise: args.get_one::("precise").map(String::as_str), - to_update: values(args, to_update), + to_update, dry_run: args.dry_run(), workspace: args.flag("workspace"), config, diff --git a/tests/testsuite/cargo_update/mod.rs b/tests/testsuite/cargo_update/mod.rs index c0ce11180711..a809d9fe9511 100644 --- a/tests/testsuite/cargo_update/mod.rs +++ b/tests/testsuite/cargo_update/mod.rs @@ -1 +1,2 @@ mod help; +mod toolchain_pkgname; diff --git a/tests/testsuite/cargo_update/toolchain_pkgname/in/Cargo.toml b/tests/testsuite/cargo_update/toolchain_pkgname/in/Cargo.toml new file mode 100644 index 000000000000..c844631b2e8c --- /dev/null +++ b/tests/testsuite/cargo_update/toolchain_pkgname/in/Cargo.toml @@ -0,0 +1,5 @@ +[package] +name = "test" +version = "0.1.0" + +[dependencies] diff --git a/tests/testsuite/cargo_update/toolchain_pkgname/in/src/main.rs b/tests/testsuite/cargo_update/toolchain_pkgname/in/src/main.rs new file mode 100644 index 000000000000..e7a11a969c03 --- /dev/null +++ b/tests/testsuite/cargo_update/toolchain_pkgname/in/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/tests/testsuite/cargo_update/toolchain_pkgname/mod.rs b/tests/testsuite/cargo_update/toolchain_pkgname/mod.rs new file mode 100644 index 000000000000..f1488b90d160 --- /dev/null +++ b/tests/testsuite/cargo_update/toolchain_pkgname/mod.rs @@ -0,0 +1,19 @@ +use cargo_test_support::curr_dir; +use cargo_test_support::prelude::*; +use cargo_test_support::Project; + +#[cargo_test] +fn case() { + let project = Project::from_template(curr_dir!().join("in")); + let project_root = project.root(); + let cwd = &project_root; + + snapbox::cmd::Command::cargo_ui() + .arg("update") + .arg("+stable") + .current_dir(cwd) + .assert() + .code(101) + .stdout_matches_path(curr_dir!().join("stdout.log")) + .stderr_matches_path(curr_dir!().join("stderr.log")); +} diff --git a/tests/testsuite/cargo_update/toolchain_pkgname/stderr.log b/tests/testsuite/cargo_update/toolchain_pkgname/stderr.log new file mode 100644 index 000000000000..7e5870c540a2 --- /dev/null +++ b/tests/testsuite/cargo_update/toolchain_pkgname/stderr.log @@ -0,0 +1,2 @@ +error: invalid character `+` in package name: `+stable` + Use `cargo +stable update` if you meant to use the `stable` toolchain. diff --git a/tests/testsuite/cargo_update/toolchain_pkgname/stdout.log b/tests/testsuite/cargo_update/toolchain_pkgname/stdout.log new file mode 100644 index 000000000000..e69de29bb2d1