From e40504da0a34bb42d08e3c6d63211bd38a358abf Mon Sep 17 00:00:00 2001 From: dawe Date: Sun, 4 May 2025 22:02:40 +0200 Subject: [PATCH 1/2] test(add): add test to show current behaviour of cargo install when git rev is in url --- tests/testsuite/install.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/testsuite/install.rs b/tests/testsuite/install.rs index dfff7a680c8..02fbee441ee 100644 --- a/tests/testsuite/install.rs +++ b/tests/testsuite/install.rs @@ -2169,6 +2169,33 @@ fn git_install_reads_workspace_manifest() { .run(); } +#[cargo_test] +fn install_git_with_rev_in_url() { + let p = git::repo(&paths::root().join("foo")) + .file("Cargo.toml", &basic_manifest("foo", "0.1.0")) + .file("src/main.rs", "fn main() {}") + .build(); + + let mut url = p.url().to_string(); + url.push_str("#0e8d88b5cfc173c5f5a6a0fe0ce1d4e6018600d4"); + + cargo_process("install --locked --git") + .arg(url.to_string()) + .with_stderr_data(str![[r#" +[UPDATING] git repository `[ROOTURL]/foo#0e8d88b5cfc173c5f5a6a0fe0ce1d4e6018600d4` +[WARNING] spurious network error (3 tries remaining): failed to resolve path '[ROOT]/foo#0e8d88b5cfc173c5f5a6a0fe0ce1d4e6018600d4': No such file or directory; class=Os (2) +[WARNING] spurious network error (2 tries remaining): failed to resolve path '[ROOT]/foo#0e8d88b5cfc173c5f5a6a0fe0ce1d4e6018600d4': No such file or directory; class=Os (2) +[WARNING] spurious network error (1 try remaining): failed to resolve path '[ROOT]/foo#0e8d88b5cfc173c5f5a6a0fe0ce1d4e6018600d4': No such file or directory; class=Os (2) +[ERROR] failed to clone into: [ROOT]/home/.cargo/git/db/foo-[HASH] + +Caused by: + failed to resolve path '[ROOT]/foo#0e8d88b5cfc173c5f5a6a0fe0ce1d4e6018600d4': No such file or directory; class=Os (2) + +"#]]) + .with_status(101) + .run(); +} + #[cargo_test] fn install_git_with_symlink_home() { // Ensure that `cargo install` with a git repo is OK when CARGO_HOME is a From dcc8cef7c359515b9906e32fd3ffde4e87c6cae3 Mon Sep 17 00:00:00 2001 From: dawe Date: Sun, 4 May 2025 22:15:50 +0200 Subject: [PATCH 2/2] fix(install): return error when rev is in git url return error if the git url contains a # and hint user to use --rev --- src/bin/cargo/commands/install.rs | 8 ++++++++ tests/testsuite/install.rs | 11 +++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/bin/cargo/commands/install.rs b/src/bin/cargo/commands/install.rs index 0f97071e69a..4ee693c57f1 100644 --- a/src/bin/cargo/commands/install.rs +++ b/src/bin/cargo/commands/install.rs @@ -173,6 +173,14 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult { let source = if let Some(url) = args.get_one::("git") { let url = url.into_url()?; + if url.fragment().is_some() { + return Err(anyhow::format_err!( + "invalid git url to install from\n\n\ + help: use `--rev ` to specify a commit" + ) + .into()); + } + let gitref = if let Some(branch) = args.get_one::("branch") { GitReference::Branch(branch.clone()) } else if let Some(tag) = args.get_one::("tag") { diff --git a/tests/testsuite/install.rs b/tests/testsuite/install.rs index 02fbee441ee..03980a8078b 100644 --- a/tests/testsuite/install.rs +++ b/tests/testsuite/install.rs @@ -2180,16 +2180,11 @@ fn install_git_with_rev_in_url() { url.push_str("#0e8d88b5cfc173c5f5a6a0fe0ce1d4e6018600d4"); cargo_process("install --locked --git") - .arg(url.to_string()) + .arg(url) .with_stderr_data(str![[r#" -[UPDATING] git repository `[ROOTURL]/foo#0e8d88b5cfc173c5f5a6a0fe0ce1d4e6018600d4` -[WARNING] spurious network error (3 tries remaining): failed to resolve path '[ROOT]/foo#0e8d88b5cfc173c5f5a6a0fe0ce1d4e6018600d4': No such file or directory; class=Os (2) -[WARNING] spurious network error (2 tries remaining): failed to resolve path '[ROOT]/foo#0e8d88b5cfc173c5f5a6a0fe0ce1d4e6018600d4': No such file or directory; class=Os (2) -[WARNING] spurious network error (1 try remaining): failed to resolve path '[ROOT]/foo#0e8d88b5cfc173c5f5a6a0fe0ce1d4e6018600d4': No such file or directory; class=Os (2) -[ERROR] failed to clone into: [ROOT]/home/.cargo/git/db/foo-[HASH] +[ERROR] invalid git url to install from -Caused by: - failed to resolve path '[ROOT]/foo#0e8d88b5cfc173c5f5a6a0fe0ce1d4e6018600d4': No such file or directory; class=Os (2) +[HELP] use `--rev ` to specify a commit "#]]) .with_status(101)