From 67346ea292cd17ba02972dc312ae39237e0eff7f Mon Sep 17 00:00:00 2001 From: Danil Hendra Suryawan Date: Sat, 3 Jun 2023 07:04:56 +0700 Subject: [PATCH] feat(cli): emit error when install is given a toolchain name instead of crate name --- src/bin/cargo/commands/install.rs | 15 +++++++++++++++ tests/testsuite/install.rs | 14 ++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/bin/cargo/commands/install.rs b/src/bin/cargo/commands/install.rs index 8197a16900f4..d79d97bee860 100644 --- a/src/bin/cargo/commands/install.rs +++ b/src/bin/cargo/commands/install.rs @@ -1,5 +1,6 @@ use crate::command_prelude::*; +use anyhow::anyhow; use cargo::core::{GitReference, SourceId, Workspace}; use cargo::ops; use cargo::util::IntoUrl; @@ -108,6 +109,20 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { .map(|k| resolve_crate(k, version)) .collect::>>()?; + for (crate_name, _) in krates.iter() { + if !crate_name.starts_with("+") { + continue; + } + + let crate_name_wo_prefix = crate_name[1..].to_string(); + + return Err(anyhow!( + "invalid package name: \"{crate_name}\" + Use `cargo {crate_name} install` if you meant to use the `{crate_name_wo_prefix}` toolchain." + ) + .into()); + } + let mut from_cwd = false; let source = if let Some(url) = args.get_one::("git") { diff --git a/tests/testsuite/install.rs b/tests/testsuite/install.rs index 9b881dfdc68b..774b9a98881e 100644 --- a/tests/testsuite/install.rs +++ b/tests/testsuite/install.rs @@ -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 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");