Skip to content

Let use_target_platform_constraints copy unmatched constraints - #30688

Open
moroten wants to merge 1 commit into
bazelbuild:masterfrom
meroton:copy-constraints-in-test-toolchains
Open

Let use_target_platform_constraints copy unmatched constraints#30688
moroten wants to merge 1 commit into
bazelbuild:masterfrom
meroton:copy-constraints-in-test-toolchains

Conversation

@moroten

@moroten moroten commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Description

Instead of requiring empty target_compatible_with and exec_compatible_with, use the following backwards compatible logic:

exec_constraints = toolchain.exec_compatible_with
if toolchain.use_target_platform_constraints:
    unmatched_constraints = target_platform.constraints - toolchain.target_compatible_with
    exec_constraints += unmatched_constraints

Motivation

This is useful for the following test toolchain, where building a test for a target platform with wasm+gpu constraints should not execute the test on a platform without gpu. In this example, the execution requirements will be linux+x86_64+gpu.

toolchain(
name = "wasm_on_linux",
exec_compatible_with = ["@platforms//os:linux", "@platforms//cpu:x86_64"],
target_compatible_with = ["@platforms//cpu:wasm64"],
use_target_platform_constraints = True,
toolchain = "@bazel_tools//tools/test:empty_toolchain",
toolchain_type = "@bazel_tools//tools/test:default_test_toolchain_type",
)

Fixes #30560

Build API Changes

Backwards compatible change of the use_target_platform_constraints=True case of the toolchain rule.

Checklist

  • I have added tests for the new use cases (if any).
  • I have updated the documentation (if applicable).

Release Notes

RELNOTES[NEW]: Toolchains using use_target_platform_constraints=True can now specify additional execution and target constraints.

Instead of requiring empty target_compatible_with and
exec_compatible_with, use the following backwards compatible logic:

    exec_constraints = toolchain.exec_compatible_with
    if toolchain.use_target_platform_constraints:
        unmatched_constraints = target_platform.constraints - toolchain.target_compatible_with
        exec_constraints += unmatched_constraints

This is useful for the following test toolchain, where building a test
for a target platform with wasm+gpu constraints should not execute the
test on a platform without gpu. In this example, the execution requirements
will be linux+x86_64+gpu.

toolchain(
    name = "wasm_on_linux",
    exec_compatible_with = ["@platforms//os:linux", "@platforms//cpu:x86_64"],
    target_compatible_with = ["@platforms//cpu:wasm64"],
    use_target_platform_constraints = True,
    toolchain = "@bazel_tools//tools/test:empty_toolchain",
    toolchain_type = "@bazel_tools//tools/test:default_test_toolchain_type",
)

Fixes bazelbuild#30560
@moroten
moroten requested a review from a team as a code owner August 12, 2026 13:51
@moroten
moroten requested review from mai93 and removed request for a team August 12, 2026 13:51
@github-actions github-actions Bot added team-Configurability platforms, toolchains, cquery, select(), config transitions team-Core Skyframe, bazel query, BEP, options parsing, bazelrc awaiting-review PR is awaiting review from an assigned reviewer labels Aug 12, 2026
@fmeum

fmeum commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

RELNOTES[NEW]: Toolchains using use_target_platform_constraints=True, mostly test toolchains, now require unmatched target platform constraints to be present on the execution platform.

The RELNOTES read as if this was an incompatible change, which it isn't (since such toolchains would previously have resulted in an error). I think it's worth phrasing that differently.

@moroten

moroten commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

RELNOTES[NEW]: Toolchains using use_target_platform_constraints=True, mostly test toolchains, now require unmatched target platform constraints to be present on the execution platform.

The RELNOTES read as if this was an incompatible change, which it isn't (since such toolchains would previously have resulted in an error). I think it's worth phrasing that differently.

Updated RELNOTES to be more general. The docs describes the functionality anyway.

@fmeum fmeum left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I have reviewed this more closely, I'm wondering whether the algorithm really has the right level of generality. In your motivating example, a reasonable target platform for WASM may set @platforms//os:none or @platforms//os:wasi, which would then cause the toolchain to no longer match an execution platform with other constraint values for that setting.

Do you have other motivating examples? Perhaps we need more complex logic that maps target to exec constraints or allow the exec platform to override values?

* constraints of the target platform.
*/
@Test
public void testExecPlatformMatchesTargetConstraintsWithDefaultTestToolchainFRME() throws Exception {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name doesn't match the content (and FRME is probably not meant to be there either ;-))

@@ -58,27 +59,15 @@
}

public boolean hasTargetToExecConstraints() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can just be dropped/inlined

ToolchainTypeInfo toolchainType,
ConstraintCollection execConstraints,
ConstraintCollection targetConstraints,
boolean useTargetPlatformConstraints,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an @param

* their {@link ConstraintSettingInfo}.
*/
public ImmutableList<ConstraintValueInfo> findMissing(ConstraintCollection expected) {
return findMissing(expected.constraints().values());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be preexisting, but I think that parent on a platform isn't handled correctly by this as well as the "target to exec" logic. Could you add a test exercising that behavior and fix it if needed?

# :linux
# :aarch64
# :is_device
# linux_gpu_test_toolchain will require exec platform with:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment needs to be adapted to this test case, there is no linux_gpu_test_toolchain.

assertThat(testAction.getExecProperties()).containsExactly("os", targetOs, "cpu", targetCpu);
}

/**

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to see a test that verifies that target_compatible_with is checked for a toolchain with use_target_platform_constraints.

* rules.
*/
@AutoCodec
public record DeclaredToolchainInfo(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either update DeclaredToolchainInfoTest.toolchainInfo_equalsTester or (preferably) drop it now that this is a record.

@moroten

moroten commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

Now that I have reviewed this more closely, I'm wondering whether the algorithm really has the right level of generality. In your motivating example, a reasonable target platform for WASM may set @platforms//os:none or @platforms//os:wasi, which would then cause the toolchain to no longer match an execution platform with other constraint values for that setting.

Does such a WASM platform require that no OS is running? Is WASI actually an OS or something that may be available on any OS?

Do you have other motivating examples? Perhaps we need more complex logic that maps target to exec constraints or allow the exec platform to override values?

My motivating example is in Buildbarn.

@fmeum

fmeum commented Aug 13, 2026

Copy link
Copy Markdown
Collaborator

My motivating example is in Buildbarn.

I see how this feature would be useful there, but if I'm being honest, cgo_on/cgo_off being on a target platform is (in hindsight, of course) just a mistake on Go's part as these aren't a property of the target platform in a conceptual sense. This was all done because Bazel didn't have optional toolchains and target_settings at the time. @rules_go//go/config:pure would be the proper way to toggle this today if needed. Happy to clean these up in rules_go and then revisit this idea.

Based on your WASM example, I could see us needing to specify entire constraint settings (rather than values) to ignore when doing the target -> exec constraint transformation. But I would want to wait for more real world use cases hitting this before deciding what the "right" level of genericity is.

Sorry for not noticing this earlier!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting-review PR is awaiting review from an assigned reviewer team-Configurability platforms, toolchains, cquery, select(), config transitions team-Core Skyframe, bazel query, BEP, options parsing, bazelrc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid running tests on platforms without all target constraints

2 participants