bazelbuild / bazel

a fast, scalable, multi-language and extensible build system
https://bazel.build
Apache License 2.0
23.15k stars 4.05k forks source link

Custom error message for target_compatible_with + select + `//platforms:incompatible` #24055

Open cramertj opened 4 days ago

cramertj commented 4 days ago

Description of the feature request:

Add a no_match_compatible_error to select or a similar mechanism to provide a custom error message when //platforms:incompatible is selected.

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

It's common for targets to express a dependency on a particular platform feature using target_compatible_with + select + //platforms:incompatible, as demonstrated in the "More expressive constraints" section of the Platforms documentation.

Unfortunately, this leads to poor error messages when attempting to build with an incompatible platform:

➜  pigweed git:(34b64d0b9) bazelisk test //pw_async2/examples:coro_blinky_loop
Target //pw_async2/examples:coro_blinky_loop failed to build
ERROR: Analysis of target '//pw_async2/examples:coro_blinky_loop' failed; build aborted: Target //pw_async2/examples:coro_blinky_loop is incompatible and cannot be built, but was explicitly requested.
Dependency chain:
    //pw_async2/examples:coro_blinky_loop (8bf521)
    //pw_async2:coro (2ccb10)   <-- target platform (@@platforms//host:host) didn't satisfy constraint @@platforms//:incompatible
INFO: Elapsed time: 0.215s, Critical Path: 0.03s
INFO: 1 process: 1 internal.
ERROR: Build did NOT complete successfully
ERROR: No test targets were found, yet testing was requested

What I would like to be able to create is an error like the following:

➜  pigweed git:(34b64d0b9) bazelisk test //pw_async2/examples:coro_blinky_loop
Target //pw_async2/examples:coro_blinky_loop failed to build
ERROR: Analysis of target '//pw_async2/examples:coro_blinky_loop' failed; build aborted: Target //pw_async2/examples:coro_blinky_loop is incompatible and cannot be built, but was explicitly requested.
Dependency chain:
    //pw_async2/examples:coro_blinky_loop (8bf521)
    //pw_async2:coro (2ccb10)   <-- (@@platforms//host:host) is incompatible:
        Target requires C++20 or greater. Consider rebuilding with `--config=cxx20`.
INFO: Elapsed time: 0.215s, Critical Path: 0.03s
INFO: 1 process: 1 internal.
ERROR: Build did NOT complete successfully
ERROR: No test targets were found, yet testing was requested

select does actually have some support for custom error messages in the form of no_match_error today, but this is not usable with target_compatible_with, as it will create a hard error when performing a wildcard build rather than skipping incompatible targets.

There should be some way to unify these features, possibly with a no_match_incompatible_error or similar arg.

cramertj commented 4 days ago

This is a duplicate of https://github.com/bazelbuild/bazel/issues/22850