Skip to content

Commit

Permalink
feat(cli): emit error when add is given a toolchain name instead of a…
Browse files Browse the repository at this point in the history
… crate name
  • Loading branch information
danilhendrasr committed Jun 3, 2023
1 parent 1548f3b commit 17b1654
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/bin/cargo/commands/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,22 @@ fn parse_dependencies(config: &Config, matches: &ArgMatches) -> CargoResult<Vec<
.flatten()
.map(|c| (Some(c.clone()), None))
.collect::<IndexMap<_, _>>();

let mut infer_crate_name = false;

for (crate_name, _) in crates.iter() {
let crate_name = crate_name.as_ref().unwrap();
if !crate_name.starts_with("+") {
continue;
}

let crate_name_wo_prefix = &crate_name[1..];
anyhow::bail!(
"invalid package name: \"{crate_name}\"
Use `cargo {crate_name} add` if you meant to use the `{crate_name_wo_prefix}` toolchain."
);
}

if crates.is_empty() {
if path.is_some() || git.is_some() {
crates.insert(None, None);
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/cargo_add/add_toolchain/in
23 changes: 23 additions & 0 deletions tests/testsuite/cargo_add/add_toolchain/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use cargo_test_support::compare::assert_ui;
use cargo_test_support::prelude::*;
use cargo_test_support::Project;

use cargo_test_support::curr_dir;

#[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("add")
.arg_line("+nightly")
.current_dir(cwd)
.assert()
.failure()
.stdout_matches_path(curr_dir!().join("stdout.log"))
.stderr_matches_path(curr_dir!().join("stderr.log"));

assert_ui().subset_matches(curr_dir!().join("out"), &project_root);
}
5 changes: 5 additions & 0 deletions tests/testsuite/cargo_add/add_toolchain/out/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[workspace]

[package]
name = "cargo-list-test-fixture"
version = "0.0.0"
2 changes: 2 additions & 0 deletions tests/testsuite/cargo_add/add_toolchain/stderr.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
error: invalid package name: "+nightly"
Use `cargo +nightly add` if you meant to use the `nightly` toolchain.
Empty file.
1 change: 1 addition & 0 deletions tests/testsuite/cargo_add/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod add_basic;
mod add_multiple;
mod add_normalized_name_external;
mod add_toolchain;
mod build;
mod build_prefer_existing_version;
mod change_rename_target;
Expand Down

0 comments on commit 17b1654

Please sign in to comment.