Skip to content

Commit 06a5153

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

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/bin/cargo/commands/install.rs

Lines changed: 15 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,20 @@ 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 !crate_name.starts_with("+") {
114+
continue;
115+
}
116+
117+
let crate_name_wo_prefix = &crate_name[1..];
118+
119+
return Err(anyhow!(
120+
"invalid package name: \"{crate_name}\"
121+
Use `cargo {crate_name} install` if you meant to use the `{crate_name_wo_prefix}` toolchain."
122+
)
123+
.into());
124+
}
125+
111126
let mut from_cwd = false;
112127

113128
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 package 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)