Skip to content

Commit 17b1654

Browse files
committed
feat(cli): emit error when add is given a toolchain name instead of a crate name
1 parent 1548f3b commit 17b1654

File tree

7 files changed

+47
-0
lines changed

7 files changed

+47
-0
lines changed

src/bin/cargo/commands/add.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,22 @@ fn parse_dependencies(config: &Config, matches: &ArgMatches) -> CargoResult<Vec<
242242
.flatten()
243243
.map(|c| (Some(c.clone()), None))
244244
.collect::<IndexMap<_, _>>();
245+
245246
let mut infer_crate_name = false;
247+
248+
for (crate_name, _) in crates.iter() {
249+
let crate_name = crate_name.as_ref().unwrap();
250+
if !crate_name.starts_with("+") {
251+
continue;
252+
}
253+
254+
let crate_name_wo_prefix = &crate_name[1..];
255+
anyhow::bail!(
256+
"invalid package name: \"{crate_name}\"
257+
Use `cargo {crate_name} add` if you meant to use the `{crate_name_wo_prefix}` toolchain."
258+
);
259+
}
260+
246261
if crates.is_empty() {
247262
if path.is_some() || git.is_some() {
248263
crates.insert(None, None);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../add-basic.in
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use cargo_test_support::compare::assert_ui;
2+
use cargo_test_support::prelude::*;
3+
use cargo_test_support::Project;
4+
5+
use cargo_test_support::curr_dir;
6+
7+
#[cargo_test]
8+
fn case() {
9+
let project = Project::from_template(curr_dir!().join("in"));
10+
let project_root = project.root();
11+
let cwd = &project_root;
12+
13+
snapbox::cmd::Command::cargo_ui()
14+
.arg("add")
15+
.arg_line("+nightly")
16+
.current_dir(cwd)
17+
.assert()
18+
.failure()
19+
.stdout_matches_path(curr_dir!().join("stdout.log"))
20+
.stderr_matches_path(curr_dir!().join("stderr.log"));
21+
22+
assert_ui().subset_matches(curr_dir!().join("out"), &project_root);
23+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[workspace]
2+
3+
[package]
4+
name = "cargo-list-test-fixture"
5+
version = "0.0.0"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: invalid package name: "+nightly"
2+
Use `cargo +nightly add` if you meant to use the `nightly` toolchain.

tests/testsuite/cargo_add/add_toolchain/stdout.log

Whitespace-only changes.

tests/testsuite/cargo_add/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod add_basic;
22
mod add_multiple;
33
mod add_normalized_name_external;
4+
mod add_toolchain;
45
mod build;
56
mod build_prefer_existing_version;
67
mod change_rename_target;

0 commit comments

Comments
 (0)