Skip to content

Commit 5be09b9

Browse files
committed
feat(cli): emit error when install is given a toolchain name instead of crate name
1 parent afcd70a commit 5be09b9

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/bin/cargo/commands/install.rs

Lines changed: 11 additions & 0 deletions
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::core::{GitReference, SourceId, Workspace};
45
use cargo::ops;
56
use cargo::util::IntoUrl;
@@ -108,6 +109,16 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
108109
.map(|k| resolve_crate(k, version))
109110
.collect::<crate::CargoResult<Vec<_>>>()?;
110111

112+
for (crate_name, _) in krates.iter() {
113+
if let Some(toolchain) = crate_name.strip_prefix("+") {
114+
return Err(anyhow!(
115+
"invalid character `+` in dependency name: \"+{toolchain}\"
116+
Use `cargo +{toolchain} install` if you meant to use the `{toolchain}` toolchain."
117+
)
118+
.into());
119+
}
120+
}
121+
111122
let mut from_cwd = false;
112123

113124
let source = if let Some(url) = args.get_one::<String>("git") {

tests/testsuite/install.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ fn simple() {
5757
assert_has_not_installed_exe(cargo_home(), "foo");
5858
}
5959

60+
#[cargo_test]
61+
fn toolchain() {
62+
pkg("foo", "0.0.1");
63+
64+
cargo_process("install +nightly")
65+
.with_status(101)
66+
.with_stderr(
67+
"\
68+
[ERROR] invalid character `+` in dependency name: \"+nightly\"
69+
Use `cargo +nightly install` if you meant to use the `nightly` toolchain.",
70+
)
71+
.run();
72+
}
73+
6074
#[cargo_test]
6175
fn simple_with_message_format() {
6276
pkg("foo", "0.0.1");

0 commit comments

Comments
 (0)