From f7067dbab19f0a2e4b26d36f98644ebdb91348eb Mon Sep 17 00:00:00 2001 From: James Wainwright Date: Tue, 15 Oct 2024 12:07:36 +0100 Subject: [PATCH] [bazel] Add hybrid-mode `rules_python` patch 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 --- third_party/python/patches/BUILD | 5 + ....hybrid_workspace_register_toolchain.patch | 94 +++++++++++++++++++ third_party/python/repos.bzl | 2 + 3 files changed, 101 insertions(+) create mode 100644 third_party/python/patches/BUILD create mode 100644 third_party/python/patches/rules_python.hybrid_workspace_register_toolchain.patch diff --git a/third_party/python/patches/BUILD b/third_party/python/patches/BUILD new file mode 100644 index 00000000000000..c85882e64fe2c0 --- /dev/null +++ b/third_party/python/patches/BUILD @@ -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"]) diff --git a/third_party/python/patches/rules_python.hybrid_workspace_register_toolchain.patch b/third_party/python/patches/rules_python.hybrid_workspace_register_toolchain.patch new file mode 100644 index 00000000000000..1e0b1f75954a7d --- /dev/null +++ b/third_party/python/patches/rules_python.hybrid_workspace_register_toolchain.patch @@ -0,0 +1,94 @@ +commit 43583d1fe84bc2b4daa1531e3dcf404dbcb98dda +Author: Richard Levasseur +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( diff --git a/third_party/python/repos.bzl b/third_party/python/repos.bzl index 0201c2d2b2e0d1..b68e8e1ea5d81d 100644 --- a/third_party/python/repos.bzl +++ b/third_party/python/repos.bzl @@ -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"], )