Skip to content

Commit 333ca23

Browse files
committed
Auto merge of #12575 - cardoso:missing_git_flag, r=arlosi
cargo install: suggest --git when package name is url ### What does this PR try to resolve? Improve the error message when specifying a URL for a package name in `cargo install`. Fixes #10485 ### How should we test and review this PR? Just cargo test and trying a common case like `cargo install https://github.com/rust-lang/cargo` ### Additional information I found this PR after finishing this one: #10522 But it seems have a larger scope to refactor some of the related code. Perhaps this one would be easier to merge and that one could focus on the refactor, otherwise sorry for the noise and feel free to close.
2 parents 0b29588 + 40dae61 commit 333ca23

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/bin/cargo/commands/install.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
121121
)
122122
.into());
123123
}
124+
125+
if let Ok(url) = crate_name.into_url() {
126+
if matches!(url.scheme(), "http" | "https") {
127+
return Err(anyhow!(
128+
"invalid package name: `{url}`
129+
Use `cargo install --git {url}` if you meant to install from a git repository."
130+
)
131+
.into());
132+
}
133+
}
124134
}
125135

126136
let mut from_cwd = false;

tests/testsuite/install.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ fn toolchain() {
7171
.run();
7272
}
7373

74+
#[cargo_test]
75+
fn url() {
76+
pkg("foo", "0.0.1");
77+
cargo_process("install https://github.com/bar/foo")
78+
.with_status(101)
79+
.with_stderr(
80+
"\
81+
[ERROR] invalid package name: `https://github.com/bar/foo`
82+
Use `cargo install --git https://github.com/bar/foo` if you meant to install from a git repository.")
83+
.run();
84+
}
85+
7486
#[cargo_test]
7587
fn simple_with_message_format() {
7688
pkg("foo", "0.0.1");

0 commit comments

Comments
 (0)