From 2ee80c3e4675c1fc84976cf4c2d036ce69fd4eb1 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Sun, 27 Oct 2024 03:36:14 +0900 Subject: [PATCH] fix(bzlmod): generate config_setting values for all python toolchains (#2350) PR #2253 broke how the config settings are generated and only generated the config setting values for the python version values that we would have the registered toolchains for. This PR restores the previous behaviour. However, if the root module uses `python.override` to remove the allowed toolchains, then `config_settings` will be also affected. (cherry picked from commit 9340a8199cac0912305dad427a5cdc72ee4fc7ff) Conflicts: - CHANGELOG.md --- CHANGELOG.md | 14 +++++++ examples/multi_python_versions/MODULE.bazel | 1 + .../multi_python_versions/tests/BUILD.bazel | 39 +++++++++++++++++++ python/private/python.bzl | 1 + 4 files changed, 55 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a94ff8763f..49128cff3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,8 +20,11 @@ A brief description of the categories of changes: * Particular sub-systems are identified using parentheses, e.g. `(bzlmod)` or `(docs)`. +{#v0-0-0} ## Unreleased +[0.0.0]: https://github.com/bazelbuild/rules_python/releases/tag/0.0.0 + ### Changed - Nothing yet @@ -34,6 +37,17 @@ A brief description of the categories of changes: ### Removed - Nothing yet +{#v0-37-2} +## [0.37.2] - 2024-10-27 + +[0.37.2]: https://github.com/bazelbuild/rules_python/releases/tag/0.37.2 + +{#v0-37-2-fixed} +### Fixed +* (bzlmod) Generate `config_setting` values for all available toolchains instead + of only the registered toolchains, which restores the previous behaviour that + `bzlmod` users would have observed. + {#v0-37-1} ## [0.37.1] - 2024-10-22 diff --git a/examples/multi_python_versions/MODULE.bazel b/examples/multi_python_versions/MODULE.bazel index 4223916d22..536940ba49 100644 --- a/examples/multi_python_versions/MODULE.bazel +++ b/examples/multi_python_versions/MODULE.bazel @@ -30,6 +30,7 @@ python.toolchain( ) use_repo( python, + "pythons_hub", python = "python_versions", ) diff --git a/examples/multi_python_versions/tests/BUILD.bazel b/examples/multi_python_versions/tests/BUILD.bazel index cf14bf0a3b..d5c66e026f 100644 --- a/examples/multi_python_versions/tests/BUILD.bazel +++ b/examples/multi_python_versions/tests/BUILD.bazel @@ -1,9 +1,14 @@ load("@bazel_skylib//rules:copy_file.bzl", "copy_file") +load("@bazel_skylib//rules:diff_test.bzl", "diff_test") +load("@bazel_skylib//rules:write_file.bzl", "write_file") load("@python//3.10:defs.bzl", py_binary_3_10 = "py_binary", py_test_3_10 = "py_test") load("@python//3.11:defs.bzl", py_binary_3_11 = "py_binary", py_test_3_11 = "py_test") load("@python//3.8:defs.bzl", py_binary_3_8 = "py_binary", py_test_3_8 = "py_test") load("@python//3.9:defs.bzl", py_binary_3_9 = "py_binary", py_test_3_9 = "py_test") +load("@pythons_hub//:versions.bzl", "MINOR_MAPPING", "PYTHON_VERSIONS") load("@rules_python//python:defs.bzl", "py_binary", "py_test") +load("@rules_python//python:versions.bzl", DEFAULT_MINOR_MAPPING = "MINOR_MAPPING", DEFAULT_TOOL_VERSIONS = "TOOL_VERSIONS") +load("@rules_python//python/private:text_util.bzl", "render") # buildifier: disable=bzl-visibility load("@rules_shell//shell:sh_test.bzl", "sh_test") copy_file( @@ -183,3 +188,37 @@ sh_test( "VERSION_PY_BINARY": "$(rootpath :version_3_10)", }, ) + +# The following test ensures that default toolchain versions are the same as in +# the TOOL_VERSIONS array. + +# NOTE @aignas 2024-10-26: This test here is to do a sanity check and not +# include extra dependencies - if rules_testing is included here, we can +# potentially uses `rules_testing` for a more lightweight test. +write_file( + name = "default_python_versions", + out = "default_python_versions.txt", + content = [ + "MINOR_MAPPING:", + render.dict(dict(sorted(DEFAULT_MINOR_MAPPING.items()))), + "PYTHON_VERSIONS:", + render.list(sorted(DEFAULT_TOOL_VERSIONS)), + ], +) + +write_file( + name = "pythons_hub_versions", + out = "pythons_hub_versions.txt", + content = [ + "MINOR_MAPPING:", + render.dict(dict(sorted(MINOR_MAPPING.items()))), + "PYTHON_VERSIONS:", + render.list(sorted(PYTHON_VERSIONS)), + ], +) + +diff_test( + name = "test_versions", + file1 = "default_python_versions", + file2 = "pythons_hub_versions", +) diff --git a/python/private/python.bzl b/python/private/python.bzl index 12ab4bb48d..d2b1007231 100644 --- a/python/private/python.bzl +++ b/python/private/python.bzl @@ -241,6 +241,7 @@ def _python_impl(module_ctx): # Last toolchain is default default_python_version = py.default_python_version, minor_mapping = py.config.minor_mapping, + python_versions = list(py.config.default["tool_versions"].keys()), toolchain_prefixes = [ render.toolchain_prefix(index, toolchain.name, _TOOLCHAIN_INDEX_PAD_LENGTH) for index, toolchain in enumerate(py.toolchains)