Skip to content
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

Patching support for crate universe local vendoring #1902

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 27 additions & 52 deletions crate_universe/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crate_universe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ anyhow = "1.0.75"
camino = "1.1.6"
cargo_metadata = "0.18.1"
cargo_toml = "0.19.2"
cargo-lock = "9.0.0"
cargo-lock = "10.0.0"
cargo-platform = "0.1.4"
cfg-expr = "0.15.5"
clap = { version = "4.3.11", features = ["derive", "env"] }
Expand Down
25 changes: 24 additions & 1 deletion crate_universe/private/crates_vendor.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -498,12 +498,35 @@ directory next to where the target is defined. To run it, simply call:
bazel run //3rdparty:crates_vendor
```

### Patching

If you need to replace crates in your third_party dependency tree with forks, the `patch` section can be used in your cargo manifest:

```toml
[patch.crates-io]
fuse3 = { path = "forks/fuse3" }
```

A build file for the patched crate will still be generated under the vendor path
(such as `crates/fuse3-0.6.1/BUILD.bazel`), but `cargo vendor` will NOT download the
crate to that location, and the generated BUILD file will point to your copy. For this
to work, you need to define the following filegroups in your patch crate's BUILD.bazel:
`:srcs`, `:crate_root`, `:compile_data`, and (if the crate has a build script)
`:build_script_crate_root`. See the
[patching example](https://github.com/bazelbuild/rules_rust/tree/main/examples/crate_universe/vendor_local_patching)
for an example of what those BUILD files look like.

This feature can also be used to avoid vendoring source code for crates that you never
actually use for the platforms you're targeting. In that case you don't need an extra
BUILD file, just crate a empty/stub Cargo.toml and `lib.rs`, and list the path to that
in your `patch` section.

<a id="#crates_vendor_repinning_updating_dependencies"></a>

### Repinning / Updating Dependencies

Repinning dependencies is controlled by both the `CARGO_BAZEL_REPIN` environment variable or the `--repin`
flag to the `crates_vendor` binary. To update dependencies, simply add the flag ro your `bazel run` invocation.
flag to the `crates_vendor` binary. To update dependencies, simply add the flag to your `bazel run` invocation.

```shell
bazel run //3rdparty:crates_vendor -- --repin
Expand Down
1 change: 1 addition & 0 deletions crate_universe/src/cli/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ pub fn vendor(opt: VendorOptions) -> Result<()> {
// Generate a splicer for creating a Cargo workspace manifest
let splicer = Splicer::new(PathBuf::from(temp_dir.as_ref()), splicing_manifest)
.context("Failed to create splicer")?;
// std::mem::forget(temp_dir);

let cargo = Cargo::new(opt.cargo, opt.rustc.clone());

Expand Down
2 changes: 1 addition & 1 deletion crate_universe/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Context {
.map(|annotation| {
let context = CrateContext::new(
annotation,
&annotations.metadata.packages,
&annotations.metadata,
&annotations.lockfile.crates,
&annotations.pairred_extras,
&annotations.metadata.workspace_metadata.tree_metadata,
Expand Down
Loading