Skip to content

Allow same source git patches with different rev / branch, etc. #10756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
0xdapper opened this issue Jun 14, 2022 · 7 comments
Closed

Allow same source git patches with different rev / branch, etc. #10756

0xdapper opened this issue Jun 14, 2022 · 7 comments
Labels
A-patch Area: [patch] table override C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`

Comments

@0xdapper
Copy link

Problem

Currently if I wanted to override and patch dependency across workspace of a dependency https://github.com/owner/repo to branch b this does not work

[patch.'https://github.com/owner/repo']
baz = { git = 'https://github.com/owner/repo', branch = 'my-branch' }

because of the input validation here

if *summary.package_id().source_id().canonical_url() == canonical {
return Err(anyhow::anyhow!(
"patch for `{}` in `{}` points to the same source, but \
patches must point to different sources",
dep.package_name(),
url
))
.context(format!("failed to resolve patches for `{}`", url));
}

Proposed Solution

Remove the validation or make an exception when the source is a git url?

Notes

No response

@0xdapper 0xdapper added the C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` label Jun 14, 2022
@weihanglo
Copy link
Member

Semi-related: #10667

Source replacement for git URL doesn't support rev and branch.

@fralalonde
Copy link

Bumping this up, this is a blocker for private project development where forks are not an option. Patching a project to override a crate dependency with a different branch of the same repo would be the way to go but is impossible right now because of this URL check.

A solution would be to compare the whole package specification (including any tag, branch, commit clause) and block only if every field is the same.

I could start work on a PR for it if this solution makes sense and the interest is there.

@dimonomid
Copy link

I definitely agree that it should be supported in cargo properly, but just to mention a workaround in case it's useful to anyone: it's possible to trick cargo into thinking that it's a different repo by using a double slash in the URL, like this:

[patch.'https://github.com/owner/repo']
baz = { git = 'https://github.com/owner//repo', branch = 'my-branch' }

Cloning https://github.com/owner//repo is the same as cloning https://github.com/owner/repo, so it works.

@PixelDust22
Copy link

@dimonomid I have tried this on Windows and I would like to report that it did not work. Maybe this is a linux-only workaround?

@dimonomid
Copy link

I'm not a Windows expert and don't have it around to try it right now, but I didn't expect that it wouldn't work. Could you share what is the error message?

github-merge-queue bot pushed a commit to hasura/ndc-postgres that referenced this issue Oct 26, 2023
### What

ndc-spec v0.1.0-rc.8 uses workspace inheritance to set the version and
edition, which unfortunately does not work with Crane for Nix (when
using Git targets).

I have inlined the version and edition to solve this, but we also need
to release a new version of ndc-spec and update ndc-sdk.

Until that happens, let's just override it (transitively) using `patch`
in _Cargo.toml_.

### How

[`[patch."..."]`, in
_Cargo.toml_](https://doc.rust-lang.org/cargo/reference/overriding-dependencies.html#the-patch-section),
allows us to hijack a dependency and point it somewhere else. Unlike
just setting the version, this works transitively, so it will also
update ndc-sdk. This is important because it's the dependency build step
which fails in Crane, so we need to ensure we have no reference to the
previous version.

Unfortunately, there's an open bug in Cargo which means that [patch
overrides can't use the same Git
repository](rust-lang/cargo#10756), even if
the commit reference is different. We work around this by adding an
extra "/" to trick Cargo into thinking it's a different repository.
damonbarry added a commit to damonbarry/iotedge that referenced this issue Dec 26, 2023
lilyinstarlight referenced this issue in pop-os/cosmic-bg Aug 28, 2024
lilyinstarlight added a commit to lilyinstarlight/cosmic-bg that referenced this issue Aug 31, 2024
This uses a URL that still follows the Cargo.toml specification, but
still works around <rust-lang/cargo#10756>.

This fixes Cargo recording valid URLs in the lockfile and correctly
finding the dependency when using `cargo vendor` (otherwise `cargo
--offline` will not work with vendored sources).
lilyinstarlight added a commit to lilyinstarlight/cosmic-bg that referenced this issue Aug 31, 2024
This fixes Cargo recording valid URLs in the lockfile and correctly
finding the dependency when using `cargo vendor` (otherwise `cargo build
--offline` will not work with vendored sources).

It uses a URL that follows the Cargo.toml specification, but still works
around <rust-lang/cargo#10756>.
lilyinstarlight added a commit to lilyinstarlight/cosmic-bg that referenced this issue Aug 31, 2024
This fixes Cargo recording valid URLs in the lockfile and correctly
finding the dependency when using `cargo vendor` (otherwise `cargo build
--offline` will not work with vendored sources).

It uses a URL that follows the Cargo.toml specification, but still works
around <rust-lang/cargo#10756>.
mmstick pushed a commit to pop-os/cosmic-bg that referenced this issue Aug 31, 2024
This fixes Cargo recording valid URLs in the lockfile and correctly
finding the dependency when using `cargo vendor` (otherwise `cargo build
--offline` will not work with vendored sources).

It uses a URL that follows the Cargo.toml specification, but still works
around <rust-lang/cargo#10756>.
@juzpermsky
Copy link

juzpermsky commented Sep 28, 2024

@dimonomid I have tried this on Windows and I would like to report that it did not work. Maybe this is a linux-only workaround?

That's worked for me on windows, also with ssh paths.
Corrected workaround, with specifying ssh/https port in patching path:

[patch.'https://github.com/owner/repo']
baz = { git = 'https://github.com:443/owner/repo', branch = 'my-branch' }

@weihanglo
Copy link
Member

Just found this is exactly the same issue as #5478. Close in favor of that.

@weihanglo weihanglo closed this as not planned Won't fix, can't repro, duplicate, stale Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-patch Area: [patch] table override C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`
Projects
None yet
Development

No branches or pull requests

7 participants