Skip to content

Commit

Permalink
fix(update): Improve error on bad argument order
Browse files Browse the repository at this point in the history
This is inspired by `cargo install`
  • Loading branch information
epage committed Aug 23, 2023
1 parent 4bf1af0 commit dc37222
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/bin/cargo/commands/update.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::command_prelude::*;

use anyhow::anyhow;
use cargo::ops::{self, UpdateOptions};
use cargo::util::print_available_packages;

Expand Down Expand Up @@ -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::<String>("precise").map(String::as_str),
to_update: values(args, to_update),
to_update,
dry_run: args.dry_run(),
workspace: args.flag("workspace"),
config,
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/cargo_update/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod help;
mod toolchain_pkgname;
5 changes: 5 additions & 0 deletions tests/testsuite/cargo_update/toolchain_pkgname/in/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "test"
version = "0.1.0"

[dependencies]
3 changes: 3 additions & 0 deletions tests/testsuite/cargo_update/toolchain_pkgname/in/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}
19 changes: 19 additions & 0 deletions tests/testsuite/cargo_update/toolchain_pkgname/mod.rs
Original file line number Diff line number Diff line change
@@ -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"));
}
2 changes: 2 additions & 0 deletions tests/testsuite/cargo_update/toolchain_pkgname/stderr.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
error: invalid character `+` in package name: `+stable`
Use `cargo +stable update` if you meant to use the `stable` toolchain.
Empty file.

0 comments on commit dc37222

Please sign in to comment.