Skip to content

Commit 17d6d64

Browse files
joshkamotorailgun
authored andcommitted
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 0f14d9d commit 17d6d64

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
@@ -2389,6 +2389,8 @@ fn to_dependency_source_id<P: ResolveToPath + Clone>(
23892389
.unwrap_or(GitReference::DefaultBranch);
23902390
let loc = git.into_url()?;
23912391

2392+
bail_if_github_pull_request(&name_in_toml, &loc)?;
2393+
23922394
if let Some(fragment) = loc.fragment() {
23932395
let msg = format!(
23942396
"URL fragment `#{fragment}` in git URL is ignored for dependency ({name_in_toml}). \
@@ -2427,6 +2429,26 @@ fn to_dependency_source_id<P: ResolveToPath + Clone>(
24272429
}
24282430
}
24292431

2432+
/// Checks if the URL is a GitHub pull request URL.
2433+
///
2434+
/// If the URL is a GitHub pull request URL, an error is returned with a message that explains
2435+
/// how to specify a specific git revision.
2436+
fn bail_if_github_pull_request(name_in_toml: &str, url: &Url) -> CargoResult<()> {
2437+
if url.host_str() != Some("github.com") {
2438+
return Ok(());
2439+
}
2440+
let path_components = url.path().split('/').collect::<Vec<_>>();
2441+
if let ["", owner, repo, "pull", pr_number, ..] = path_components[..] {
2442+
bail!(
2443+
"dependency ({name_in_toml}) specifies a GitHub pull request link. \
2444+
If you were trying to specify a specific github PR, replace the URL with the git \
2445+
URL (e.g. `git = \"https://github.com/{owner}/{repo}.git\"`) \
2446+
and add `rev = \"refs/pull/{pr_number}/head\"` in the dependency declaration.",
2447+
);
2448+
}
2449+
Ok(())
2450+
}
2451+
24302452
pub(crate) fn lookup_path_base<'a>(
24312453
base: &PathBaseName,
24322454
gctx: &GlobalContext,

tests/testsuite/bad_config.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,6 +2259,37 @@ Caused by:
22592259
.run();
22602260
}
22612261

2262+
#[cargo_test]
2263+
fn github_pull_request_url() {
2264+
let p = project()
2265+
.file(
2266+
"Cargo.toml",
2267+
r#"
2268+
[package]
2269+
name = "foo"
2270+
version = "0.0.0"
2271+
edition = "2015"
2272+
authors = []
2273+
2274+
[dependencies.bar]
2275+
git = "https://github.com/foo/bar/pull/123"
2276+
"#,
2277+
)
2278+
.file("src/lib.rs", "")
2279+
.build();
2280+
2281+
p.cargo("check -v")
2282+
.with_status(101)
2283+
.with_stderr_data(str![[r#"
2284+
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
2285+
2286+
Caused by:
2287+
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.
2288+
2289+
"#]])
2290+
.run();
2291+
}
2292+
22622293
#[cargo_test]
22632294
fn fragment_in_git_url() {
22642295
let p = project()

0 commit comments

Comments
 (0)