refactor(toolchains): define the Python toolchain type in rules_python (#4404) `//python:toolchain_type` is an alias of the type in `@bazel_tools`. To move the type out of `@bazel_tools`, Bazel will forward `@bazel_tools//tools/python:toolchain_type` to this target (bazelbuild/bazel#32412), which requires it to be the actual type on those Bazel versions. `//python:toolchain_type` is now a `toolchain_type` if a new value in the `@rules_python_internal` config is set and remains an alias otherwise, so both labels always refer to the same type. The `config` extension sets the value from a `bazel_features` flag, which is `False` for now, so nothing changes yet. WORKSPACE builds leave it unset, as Bazel versions that forward the type no longer support WORKSPACE. This also avoids requiring `bazel_features` in WORKSPACE builds. Work towards bazelbuild/bazel#32428 --------- Co-authored-by: Richard Levasseur <richardlev@gmail.com>
diff --git a/MODULE.bazel b/MODULE.bazel index aa1f4eb..75087fd 100644 --- a/MODULE.bazel +++ b/MODULE.bazel
@@ -4,7 +4,7 @@ compatibility_level = 1, ) -bazel_dep(name = "bazel_features", version = "1.21.0") +bazel_dep(name = "bazel_features", version = "1.55.0") bazel_dep(name = "bazel_skylib", version = "1.8.2") bazel_dep(name = "package_metadata", version = "0.0.7") bazel_dep(name = "platforms", version = "0.0.11")
diff --git a/python/BUILD.bazel b/python/BUILD.bazel index e0e32c5..f824bb2 100644 --- a/python/BUILD.bazel +++ b/python/BUILD.bazel
@@ -30,6 +30,7 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") load("//python/private:distribution_filegroup.bzl", "distribution_filegroup") +load("//python/private:python_toolchain_type.bzl", "python_toolchain_type") load(":current_py_toolchain.bzl", "current_py_toolchain") package(default_visibility = ["//visibility:public"]) @@ -115,9 +116,8 @@ # The toolchain type for Python rules. Provides a Python 2 and/or Python 3 # runtime. -alias( +python_toolchain_type( name = "toolchain_type", - actual = "@bazel_tools//tools/python:toolchain_type", ) toolchain_type(
diff --git a/python/extensions/config.bzl b/python/extensions/config.bzl index f836574..ecfb55a 100644 --- a/python/extensions/config.bzl +++ b/python/extensions/config.bzl
@@ -95,6 +95,7 @@ transition_settings = transition_settings, explicit_init_py_modules = explicit_init_py_modules, tool_deps_dev_only = tool_deps_dev_only, + toolchain_type_in_rules_python = bazel_features.rules._toolchain_type_in_rules_python, ) pypi_deps()
diff --git a/python/private/BUILD.bazel b/python/private/BUILD.bazel index dc0758e..b111df6 100644 --- a/python/private/BUILD.bazel +++ b/python/private/BUILD.bazel
@@ -800,6 +800,12 @@ ) bzl_library( + name = "python_toolchain_type", + srcs = ["python_toolchain_type.bzl"], + deps = ["@rules_python_internal//:rules_python_config"], +) + +bzl_library( name = "pythons_hub", srcs = ["pythons_hub.bzl"], deps = [
diff --git a/python/private/internal_config_repo.bzl b/python/private/internal_config_repo.bzl index 8f203bb..fef5caa 100644 --- a/python/private/internal_config_repo.bzl +++ b/python/private/internal_config_repo.bzl
@@ -39,6 +39,7 @@ BuiltinPyCcLinkParamsProvider = getattr(getattr(native, "legacy_globals", None), "PyCcLinkParamsProvider", {builtin_py_cc_link_params_provider}), modules_using_explicit_initpy = {modules_using_explicit_initpy}, tool_deps_dev_only = {tool_deps_dev_only}, + toolchain_type_in_rules_python = {toolchain_type_in_rules_python}, ) """ @@ -114,6 +115,7 @@ builtin_py_cc_link_params_provider = builtin_py_cc_link_params_provider, modules_using_explicit_initpy = str(explicit_init_py_modules), tool_deps_dev_only = str(rctx.attr.tool_deps_dev_only), + toolchain_type_in_rules_python = str(rctx.attr.toolchain_type_in_rules_python), bazel_8_or_later = str(bazel_major_version >= 8), bazel_9_or_later = str(bazel_major_version >= 9), bazel_10_or_later = str(bazel_major_version > 9), @@ -147,6 +149,7 @@ attrs = { "explicit_init_py_modules": attr.string_dict(), "tool_deps_dev_only": attr.bool(default = True), + "toolchain_type_in_rules_python": attr.bool(), "transition_setting_generators": attr.string_list_dict(), "transition_settings": attr.string_list(), },
diff --git a/python/private/python_toolchain_type.bzl b/python/private/python_toolchain_type.bzl new file mode 100644 index 0000000..4c9d2c9 --- /dev/null +++ b/python/private/python_toolchain_type.bzl
@@ -0,0 +1,28 @@ +"""Helper macro for defining the Python toolchain type target.""" + +load("@rules_python_internal//:rules_python_config.bzl", "config") + +def python_toolchain_type(name, **kwargs): + """Defines the Python toolchain type target or aliases `@bazel_tools`. + + Bazel versions that forward `@bazel_tools//tools/python:toolchain_type` to + this target get the actual `toolchain_type`. On all others, this is an alias + of the type in `@bazel_tools`, so that both labels always refer to the same + type. + + Args: + name: {type}`str` The name of the target. + **kwargs: Additional attributes forwarded to `toolchain_type` or + `alias`. + """ + if config.toolchain_type_in_rules_python: + native.toolchain_type( + name = name, + **kwargs + ) + else: + native.alias( + name = name, + actual = "@bazel_tools//tools/python:toolchain_type", + **kwargs + )
diff --git a/tests/integration/bzlmod_lockfile/MODULE.bazel.lock b/tests/integration/bzlmod_lockfile/MODULE.bazel.lock index a487d8e..e8f38b0 100644 --- a/tests/integration/bzlmod_lockfile/MODULE.bazel.lock +++ b/tests/integration/bzlmod_lockfile/MODULE.bazel.lock
@@ -27,7 +27,6 @@ "https://bcr.bazel.build/modules/bazel_features/1.17.0/MODULE.bazel": "039de32d21b816b47bd42c778e0454217e9c9caac4a3cf8e15c7231ee3ddee4d", "https://bcr.bazel.build/modules/bazel_features/1.18.0/MODULE.bazel": "1be0ae2557ab3a72a57aeb31b29be347bcdc5d2b1eb1e70f39e3851a7e97041a", "https://bcr.bazel.build/modules/bazel_features/1.19.0/MODULE.bazel": "59adcdf28230d220f0067b1f435b8537dd033bfff8db21335ef9217919c7fb58", - "https://bcr.bazel.build/modules/bazel_features/1.21.0/MODULE.bazel": "675642261665d8eea09989aa3b8afb5c37627f1be178382c320d1b46afba5e3b", "https://bcr.bazel.build/modules/bazel_features/1.23.0/MODULE.bazel": "fd1ac84bc4e97a5a0816b7fd7d4d4f6d837b0047cf4cbd81652d616af3a6591a", "https://bcr.bazel.build/modules/bazel_features/1.27.0/MODULE.bazel": "621eeee06c4458a9121d1f104efb80f39d34deff4984e778359c60eaf1a8cb65", "https://bcr.bazel.build/modules/bazel_features/1.28.0/MODULE.bazel": "4b4200e6cbf8fa335b2c3f43e1d6ef3e240319c33d43d60cc0fbd4b87ece299d", @@ -37,7 +36,8 @@ "https://bcr.bazel.build/modules/bazel_features/1.4.1/MODULE.bazel": "e45b6bb2350aff3e442ae1111c555e27eac1d915e77775f6fdc4b351b758b5d7", "https://bcr.bazel.build/modules/bazel_features/1.42.1/MODULE.bazel": "275a59b5406ff18c01739860aa70ad7ccb3cfb474579411decca11c93b951080", "https://bcr.bazel.build/modules/bazel_features/1.50.0/MODULE.bazel": "2083ef9c7a469f520890483ccf8e0189d6e71e2117e7752e15e6554433d5ae3e", - "https://bcr.bazel.build/modules/bazel_features/1.50.0/source.json": "e0ee3debde2789ff56e4452e612d126925ba9ab64d4bde79c67f099d2902df9b", + "https://bcr.bazel.build/modules/bazel_features/1.55.0/MODULE.bazel": "6cd2e9a25e1f28957c87c94ebf7a5a730d2312582d97c24ec1e1e9a5e6aca596", + "https://bcr.bazel.build/modules/bazel_features/1.55.0/source.json": "0bc67827e4a293029523788d7c0099a14fd6e55ec44f539731dad00edf9c5ddc", "https://bcr.bazel.build/modules/bazel_skylib/1.0.3/MODULE.bazel": "bcb0fd896384802d1ad283b4e4eb4d718eebd8cb820b0a2c3a347fb971afd9d8", "https://bcr.bazel.build/modules/bazel_skylib/1.1.1/MODULE.bazel": "1add3e7d93ff2e6998f9e118022c84d163917d912f5afafb3058e3d2f1545b5e", "https://bcr.bazel.build/modules/bazel_skylib/1.2.0/MODULE.bazel": "44fe84260e454ed94ad326352a698422dbe372b21a1ac9f3eab76eb531223686", @@ -290,7 +290,7 @@ }, "@@rules_rust+//crate_universe/private:internal_extensions.bzl%cu_nr": { "general": { - "bzlTransitiveDigest": "g4nHsJk7G2xJL32n0yvZNPe9VCXql6RqEYG/e3/JzrY=", + "bzlTransitiveDigest": "vMDQkm62ghhQFkvmBfnkmBrUXlPNX0B5uBOvm8QJzJo=", "usagesDigest": "+1w4EnEfZLFpf8Ak18jhmCWsOTE75Noy2i16whyXYWs=", "recordedInputs": [ "REPO_MAPPING:apple_support+,bazel_skylib bazel_skylib+",