Skip to content

Commit 43583d1

Browse files
authored
fix(bzlmod): let workspace-invoked python_register_toolchains to register toolchains (#2289)
While migrating to bzlmod, users may have a hybrid build where WORKSPACE contains python_register_toolchain() calls. Because these calls originate from WORKSPACE files, the `native.register_toolchain` APIs are still available. At the same time, we still detect that bzlmod is enabled, and thus disable calling those functions. The net effect is, users aren't able to register toolchains in such a hybrid setup. At the same time, because the code path is shared, we can't have the bzlmod toolchain code try to call them, as it'll fail. To accomodate both cases, have the bzlmod toolchain code pass a special arg so that `python_register_toolchains` knows to skip parts that don't apply to the bzlmod toolchain invocation. This was all unwound by some users and pointed out in a Slack thread. A few people are manually carrying an equivalent patch for a working hybrid mode. Fixes #1675
1 parent 1388a89 commit 43583d1

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ A brief description of the categories of changes:
4747
* (rules) `compile_pip_requirements` passes `env` to the `X.update` target (and
4848
not only to the `X_test` target, a bug introduced in
4949
[#1067](https://github.com/bazelbuild/rules_python/pull/1067)).
50+
* (bzlmod) In hybrid bzlmod with WORKSPACE builds,
51+
`python_register_toolchains(register_toolchains=True)` is respected
52+
([#1675](https://github.com/bazelbuild/rules_python/issues/1675)).
5053

5154
### Added
5255
* (py_wheel) Now supports `compress = (True|False)` to allow disabling

python/private/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ bzl_library(
177177
deps = [
178178
":auth_bzl",
179179
":bazel_tools_bzl",
180-
":bzlmod_enabled_bzl",
181180
":coverage_deps_bzl",
182181
":full_version_bzl",
183182
":internal_config_repo_bzl",

python/private/python.bzl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,11 @@ def _python_impl(module_ctx):
228228
kwargs.update(py.config.kwargs.get(toolchain_info.python_version, {}))
229229
kwargs.update(py.config.kwargs.get(full_python_version, {}))
230230
kwargs.update(py.config.default)
231-
python_register_toolchains(name = toolchain_info.name, **kwargs)
231+
python_register_toolchains(
232+
name = toolchain_info.name,
233+
_internal_bzlmod_toolchain_call = True,
234+
**kwargs
235+
)
232236

233237
# Create the pythons_hub repo for the interpreter meta data and the
234238
# the various toolchains.

python/private/python_register_toolchains.bzl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ load(
2323
"TOOL_VERSIONS",
2424
"get_release_info",
2525
)
26-
load(":bzlmod_enabled.bzl", "BZLMOD_ENABLED")
2726
load(":coverage_deps.bzl", "coverage_dep")
2827
load(":full_version.bzl", "full_version")
2928
load(":python_repository.bzl", "python_repository")
@@ -75,9 +74,8 @@ def python_register_toolchains(
7574
version.
7675
**kwargs: passed to each {obj}`python_repository` call.
7776
"""
78-
79-
if BZLMOD_ENABLED:
80-
# you cannot used native.register_toolchains when using bzlmod.
77+
bzlmod_toolchain_call = kwargs.pop("_internal_bzlmod_toolchain_call", False)
78+
if bzlmod_toolchain_call:
8179
register_toolchains = False
8280

8381
base_url = kwargs.pop("base_url", DEFAULT_RELEASE_BASE_URL)
@@ -169,7 +167,7 @@ def python_register_toolchains(
169167
)
170168

171169
# in bzlmod we write out our own toolchain repos
172-
if BZLMOD_ENABLED:
170+
if bzlmod_toolchain_call:
173171
return
174172

175173
toolchains_repo(

0 commit comments

Comments
 (0)