bazelbuild / bazelbuild/bazel

Avoid running tests on platforms without all target constraints

Open
#30,560 4 comments 0 reactions 0 assignees View on GitHub
P3 team-Configurability type: feature request
Dominant language
Java
Stars
25.8k
Forks
4.6k
Avg merge
2d 20h
Merged PRs (30d)
72

Description

### Description of the feature request:

### Background
The example below will allow running a test built for wasm+gpu on a platform without any GPU.

Consider the following test toolchain:
```
toolchain(
name = "wasm_on_linux",
exec_compatible_with = ["@platforms//os:linux", "@platforms//cpu:x86_64"],
target_compatible_with = ["@platforms//cpu:wasm64"],
toolchain = "@bazel_tools//tools/test:empty_toolchain",
toolchain_type = "@bazel_tools//tools/test:default_test_toolchain_type",
)
```
With this toolchain, tests can run on a Linux amd64 machine when being built for wasm. The problem is that if the target platform has the constraints `["@platforms//cpu:wasm64", "//gpu:small"]`, it will also match.

One can consider adding another toolchain:
```
toolchain(
name = "wasm_on_linux",
exec_compatible_with = ["@platforms//os:linux", "@platforms//cpu:x86_64", "//gpu:small"],
target_compatible_with = ["@platforms//cpu:wasm64", "//gpu:small"],
toolchain = "@bazel_tools//tools/test:empty_toolchain",
toolchain_type = "@bazel_tools//tools/test:default_test_toolchain_type",
)
```
If that toolchain is registered earlier, things will work, as long as no-one tries to build with the constraint `//gpu:large` in their target platform.

### Suggestion
Extend the meaning of `use_target_platform_constrains` in the [`toolchain` rule](https://bazel.build/reference/be/platforms-and-toolchains#toolchain) to the following algorithm:
```
exec_constraints = toolchain.exec_compatible_with
if toolchain.use_target_platform_constrains:
unmatched_constraints = target_platform.constraints - toolchain.target_compatible_with
exec_constraints += unmatched_constraints
```
Existing rules with `use_target_platform_constrains=True` already have empty `target_compatible_with` and `exec_compatible_with`, so that will behave the same. In the example above, the GPU constraint will be added to the `exec_compatible_with` constraints as it is not matched by the `target_compatible_with` constraints.

### Which category does this issue belong to?

Configurability

### What underlying problem are you trying to solve with this feature?

Avoiding running tests on platforms that do not fulfill all the target constraints.

### Which operating system are you running Bazel on?

Linux

### What is the output of `bazel info release`?

release 9.1.0

### If `bazel info release` returns `development version` or `(@non-git)`, tell us how you built Bazel.

_No response_

### What's the output of `git remote get-url origin; git rev-parse HEAD` ?

```text

```

### Have you found anything relevant by searching the web?

Implementation tracking issue #25160 for _Execution platform selection for test actions in multi-platform builds_.

The issue was found in https://github.com/buildbarn/bb-storage/pull/357 where the `@rules_go//go/toolchain:cgo_off` constraint is problematic.

### Any other information, logs, or outputs that you want to share?

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with the toolchain rule documentation and implementation-tracking issue #25160, then trace how test toolchains match target and execution-platform constraints. Validate the change with the wasm64/Linux x86_64 example and GPU constraints, while confirming existing use_target_platform_constrains=True rules retain their behavior.

Written by the indexing model from the issue text.

Assessment

Domain
build-system, testing-qa
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.