Skip to content

Commit 1a7720c

Browse files
committed
fix: add error message for github PR url in dep
Prior to this, using a github PR URL would cause cargo to attempt to fetch from an incorrect URL several times before failing. Providing a github pull request url now fails with an error message that shows how to fix the problem. E.g.: ```toml bar = { git = "https://github.com/foo/bar/pull/123" } ``` Now gives the following error message: ``` dependency (bar) specifies a GitHub pull request link. If you were trying to specify a specific github PR, replace the URL with the git URL (e.g. `git = "https://github.com/foo/bar.git"`) and add `rev = "refs/pull/123/head"` in the dependency declaration. ``` Fixes: #15001
1 parent 0200aa0 commit 1a7720c

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/cargo/util/toml/mod.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,6 +2144,8 @@ fn to_dependency_source_id<P: ResolveToPath + Clone>(
21442144
.unwrap_or(GitReference::DefaultBranch);
21452145
let loc = git.into_url()?;
21462146

2147+
bail_if_github_pull_request(&name_in_toml, &loc)?;
2148+
21472149
if let Some(fragment) = loc.fragment() {
21482150
let msg = format!(
21492151
"URL fragment `#{fragment}` in git URL is ignored for dependency ({name_in_toml}). \
@@ -2182,6 +2184,26 @@ fn to_dependency_source_id<P: ResolveToPath + Clone>(
21822184
}
21832185
}
21842186

2187+
/// Checks if the URL is a GitHub pull request URL.
2188+
///
2189+
/// If the URL is a GitHub pull request URL, an error is returned with a message that explains
2190+
/// how to specify a specific git revision.
2191+
fn bail_if_github_pull_request(name_in_toml: &str, url: &Url) -> CargoResult<()> {
2192+
if url.host_str() != Some("github.com") {
2193+
return Ok(());
2194+
}
2195+
let path_components = url.path().split('/').collect::<Vec<_>>();
2196+
if let ["", owner, repo, "pull", pr_number, ..] = path_components[..] {
2197+
bail!(
2198+
"dependency ({name_in_toml}) specifies a GitHub pull request link. \
2199+
If you were trying to specify a specific github PR, replace the URL with the git \
2200+
URL (e.g. `git = \"https://github.com/{owner}/{repo}.git\"`) \
2201+
and add `rev = \"refs/pull/{pr_number}/head\"` in the dependency declaration.",
2202+
);
2203+
}
2204+
Ok(())
2205+
}
2206+
21852207
pub(crate) fn lookup_path_base<'a>(
21862208
base: &PathBaseName,
21872209
gctx: &GlobalContext,

tests/testsuite/bad_config.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,6 +2151,37 @@ Caused by:
21512151
.run();
21522152
}
21532153

2154+
#[cargo_test]
2155+
fn github_pull_request_url() {
2156+
let p = project()
2157+
.file(
2158+
"Cargo.toml",
2159+
r#"
2160+
[package]
2161+
name = "foo"
2162+
version = "0.0.0"
2163+
edition = "2015"
2164+
authors = []
2165+
2166+
[dependencies.bar]
2167+
git = "https://github.com/foo/bar/pull/123"
2168+
"#,
2169+
)
2170+
.file("src/lib.rs", "")
2171+
.build();
2172+
2173+
p.cargo("check -v")
2174+
.with_status(101)
2175+
.with_stderr_data(str![[r#"
2176+
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
2177+
2178+
Caused by:
2179+
dependency (bar) specifies a GitHub pull request link. If you were trying to specify a specific github PR, replace the URL with the git URL (e.g. `git = "https://github.com/foo/bar.git"`) and add `rev = "refs/pull/123/head"` in the dependency declaration.
2180+
2181+
"#]])
2182+
.run();
2183+
}
2184+
21542185
#[cargo_test]
21552186
fn fragment_in_git_url() {
21562187
let p = project()

0 commit comments

Comments
 (0)