-
Notifications
You must be signed in to change notification settings - Fork 564
Description
Problem
The cargo_build_script macro does not respect target_compatible_with when build_manual_tests is enabled.
Description
In our workspace we have build --build_manual_tests set in our .bazelrc. We have a Rust target that is Linux only, that depends on files generated via a build script. This build script is also only supported on Linux.
Adding target_compatible_with = ["@platforms//os:linux"] to the main rust_binary target works to skip building the binary on non-Linux platforms (macOS in my case), but it does not work for the cargo_build_script rule. That is, the cargo_build_script target still runs (and fails) on macOS.
As far as I can tell what is happening is that the cargo_build_script rule creates its own rust_binary, but target_compatible_with is not forwarded to that binary. I've applied the following patch which seems to resolve the issue, though I'm not sure if it's the proper fix:
diff --git a/cargo/private/cargo_build_script_wrapper.bzl b/cargo/private/cargo_build_script_wrapper.bzl
index a7295b80d..38174f091 100644
--- a/cargo/private/cargo_build_script_wrapper.bzl
+++ b/cargo/private/cargo_build_script_wrapper.bzl
@@ -151,7 +151,7 @@ def cargo_build_script(
rustc_env["CARGO_CRATE_NAME"] = name_to_crate_name(name_to_pkg_name(name))
script_kwargs = {}
- for arg in ("exec_compatible_with", "testonly"):
+ for arg in ("exec_compatible_with", "testonly", "target_compatible_with"):
if arg in kwargs:
script_kwargs[arg] = kwargs[arg]
I also tried setting exec_compatible_with on the cargo_build_script rule, but that fails for a different reason (Unable to find an execution platform for target platform @@platforms//host:host from available execution platforms []).
Below is a simplified version of our BUILD file. It's not an exact reproducer of course since it doesn't include a build.rs file. I can work on putting together a full reproduction, but first wanted to make sure that I wasn't missing something obvious about how to do this correctly, or if the patch above is indeed necessary.
Thanks!
cargo_build_script(
name = "my_build_script",
srcs = [
"build.rs",
],
target_compatible_with = ["@platforms//os:linux"],
visibility = ["//visibility:private"],
deps = all_crate_deps(
build = True,
),
)
rust_binary(
name = "my_binary",
srcs = [
# ...
],
aliases = aliases(),
proc_macro_deps = all_crate_deps(
proc_macro = True,
),
stamp = True,
target_compatible_with = ["@platforms//os:linux"],
visibility = ["//visibility:public"],
deps = [
":my_build_script",
] + all_crate_deps(
build = True,
normal = True,
),
)