Skip to content

Commit

Permalink
feat(cli): emit error when install is given a toolchain name instead …
Browse files Browse the repository at this point in the history
…of crate name
  • Loading branch information
danilhendrasr committed Jun 3, 2023
1 parent 17b1654 commit 67346ea
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
15 changes: 15 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,20 @@ 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 !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::<String>("git") {
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 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

0 comments on commit 67346ea

Please sign in to comment.