Skip to content

Commit

Permalink
Add patching example and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
P1n3appl3 committed Mar 31, 2023
1 parent ae674a8 commit 555a897
Show file tree
Hide file tree
Showing 25 changed files with 1,889 additions and 444 deletions.
25 changes: 24 additions & 1 deletion crate_universe/private/crates_vendor.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,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
2 changes: 1 addition & 1 deletion crate_universe/src/context/crate_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ fn get_attributes(
// TODO: remove once added to help-docs
println!("\nThere's a patch crate at '//{pkg}'.");
println!("Make sure that '//{pkg}/BUILD.bazel' exposes the following filegroups:");
println!("'crate_root', 'srcs', 'compile_data', and (if necessary) 'build_script'");
println!("'crate_root', 'srcs', 'compile_data', and (if necessary) 'build_script_crate_root'");
let srcs = GlobOrLabels::Labels(vec![Label {
repository: None,
package: Some(pkg.clone()),
Expand Down
25 changes: 24 additions & 1 deletion docs/crate_universe.md
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,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.

&lt;a id="#crates_vendor_repinning_updating_dependencies"&gt;&lt;/a&gt;

### 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
Loading

0 comments on commit 555a897

Please sign in to comment.