Skip to content

Commit

Permalink
fix: tweak message and add test for patch
Browse files Browse the repository at this point in the history
  • Loading branch information
joshka committed Jan 3, 2025
1 parent 1a7720c commit d743987
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -2194,11 +2195,12 @@ fn bail_if_github_pull_request(name_in_toml: &str, url: &Url) -> CargoResult<()>
}
let path_components = url.path().split('/').collect::<Vec<_>>();
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}) 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/{owner}/{repo}.git\"`) \
and add `rev = \"refs/pull/{pr_number}/head\"` in the dependency declaration.",
"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(())
Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/bad_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2176,7 +2176,7 @@ fn github_pull_request_url() {
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
Caused by:
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.
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();
Expand Down
44 changes: 44 additions & 0 deletions tests/testsuite/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit d743987

Please sign in to comment.