Skip to content

Commit

Permalink
Auto merge of #12575 - cardoso:missing_git_flag, r=arlosi
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Aug 29, 2023
2 parents 0b29588 + 40dae61 commit 333ca23
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/bin/cargo/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
)
.into());
}

if let Ok(url) = crate_name.into_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());
}
}
}

let mut from_cwd = false;
Expand Down
12 changes: 12 additions & 0 deletions tests/testsuite/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 333ca23

Please sign in to comment.