From 426a64a1b162f492c09725ac34d9fcaa013f1f93 Mon Sep 17 00:00:00 2001 From: Randolf Jung Date: Wed, 23 Sep 2026 22:16:49 -0700 Subject: [PATCH 1/2] cargo: honor build script execution constraints Apply exec_compatible_with to both the build-script compiler action and the action that runs the script. A regression guard checks the macro expansion for both stages. Assisted-by: OpenAI Codex --- cargo/private/cargo_build_script_wrapper.bzl | 2 ++ .../compatible_with/BUILD.bazel | 16 ++++++++++++++++ .../compatible_with/exec_constraints_test.bzl | 14 ++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 cargo/tests/cargo_build_script/compatible_with/exec_constraints_test.bzl diff --git a/cargo/private/cargo_build_script_wrapper.bzl b/cargo/private/cargo_build_script_wrapper.bzl index 95cec14f93..2c7e040eef 100644 --- a/cargo/private/cargo_build_script_wrapper.bzl +++ b/cargo/private/cargo_build_script_wrapper.bzl @@ -200,6 +200,7 @@ def cargo_build_script( # it is translated to `target_compatible_with` to match the `cfg = "exec"` # consumption of the target. script_kwargs["target_compatible_with"] = exec_compatible_with + script_kwargs["exec_compatible_with"] = exec_compatible_with binary_tags = depset( (tags if tags else []) + ["manual"], @@ -241,6 +242,7 @@ def cargo_build_script( # This target executes the build script. _build_script_run( name = name, + exec_compatible_with = exec_compatible_with, script = ":{}_".format(name), data_runfiles = ":{}-".format(name), data = data, diff --git a/cargo/tests/cargo_build_script/compatible_with/BUILD.bazel b/cargo/tests/cargo_build_script/compatible_with/BUILD.bazel index c3b5b9f733..65a6c9ad38 100644 --- a/cargo/tests/cargo_build_script/compatible_with/BUILD.bazel +++ b/cargo/tests/cargo_build_script/compatible_with/BUILD.bazel @@ -1,5 +1,6 @@ load("//cargo:defs.bzl", "cargo_build_script") load("//rust:defs.bzl", "rust_library", "rust_test") +load(":exec_constraints_test.bzl", "exec_constraints_test") environment_group( name = "foo_environment", @@ -39,3 +40,18 @@ rust_test( edition = "2018", deps = [":empty_build_script"], ) + +cargo_build_script( + name = "exec_constrained_build_script", + testonly = True, + srcs = ["do_nothing.rs"], + edition = "2018", + exec_compatible_with = ["@platforms//os:linux"], + tags = ["manual"], +) + +exec_constraints_test( + name = "exec_constraints_test", + build_script = "exec_constrained_build_script", + constraint = "@platforms//os:linux", +) diff --git a/cargo/tests/cargo_build_script/compatible_with/exec_constraints_test.bzl b/cargo/tests/cargo_build_script/compatible_with/exec_constraints_test.bzl new file mode 100644 index 0000000000..5a99f698b9 --- /dev/null +++ b/cargo/tests/cargo_build_script/compatible_with/exec_constraints_test.bzl @@ -0,0 +1,14 @@ +"""Check that a Cargo build script constrains both execution stages.""" + +def exec_constraints_test(name, build_script, constraint): + """Assert macro expansion forwards the execution constraint to both actions.""" + expected = str(Label(constraint)) + for target in [build_script + "_", build_script]: + rule = native.existing_rule(target) + if rule == None: + fail("missing build script target " + target) + actual = rule.get("exec_compatible_with", []) + if len(actual) != 1 or str(actual[0]) != expected: + fail("{} must execute on {}, got {}".format(target, expected, actual)) + + native.test_suite(name = name, tests = [":test_compatible_with"]) From 94f6ca8fbbc50d9ce800ca401b7aa045c15db999 Mon Sep 17 00:00:00 2001 From: Randolf Jung Date: Thu, 24 Sep 2026 01:57:45 -0700 Subject: [PATCH 2/2] test: document execution constraint assertion arguments --- .../compatible_with/exec_constraints_test.bzl | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/cargo/tests/cargo_build_script/compatible_with/exec_constraints_test.bzl b/cargo/tests/cargo_build_script/compatible_with/exec_constraints_test.bzl index 5a99f698b9..92db74bed3 100644 --- a/cargo/tests/cargo_build_script/compatible_with/exec_constraints_test.bzl +++ b/cargo/tests/cargo_build_script/compatible_with/exec_constraints_test.bzl @@ -1,7 +1,13 @@ """Check that a Cargo build script constrains both execution stages.""" def exec_constraints_test(name, build_script, constraint): - """Assert macro expansion forwards the execution constraint to both actions.""" + """Assert macro expansion forwards the execution constraint to both actions. + + Args: + name: Name of the generated test suite. + build_script: Build script target to inspect. + constraint: Expected execution constraint label. + """ expected = str(Label(constraint)) for target in [build_script + "_", build_script]: rule = native.existing_rule(target)