-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Don't require downstream users to register Python toolchains.
The Python toolchains are only needed for dev purposes (doc generation). Unfortunately, Bazel doesn't provide an easy way to mark a register_toolchains() call as dev-only. To work around this, we use an extension to create a repository whose contents depend on if its the root module or not. If its the root module, it means we're the rules_testing module and its the dev-dependency case, so write a build file with the real toolchains. If its not the root module, then its not the dev-dependency case, so write an empty build file. Fixes #33
- Loading branch information
Showing
4 changed files
with
87 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"""Extension only used for development purposes.""" | ||
|
||
def _dev_ext_impl(mctx): | ||
module = mctx.modules[0] | ||
_dev_toolchains_repo( | ||
name = "rules_testing_dev_toolchains", | ||
is_root = module.is_root, | ||
) | ||
|
||
dev = module_extension( | ||
implementation = _dev_ext_impl, | ||
tag_classes = { | ||
"setup": tag_class(), | ||
}, | ||
) | ||
|
||
def _dev_toolchains_repo_impl(rctx): | ||
# If its the root module, then we're in rules_testing and | ||
# it's a dev dependency situation. | ||
if rctx.attr.is_root: | ||
toolchain_build = Label("@python3_11_toolchains//:BUILD.bazel") | ||
|
||
# NOTE: This is brittle. It only works because, luckily, | ||
# rules_python's toolchain BUILD file is essentially self-contained. | ||
# It only uses absolute references and doesn't load anything, | ||
# so we can copy it elsewhere and it still works. | ||
rctx.symlink(toolchain_build, "BUILD.bazel") | ||
else: | ||
rctx.file("BUILD.bazel", "") | ||
|
||
_dev_toolchains_repo = repository_rule( | ||
implementation = _dev_toolchains_repo_impl, | ||
attrs = { | ||
"is_root": attr.bool(), | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,7 @@ | |
load(":tests.bzl", "bzlmod_test_suite") | ||
|
||
bzlmod_test_suite(name = "bzlmod_tests") | ||
|
||
toolchain_type( | ||
name = "fake", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters