Skip to content

Commit

Permalink
[bazel] Add hybrid-mode rules_python patch
Browse files Browse the repository at this point in the history
Required for the python toolchain to be registered from `WORKSPACE` when
using Bazel in hybrid mode.

This patch will be included in the `0.37` release of `rules_rust`. It
can also be removed once we move `rules_python` to `MODULE.bazel`.

Signed-off-by: James Wainwright <[email protected]>
  • Loading branch information
jwnrt committed Oct 15, 2024
1 parent b12073c commit f7067db
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
5 changes: 5 additions & 0 deletions third_party/python/patches/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright lowRISC contributors (OpenTitan project).
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

package(default_visibility = ["//visibility:public"])
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
commit 43583d1fe84bc2b4daa1531e3dcf404dbcb98dda
Author: Richard Levasseur <[email protected]>
Date: Thu Oct 10 20:12:55 2024 -0700

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 https://github.com/bazelbuild/rules_python/issues/1675

diff --git python/private/BUILD.bazel python/private/BUILD.bazel
index b4084fb7..6fb4a1cc 100644
--- python/private/BUILD.bazel
+++ python/private/BUILD.bazel
@@ -177,7 +177,6 @@ bzl_library(
deps = [
":auth_bzl",
":bazel_tools_bzl",
- ":bzlmod_enabled_bzl",
":coverage_deps_bzl",
":full_version_bzl",
":internal_config_repo_bzl",
diff --git python/private/python.bzl python/private/python.bzl
index 83bc43f9..12ab4bb4 100644
--- python/private/python.bzl
+++ python/private/python.bzl
@@ -228,7 +228,11 @@ def _python_impl(module_ctx):
kwargs.update(py.config.kwargs.get(toolchain_info.python_version, {}))
kwargs.update(py.config.kwargs.get(full_python_version, {}))
kwargs.update(py.config.default)
- python_register_toolchains(name = toolchain_info.name, **kwargs)
+ python_register_toolchains(
+ name = toolchain_info.name,
+ _internal_bzlmod_toolchain_call = True,
+ **kwargs
+ )

# Create the pythons_hub repo for the interpreter meta data and the
# the various toolchains.
diff --git python/private/python_register_toolchains.bzl python/private/python_register_toolchains.bzl
index d20e0496..64b66d5a 100644
--- python/private/python_register_toolchains.bzl
+++ python/private/python_register_toolchains.bzl
@@ -23,7 +23,6 @@ load(
"TOOL_VERSIONS",
"get_release_info",
)
-load(":bzlmod_enabled.bzl", "BZLMOD_ENABLED")
load(":coverage_deps.bzl", "coverage_dep")
load(":full_version.bzl", "full_version")
load(":python_repository.bzl", "python_repository")
@@ -75,9 +74,8 @@ def python_register_toolchains(
version.
**kwargs: passed to each {obj}`python_repository` call.
"""
-
- if BZLMOD_ENABLED:
- # you cannot used native.register_toolchains when using bzlmod.
+ bzlmod_toolchain_call = kwargs.pop("_internal_bzlmod_toolchain_call", False)
+ if bzlmod_toolchain_call:
register_toolchains = False

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

# in bzlmod we write out our own toolchain repos
- if BZLMOD_ENABLED:
+ if bzlmod_toolchain_call:
return

toolchains_repo(
2 changes: 2 additions & 0 deletions third_party/python/repos.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ def python_repos():
sha256 = "ca77768989a7f311186a29747e3e95c936a41dffac779aff6b443db22290d913",
strip_prefix = "rules_python-0.36.0",
url = "https://github.com/bazelbuild/rules_python/releases/download/0.36.0/rules_python-0.36.0.tar.gz",
# This patch can be removed once 0.37.0 is released (or we move `rules_python` to bzlmod).
patches = ["//third_party/python/patches:rules_python.hybrid_workspace_register_toolchain.patch"],
)

0 comments on commit f7067db

Please sign in to comment.