rust-lang / cargo

The Rust package manager
https://doc.rust-lang.org/cargo
Apache License 2.0
12.7k stars 2.41k forks source link

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

Open 0xdapper opened 2 years ago

0xdapper commented 2 years ago

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 https://github.com/rust-lang/cargo/blob/13662250abdd813755263e0de5623bd314a443db/src/cargo/core/registry.rs#L363-L371

Proposed Solution

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

Notes

No response

weihanglo commented 2 years ago

Semi-related: https://github.com/rust-lang/cargo/issues/10667

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

fralalonde commented 1 year ago

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 commented 1 year ago

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.

Neo-Zhixing commented 1 year ago

@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 commented 1 year ago

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?

juzpermsky commented 3 weeks ago

@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' }