Skip to content

Commit 06b368d

Browse files
committed
fix(update): Improve error on bad argument order
This is inspired by `cargo install`
1 parent 99b0ecb commit 06b368d

File tree

7 files changed

+42
-1
lines changed

7 files changed

+42
-1
lines changed

src/bin/cargo/commands/update.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::command_prelude::*;
22

3+
use anyhow::anyhow;
34
use cargo::ops::{self, UpdateOptions};
45
use cargo::util::print_available_packages;
56

@@ -58,11 +59,21 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
5859
} else {
5960
"package2"
6061
};
62+
let to_update = values(args, to_update);
63+
for crate_name in to_update.iter() {
64+
if let Some(toolchain) = crate_name.strip_prefix("+") {
65+
return Err(anyhow!(
66+
"invalid character `+` in package name: `+{toolchain}`
67+
Use `cargo +{toolchain} update` if you meant to use the `{toolchain}` toolchain."
68+
)
69+
.into());
70+
}
71+
}
6172

6273
let update_opts = UpdateOptions {
6374
aggressive: args.flag("aggressive"),
6475
precise: args.get_one::<String>("precise").map(String::as_str),
65-
to_update: values(args, to_update),
76+
to_update,
6677
dry_run: args.dry_run(),
6778
workspace: args.flag("workspace"),
6879
config,

tests/testsuite/cargo_update/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
mod help;
2+
mod toolchain_pkgname;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[package]
2+
name = "test"
3+
version = "0.1.0"
4+
5+
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("Hello, world!");
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use cargo_test_support::curr_dir;
2+
use cargo_test_support::prelude::*;
3+
use cargo_test_support::Project;
4+
5+
#[cargo_test]
6+
fn case() {
7+
let project = Project::from_template(curr_dir!().join("in"));
8+
let project_root = project.root();
9+
let cwd = &project_root;
10+
11+
snapbox::cmd::Command::cargo_ui()
12+
.arg("update")
13+
.arg("+stable")
14+
.current_dir(cwd)
15+
.assert()
16+
.code(101)
17+
.stdout_matches_path(curr_dir!().join("stdout.log"))
18+
.stderr_matches_path(curr_dir!().join("stderr.log"));
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: invalid character `+` in package name: `+stable`
2+
Use `cargo +stable update` if you meant to use the `stable` toolchain.

tests/testsuite/cargo_update/toolchain_pkgname/stdout.log

Whitespace-only changes.

0 commit comments

Comments
 (0)