diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index bc80097b705..ea6a088cdf4 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -4,6 +4,7 @@ use std::ffi::OsStr; use std::path::{Path, PathBuf}; use std::rc::Rc; use std::str::{self, FromStr}; +use tracing_subscriber::fmt::format; use crate::core::summary::MissingDependencyError; use crate::AlreadyPrintedError; @@ -2144,6 +2145,8 @@ fn to_dependency_source_id( .unwrap_or(GitReference::DefaultBranch); let loc = git.into_url()?; + bail_if_github_pull_request(&name_in_toml, &loc)?; + if let Some(fragment) = loc.fragment() { let msg = format!( "URL fragment `#{fragment}` in git URL is ignored for dependency ({name_in_toml}). \ @@ -2182,6 +2185,27 @@ fn to_dependency_source_id( } } +/// Checks if the URL is a GitHub pull request URL. +/// +/// If the URL is a GitHub pull request URL, an error is returned with a message that explains +/// how to specify a specific git revision. +fn bail_if_github_pull_request(name_in_toml: &str, url: &Url) -> CargoResult<()> { + if url.host_str() != Some("github.com") { + return Ok(()); + } + let path_components = url.path().split('/').collect::>(); + if let ["", owner, repo, "pull", pr_number, ..] = path_components[..] { + let repo_url = format!("https://github.com/{owner}/{repo}.git"); + let rev = format!("refs/pull/{pr_number}/head"); + bail!( + "dependency ({name_in_toml}) git url {url} is not a repository. \ + The path looks like a pull request. Try replacing the dependency with: \ + `git = \"{repo_url}\" rev = \"{rev}\"` in the dependency declaration.", + ); + } + Ok(()) +} + pub(crate) fn lookup_path_base<'a>( base: &PathBaseName, gctx: &GlobalContext, diff --git a/tests/testsuite/bad_config.rs b/tests/testsuite/bad_config.rs index 0fb00f8e8fa..80f69e543a9 100644 --- a/tests/testsuite/bad_config.rs +++ b/tests/testsuite/bad_config.rs @@ -2151,6 +2151,37 @@ Caused by: .run(); } +#[cargo_test] +fn github_pull_request_url() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.0" + edition = "2015" + authors = [] + + [dependencies.bar] + git = "https://github.com/foo/bar/pull/123" + "#, + ) + .file("src/lib.rs", "") + .build(); + + p.cargo("check -v") + .with_status(101) + .with_stderr_data(str![[r#" +[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml` + +Caused by: + dependency (bar) git url https://github.com/foo/bar/pull/123 is not a repository. The path looks like a pull request. Try replacing the dependency with: `git = "https://github.com/foo/bar.git" rev = "refs/pull/123/head"` in the dependency declaration. + +"#]]) + .run(); +} + #[cargo_test] fn fragment_in_git_url() { let p = project() diff --git a/tests/testsuite/patch.rs b/tests/testsuite/patch.rs index ff46b324091..99488e1f38c 100644 --- a/tests/testsuite/patch.rs +++ b/tests/testsuite/patch.rs @@ -357,6 +357,50 @@ fn patch_to_git() { .run(); } +#[cargo_test] +fn patch_to_git_pull_request() { + let bar = git::repo(&paths::root().join("override")) + .file("Cargo.toml", &basic_manifest("bar", "0.1.0")) + .file("src/lib.rs", "pub fn bar() {}") + .build(); + + Package::new("bar", "0.1.0").publish(); + + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + edition = "2015" + authors = [] + + [dependencies] + bar = "0.1" + + [patch.crates-io] + bar = { git = 'https://github.com/foo/bar/pull/123' } + "#, + ) + .file( + "src/lib.rs", + "extern crate bar; pub fn foo() { bar::bar(); }", + ) + .build(); + + p.cargo("check -v") + .with_status(101) + .with_stderr_data(str![[r#" +[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml` + +Caused by: + dependency (bar) git url https://github.com/foo/bar/pull/123 is not a repository. The path looks like a pull request. Try replacing the dependency with: `git = "https://github.com/foo/bar.git" rev = "refs/pull/123/head"` in the dependency declaration. + +"#]]) + .run(); +} + #[cargo_test] fn unused() { Package::new("bar", "0.1.0").publish();