Closed rrbutani closed 11 months ago
Hi @rrbutani, thank you for your contribution. I think your approach is reasonable, and skipping the creation of the toolchain instead of its registration is OK for me.
@mergifyio rebase
rebase
My bad, had an extra closing quote on one of the doc strings; CI should (hopefully) pass now? :crossed_fingers:
Motivation
I have a use case where I want to register my own posix toolchain and don't want Bazel to fall back to using the
local_posix
toolchain (the toolchain I'm making has certain constraints — when they aren't met I want Bazel to error).sh_posix_configure
provides an easy way to skip registering the local toolchain at@local_posix_config//:local_posix_toolchain
but usingrules_sh
via bzlmod does not expose a way to configure this:https://github.com/tweag/rules_sh/blob/0e3d91d962ebbbc2425a6414d0052ff43d853149/MODULE.bazel#L14
A Solution
While we can condition the
register_toolchains
invocation [^1] inMODULE.bazel
(i.e.register_toolchains(...) if <condition> else None
is permitted), unfortunately, as far as I know, there isn't a way to read data from module extension tag classes withinMODULE.bazel
.This PR instead has the
sh_configure
module extension conditionally generate the@local_posix_config//:local_posix_toolchain
toolchain and altersMODULE.bazel
to register@local_posix_config//...
(empty when the toolchain isn't created).[^1]: additionally, only
register_toolchains
invocations from withinMODULE.bazel
files are honoredThis PR adds a
local_posix_config
tag class tosh_configure
. This allows users to add snippets like this to theirMODULE.bazel
file to configure whether the local posix toolchain is generated and registered:If no tags are specified, the toolchain is generated (making this is a backwards compatible change).
If multiple tags are specified with different values for
enable
, the extension emits a warning and takes the first value inmodule_ctx.modules
(i.e. first tag of the first module reached via BFS from the root module):Open Questions/Notes
sh_posix_configure
okay? (i.e. gating the creation of the toolchain instead of just its registration)@local_posix_config//:local_posix_toolchain
and not its creation, we can add another level of indirection: a wrapper repo thatsh_configure
emits that can (conditionally) re-export the toolchainextensions.bzl
in this PR — I can update this PR with those changes if desired)