Skip to content

Commit 5ff514a

Browse files
authored
fix: generation of toolchain aliases //:defs.bzl file. (bazel-contrib#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 information
1 parent 8400610 commit 5ff514a

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

examples/bzlmod/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("@pip//:requirements.bzl", "requirement")
2+
load("@python3_9//:defs.bzl", py_test_with_transition = "py_test")
23
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")
34
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
45

@@ -35,3 +36,10 @@ py_test(
3536
srcs = ["test.py"],
3637
deps = [":lib"],
3738
)
39+
40+
py_test_with_transition(
41+
name = "test_with_transition",
42+
srcs = ["test.py"],
43+
main = "test.py",
44+
deps = [":lib"],
45+
)

examples/bzlmod/MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ python.toolchain(
1616
configure_coverage_tool = True,
1717
python_version = "3.9",
1818
)
19+
use_repo(python, "python3_9")
1920
use_repo(python, "python3_9_toolchains")
2021

2122
register_toolchains(

python/private/toolchains_repo.bzl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ load(
3131
"WINDOWS_NAME",
3232
)
3333

34+
def get_repository_name(repository_workspace):
35+
dummy_label = "//:_"
36+
return str(repository_workspace.relative(dummy_label))[:-len(dummy_label)] or "@"
37+
3438
def _toolchains_repo_impl(rctx):
35-
rules_python_repository_name = rctx.attr._rules_python_workspace.workspace_name
36-
python_version_constraint = "@{rules_python}//python/config_settings:is_python_{python_version}".format(
37-
rules_python = rules_python_repository_name,
39+
python_version_constraint = "{rules_python}//python/config_settings:is_python_{python_version}".format(
40+
rules_python = get_repository_name(rctx.attr._rules_python_workspace),
3841
python_version = rctx.attr.python_version,
3942
)
4043

@@ -90,8 +93,6 @@ def _toolchain_aliases_impl(rctx):
9093
is_windows = (os_name == WINDOWS_NAME)
9194
python3_binary_path = "python.exe" if is_windows else "bin/python3"
9295

93-
rules_python_repository_name = rctx.attr._rules_python_workspace.workspace_name
94-
9596
# Base BUILD file for this repository.
9697
build_contents = """\
9798
# Generated by python/private/toolchains_repo.bzl
@@ -123,8 +124,8 @@ alias(name = "pip", actual = select({{":" + item: "@{py_repository}_
123124
rctx.file("defs.bzl", content = """\
124125
# Generated by python/private/toolchains_repo.bzl
125126
126-
load("@{rules_python}//python/config_settings:transition.bzl", _py_binary = "py_binary", _py_test = "py_test")
127-
load("@{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
127+
load("{rules_python}//python/config_settings:transition.bzl", _py_binary = "py_binary", _py_test = "py_test")
128+
load("{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
128129
129130
host_platform = "{host_platform}"
130131
interpreter = "@{py_repository}_{host_platform}//:{python3_binary_path}"
@@ -156,7 +157,7 @@ def compile_pip_requirements(name, **kwargs):
156157
py_repository = rctx.attr.user_repository_name,
157158
python_version = rctx.attr.python_version,
158159
python3_binary_path = python3_binary_path,
159-
rules_python = rules_python_repository_name,
160+
rules_python = get_repository_name(rctx.attr._rules_python_workspace),
160161
))
161162

162163
toolchain_aliases = repository_rule(

0 commit comments

Comments
 (0)