From ecf05e4ff1dee9148a83c0e83f24fccdbdb2f813 Mon Sep 17 00:00:00 2001 From: Matheus Cardoso Date: Sun, 27 Aug 2023 23:03:14 -0300 Subject: [PATCH 1/2] cargo install: suggest --git when package name is url --- src/bin/cargo/commands/install.rs | 8 ++++++++ tests/testsuite/install.rs | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/bin/cargo/commands/install.rs b/src/bin/cargo/commands/install.rs index 8e3062253b1..8cf59e5afa7 100644 --- a/src/bin/cargo/commands/install.rs +++ b/src/bin/cargo/commands/install.rs @@ -121,6 +121,14 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { ) .into()); } + + if let Ok(url) = crate_name.into_url() { + return Err(anyhow!( + "invalid package name: `{url}` + Use `cargo install --git {url}` if you meant to install from a git repository." + ) + .into()); + } } let mut from_cwd = false; diff --git a/tests/testsuite/install.rs b/tests/testsuite/install.rs index a3d456ace0d..98f11356e0a 100644 --- a/tests/testsuite/install.rs +++ b/tests/testsuite/install.rs @@ -71,6 +71,18 @@ fn toolchain() { .run(); } +#[cargo_test] +fn url() { + pkg("foo", "0.0.1"); + cargo_process("install https://github.com/bar/foo") + .with_status(101) + .with_stderr( + "\ +[ERROR] invalid package name: `https://github.com/bar/foo` + Use `cargo install --git https://github.com/bar/foo` if you meant to install from a git repository.") + .run(); +} + #[cargo_test] fn simple_with_message_format() { pkg("foo", "0.0.1"); From 40dae6123a5a848d69cf03fbfc6ecb71ab60c26b Mon Sep 17 00:00:00 2001 From: Matheus Cardoso Date: Mon, 28 Aug 2023 18:47:30 -0300 Subject: [PATCH 2/2] only suggest --git flag if scheme is http(s) --- src/bin/cargo/commands/install.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/bin/cargo/commands/install.rs b/src/bin/cargo/commands/install.rs index 8cf59e5afa7..4ec3f6951e6 100644 --- a/src/bin/cargo/commands/install.rs +++ b/src/bin/cargo/commands/install.rs @@ -123,11 +123,13 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { } if let Ok(url) = crate_name.into_url() { - return Err(anyhow!( - "invalid package name: `{url}` + if matches!(url.scheme(), "http" | "https") { + return Err(anyhow!( + "invalid package name: `{url}` Use `cargo install --git {url}` if you meant to install from a git repository." - ) - .into()); + ) + .into()); + } } }