fix: generation of toolchain aliases //:defs.bzl file. (#1088)
## PR Checklist
Please check if your PR fulfills the following requirements:
- [ ] Tests for the changes have been added (for bug fixes / features)
- [ ] Docs have been added / updated (for bug fixes / features)
## PR Type
What kind of change does this PR introduce?
<!-- Please check the one that applies to this PR using "x". -->
- [X] Bugfix
- [ ] Feature (please, look at the "Scope of the project" section in the
README.md file)
- [ ] Code style update (formatting, local variables)
- [ ] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] CI related changes
- [ ] Documentation content changes
- [ ] Other... Please describe:
## What is the current behavior?
<!-- Please describe the current behavior that you are modifying, or
link to a relevant issue. -->
With `common --experimental_enable_bzlmod` option defs.bzl is generated
as
```
load("@rules_python~override//python/config_settings:transition.bzl", _py_binary = "py_binary", _py_test = "py_test")
load("@rules_python~override//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
```
and these lines cause a problem at
```
load("@python3_9//:defs.bzl", "interpreter")
```
as
```
ERROR: .../BUILD:25:11: error loading package 'src': at .../external/rules_python~override~python~python3_9/defs.bzl:4:6: Unable to find package for @[unknown repo 'rules_python~override' requested from @rules_python~override~python~python3_9]//python:pip.bzl: The repository '@[unknown repo 'rules_python~override' requested from @rules_python~override~python~python3_9]' could not be resolved: No repository visible as '@rules_python~override' from repository '@rules_python~override~python~python3_9'. and referenced by '...'
```
Issue Number: N/A
## What is the new behavior?
Generated load statements
```
load("@@rules_python~override//python/config_settings:transition.bzl", _py_binary = "py_binary", _py_test = "py_test")
load("@@rules_python~override//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
```
## Does this PR introduce a breaking change?
- [ ] Yes
- [X] No
<!-- If this PR contains a breaking change, please describe the impact
and migration path for existing applications below. -->
## Other informationdiff --git a/examples/bzlmod/BUILD.bazel b/examples/bzlmod/BUILD.bazel
index 7b7566b..7ecc035 100644
--- a/examples/bzlmod/BUILD.bazel
+++ b/examples/bzlmod/BUILD.bazel
@@ -1,4 +1,5 @@
load("@pip//:requirements.bzl", "requirement")
+load("@python3_9//:defs.bzl", py_test_with_transition = "py_test")
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
@@ -35,3 +36,10 @@
srcs = ["test.py"],
deps = [":lib"],
)
+
+py_test_with_transition(
+ name = "test_with_transition",
+ srcs = ["test.py"],
+ main = "test.py",
+ deps = [":lib"],
+)
diff --git a/examples/bzlmod/MODULE.bazel b/examples/bzlmod/MODULE.bazel
index 5f984c3..ce91228 100644
--- a/examples/bzlmod/MODULE.bazel
+++ b/examples/bzlmod/MODULE.bazel
@@ -16,6 +16,7 @@
configure_coverage_tool = True,
python_version = "3.9",
)
+use_repo(python, "python3_9")
use_repo(python, "python3_9_toolchains")
register_toolchains(
diff --git a/python/private/toolchains_repo.bzl b/python/private/toolchains_repo.bzl
index 4b832d9..9bed73e 100644
--- a/python/private/toolchains_repo.bzl
+++ b/python/private/toolchains_repo.bzl
@@ -31,10 +31,13 @@
"WINDOWS_NAME",
)
+def get_repository_name(repository_workspace):
+ dummy_label = "//:_"
+ return str(repository_workspace.relative(dummy_label))[:-len(dummy_label)] or "@"
+
def _toolchains_repo_impl(rctx):
- rules_python_repository_name = rctx.attr._rules_python_workspace.workspace_name
- python_version_constraint = "@{rules_python}//python/config_settings:is_python_{python_version}".format(
- rules_python = rules_python_repository_name,
+ python_version_constraint = "{rules_python}//python/config_settings:is_python_{python_version}".format(
+ rules_python = get_repository_name(rctx.attr._rules_python_workspace),
python_version = rctx.attr.python_version,
)
@@ -90,8 +93,6 @@
is_windows = (os_name == WINDOWS_NAME)
python3_binary_path = "python.exe" if is_windows else "bin/python3"
- rules_python_repository_name = rctx.attr._rules_python_workspace.workspace_name
-
# Base BUILD file for this repository.
build_contents = """\
# Generated by python/private/toolchains_repo.bzl
@@ -123,8 +124,8 @@
rctx.file("defs.bzl", content = """\
# Generated by python/private/toolchains_repo.bzl
-load("@{rules_python}//python/config_settings:transition.bzl", _py_binary = "py_binary", _py_test = "py_test")
-load("@{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
+load("{rules_python}//python/config_settings:transition.bzl", _py_binary = "py_binary", _py_test = "py_test")
+load("{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
host_platform = "{host_platform}"
interpreter = "@{py_repository}_{host_platform}//:{python3_binary_path}"
@@ -156,7 +157,7 @@
py_repository = rctx.attr.user_repository_name,
python_version = rctx.attr.python_version,
python3_binary_path = python3_binary_path,
- rules_python = rules_python_repository_name,
+ rules_python = get_repository_name(rctx.attr._rules_python_workspace),
))
toolchain_aliases = repository_rule(