Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit error when users try to use a toolchain via the add or install command #12226

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/bin/cargo/commands/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,20 @@ 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 let Some(toolchain) = crate_name.strip_prefix("+") {
anyhow::bail!(
"invalid character `+` in dependency name: `+{toolchain}`
Use `cargo +{toolchain} add` if you meant to use the `{toolchain}` toolchain."
);
}
}

if crates.is_empty() {
if path.is_some() || git.is_some() {
crates.insert(None, None);
Expand Down
11 changes: 11 additions & 0 deletions src/bin/cargo/commands/install.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::command_prelude::*;

use anyhow::anyhow;
use cargo::core::{GitReference, SourceId, Workspace};
use cargo::ops;
use cargo::util::IntoUrl;
Expand Down Expand Up @@ -108,6 +109,16 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
.map(|k| resolve_crate(k, version))
.collect::<crate::CargoResult<Vec<_>>>()?;

for (crate_name, _) in krates.iter() {
if let Some(toolchain) = crate_name.strip_prefix("+") {
return Err(anyhow!(
"invalid character `+` in package name: `+{toolchain}`
Use `cargo +{toolchain} install` if you meant to use the `{toolchain}` toolchain."
)
.into());
}
}

let mut from_cwd = false;

let source = if let Some(url) = args.get_one::<String>("git") {
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 character `+` in dependency 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
14 changes: 14 additions & 0 deletions tests/testsuite/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ fn simple() {
assert_has_not_installed_exe(cargo_home(), "foo");
}

#[cargo_test]
fn toolchain() {
pkg("foo", "0.0.1");

cargo_process("install +nightly")
.with_status(101)
.with_stderr(
"\
[ERROR] invalid character `+` in package name: `+nightly`
Use `cargo +nightly install` if you meant to use the `nightly` toolchain.",
)
.run();
}

#[cargo_test]
fn simple_with_message_format() {
pkg("foo", "0.0.1");
Expand Down