-
Notifications
You must be signed in to change notification settings - Fork 504
Description
When using crate_universe with bzlmod, settings like crate.annotation.patches
and crate.render_config.default_alias_rule_bzl
cause a bazel label to be included in the cargo-bazel
config file. These labels differ based on whether or not the corresponding files are in the root module or not (e.g. @@//some:path
vs @@module+//some:path
). This means that it's not possible for a module using these options and a lockfile to be used as a non-root module.
I'm happy to help with a fix, but I'm not sure if there's a good way to canonicalize these labels before including them in the workspace config.
Tested with bazel 7.5.0 and 8.3.1.
Workaround
It's possible (but inconvenient) to work around this issue by always placing patch files in a non-root module.
Repro
Here's a small repro using two bazel modules. bazel build @module1//:main
works from module1 but fails from module2.
Files
-
module1/MODULE.bazel
:module(name = "module1") bazel_dep(name = "rules_rust", version = "0.62.0") crate = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate") crate.spec( package = "getrandom", version = "0.3.3", ) crate.annotation( crate = "getrandom", patches = ["//:getrandom.patch"], ) crate.from_specs( cargo_lockfile = "//:Cargo.Bazel.lock", lockfile = "//:Cargo.Bazel.lock.json", ) use_repo(crate, "crates")
-
module1/BUILD
:load("@rules_rust//rust:defs.bzl", "rust_binary") rust_binary( name = "main", srcs = ["main.rs"], deps = ["@crates//:getrandom"], )
-
module1/Cargo.Bazel.lock
(populated byCARGO_BAZEL_REPIN=1 bazel build @module1//:main
-
module1/Cargo.Bazel.lock.json
(populated byCARGO_BAZEL_REPIN=1 bazel build @module1//:main
-
module1/getrandom.patch
(empty) -
module1/main
:fn main() { println!("{:?}", getrandom::u32()); }
-
module2/MODULE.bazel
:bazel_dep(name = "module1") local_path_override( module_name = "module1", path = "../module1", )
-
module2/BUILD
(empty)
bazel output
When running bazel build @module1//:main
from the module2
directory, the build fails because the lockfile checksum doesn't match:
Error: Digests do not match: Current Digest("043fdcac42d609e525d781f8da6fabbab6653c7e84360dff2600569a69667bdf") != Expected Digest("d32921dd473d82af6868c4cb09a725b464f361fc6da9374c3d2ea12bf79c856c")
The current `lockfile` is out of date for 'crates'. Please re-run bazel using `CARGO_BAZEL_REPIN=true` if this is expected and the lockfile should be updated.
Running with CARGO_BAZEL_DEBUG=TRACE
shows that the workspace config causes the mismatch:
Query 2025-07-22T22:48:31.251008Z DEBUG cargo_bazel::lockfile: cargo-bazel version hash: 55602991fd6053e2fc7275b186f7b15a49e1a1edaeae8cf44cf8275b84e3f195
Query 2025-07-22T22:48:31.251438Z DEBUG cargo_bazel::lockfile: lockfile context hash: 183ca49156e5d4021437f10ff841092ce90e60b15483e48a27509fa184c7d99d
Query 2025-07-22T22:48:31.251521Z DEBUG cargo_bazel::lockfile: workspace config hash: bcc0b266b9b769419389e3a0d40d9a4874a3196a942daf2b3f725de8a0d859d1
Query 2025-07-22T22:48:31.251542Z DEBUG cargo_bazel::lockfile: splicing manifest hash: 4e412256d7c23c08e640550035d1c4e2e62a97437fae03e6a85b87b8a0a07f2c
Query 2025-07-22T22:48:31.251550Z DEBUG cargo_bazel::lockfile: Cargo version hash: d5ccaf216b98d1cf733fb6e9a6092a6347f51cd513e41056f51969516614925c
Query 2025-07-22T22:48:31.251557Z DEBUG cargo_bazel::lockfile: Rustc version hash: d391e0b7f24c689e39f6ed42b02608fbdfb7aacb4b5f24deec1634d139361dd2
Query 2025-07-22T22:48:31.251564Z DEBUG cargo_bazel::lockfile: Digest hash: d32921dd473d82af6868c4cb09a725b464f361fc6da9374c3d2ea12bf79c856c
vs
Query 2025-07-22T22:50:22.643600Z DEBUG cargo_bazel::lockfile: cargo-bazel version hash: 55602991fd6053e2fc7275b186f7b15a49e1a1edaeae8cf44cf8275b84e3f195
Query 2025-07-22T22:50:22.643981Z DEBUG cargo_bazel::lockfile: lockfile context hash: 183ca49156e5d4021437f10ff841092ce90e60b15483e48a27509fa184c7d99d
Query 2025-07-22T22:50:22.644043Z DEBUG cargo_bazel::lockfile: workspace config hash: 4f5101f3330e245e15bd62823873b18695969ec8fc1ec4e558455ea929007d7d
Query 2025-07-22T22:50:22.644064Z DEBUG cargo_bazel::lockfile: splicing manifest hash: 4e412256d7c23c08e640550035d1c4e2e62a97437fae03e6a85b87b8a0a07f2c
Query 2025-07-22T22:50:22.644072Z DEBUG cargo_bazel::lockfile: Cargo version hash: d5ccaf216b98d1cf733fb6e9a6092a6347f51cd513e41056f51969516614925c
Query 2025-07-22T22:50:22.644078Z DEBUG cargo_bazel::lockfile: Rustc version hash: d391e0b7f24c689e39f6ed42b02608fbdfb7aacb4b5f24deec1634d139361dd2
Query 2025-07-22T22:50:22.644085Z DEBUG cargo_bazel::lockfile: Digest hash: 043fdcac42d609e525d781f8da6fabbab6653c7e84360dff2600569a69667bdf
The cargo-bazel config for module1 uses @@//:getrandom.patch.
{
"annotations": {
"getrandom *": {
"alias_rule": null,
"build_script_compile_data": null,
"build_script_data": null,
"build_script_data_glob": null,
"build_script_deps": null,
"build_script_env": null,
"build_script_link_deps": null,
"build_script_proc_macro_deps": null,
"build_script_rundir": null,
"build_script_rustc_env": null,
"build_script_toolchains": null,
"build_script_tools": null,
"build_script_use_default_shell_env": null,
"compile_data": null,
"compile_data_glob": null,
"compile_data_glob_excludes": null,
"crate_features": null,
"data": null,
"data_glob": null,
"deps": null,
"disable_pipelining": false,
"extra_aliased_targets": null,
"gen_binaries": null,
"gen_build_script": null,
"override_targets": null,
"patch_args": null,
"patch_tool": null,
"patches": [
"@@//:getrandom.patch"
],
"proc_macro_deps": null,
"rustc_env": null,
"rustc_env_files": null,
"rustc_flags": null,
"shallow_since": null
}
},
"cargo_config": null,
"generate_binaries": false,
"generate_build_scripts": true,
"rendering": {
"build_file_template": "@crates//crates:BUILD.{name}-{version}.bazel",
"crate_alias_template": "@{repository}//:{name}-{version}",
"crate_label_template": "@{repository}__{name}-{version}//:{target}",
"crate_repository_template": "{repository}__{name}-{version}",
"crates_module_template": "@crates//crates:{file}",
"default_alias_rule": "alias",
"default_package_name": null,
"generate_cargo_toml_env_vars": true,
"generate_rules_license_metadata": false,
"generate_target_compatible_with": true,
"platforms_template": "@rules_rust//rust/platform:{triple}",
"regen_command": "bazel mod show_repo 'module1'",
"repository_name": "crates",
"vendor_mode": "remote"
},
"supported_platform_triples": [
"aarch64-apple-darwin",
"aarch64-unknown-linux-gnu",
"wasm32-unknown-unknown",
"wasm32-wasip1",
"x86_64-pc-windows-msvc",
"x86_64-unknown-linux-gnu",
"x86_64-unknown-nixos-gnu"
]
}
The cargo-bazel config for module2 uses @@module1+//:getrandom.patch.
{
"annotations": {
"getrandom *": {
"alias_rule": null,
"build_script_compile_data": null,
"build_script_data": null,
"build_script_data_glob": null,
"build_script_deps": null,
"build_script_env": null,
"build_script_link_deps": null,
"build_script_proc_macro_deps": null,
"build_script_rundir": null,
"build_script_rustc_env": null,
"build_script_toolchains": null,
"build_script_tools": null,
"build_script_use_default_shell_env": null,
"compile_data": null,
"compile_data_glob": null,
"compile_data_glob_excludes": null,
"crate_features": null,
"data": null,
"data_glob": null,
"deps": null,
"disable_pipelining": false,
"extra_aliased_targets": null,
"gen_binaries": null,
"gen_build_script": null,
"override_targets": null,
"patch_args": null,
"patch_tool": null,
"patches": [
"@@module1+//:getrandom.patch"
],
"proc_macro_deps": null,
"rustc_env": null,
"rustc_env_files": null,
"rustc_flags": null,
"shallow_since": null
}
},
"cargo_config": null,
"generate_binaries": false,
"generate_build_scripts": true,
"rendering": {
"build_file_template": "@crates//crates:BUILD.{name}-{version}.bazel",
"crate_alias_template": "@{repository}//:{name}-{version}",
"crate_label_template": "@{repository}__{name}-{version}//:{target}",
"crate_repository_template": "{repository}__{name}-{version}",
"crates_module_template": "@crates//crates:{file}",
"default_alias_rule": "alias",
"default_package_name": null,
"generate_cargo_toml_env_vars": true,
"generate_rules_license_metadata": false,
"generate_target_compatible_with": true,
"platforms_template": "@rules_rust//rust/platform:{triple}",
"regen_command": "bazel mod show_repo 'module1'",
"repository_name": "crates",
"vendor_mode": "remote"
},
"supported_platform_triples": [
"aarch64-apple-darwin",
"aarch64-unknown-linux-gnu",
"wasm32-unknown-unknown",
"wasm32-wasip1",
"x86_64-pc-windows-msvc",
"x86_64-unknown-linux-gnu",
"x86_64-unknown-nixos-gnu"
]
}