fix(toolchain): keep the strip_prefix when single_version_override has no urls (#4435) Keep the `strip_prefix` of the existing archives when `python.single_version_override` does not override the `urls`. Before this change, an override that only sets e.g. `libpython` or `distutils` reset the `strip_prefix` of every platform to the tag default `python`. Some versions (e.g. 3.13.0) use the `full` archives for the freethreaded builds, which need `python/install`, so such an override broke those toolchains. The `strip_prefix` from the tag is still used for the platforms whose `urls` and `sha256` are overridden. This is needed for #4434, which makes `single_version_override` the way to set the `coverage_tool` for all platforms of a version.
diff --git a/news/4435.fixed.md b/news/4435.fixed.md new file mode 100644 index 0000000..651cc86 --- /dev/null +++ b/news/4435.fixed.md
@@ -0,0 +1,3 @@ +(toolchains) {obj}`python.single_version_override` without `urls` no longer +resets the `strip_prefix` of the existing archives, which broke the +freethreaded toolchains that use the `full` archives.
diff --git a/python/private/python.bzl b/python/private/python.bzl index 6d8237b..0d392f8 100644 --- a/python/private/python.bzl +++ b/python/private/python.bzl
@@ -612,12 +612,18 @@ return sha256 = dict(tag.sha256) or available_versions[tag.python_version]["sha256"] - override = { - "sha256": sha256, - "strip_prefix": { + if tag.urls: + strip_prefix = { platform: tag.strip_prefix for platform in sha256 - }, + } + else: + # The archives are not overridden, so keep their strip_prefix, which + # differs between the archive flavors. + strip_prefix = available_versions[tag.python_version]["strip_prefix"] + override = { + "sha256": sha256, + "strip_prefix": strip_prefix, "url": { platform: list(tag.urls) for platform in tag.sha256
diff --git a/tests/python/python_tests.bzl b/tests/python/python_tests.bzl index e6956fa..e881feb 100644 --- a/tests/python/python_tests.bzl +++ b/tests/python/python_tests.bzl
@@ -16,6 +16,7 @@ load("@pythons_hub//:versions.bzl", "MINOR_MAPPING") load("@rules_testing//lib:test_suite.bzl", "test_suite") +load("//python:versions.bzl", "TOOL_VERSIONS") load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") # buildifier: disable=bzl-visibility load("//python/private:python.bzl", "parse_modules") # buildifier: disable=bzl-visibility load("//python/private:repo_utils.bzl", "repo_utils") # buildifier: disable=bzl-visibility @@ -441,6 +442,34 @@ _tests.append(_test_libpython_override) +def _test_single_version_override_keeps_strip_prefix(env): + # This version has `full` archives for the freethreaded builds, which use + # a different strip_prefix. + python_version = "3.13.0" + py = parse_modules( + module_ctx = python_ext.mctx( + python_ext.module( + name = "my_module", + is_root = True, + single_version_override = [ + python_ext.single_version_override( + libpython = "exclude", + python_version = python_version, + ), + ], + toolchain = [python_ext.toolchain(python_version = "3.13")], + ), + _rules_python_module(), + ), + logger = repo_utils.logger(verbosity_level = 0, name = "python"), + ) + + env.expect.that_dict( + py.config.default["tool_versions"][python_version]["strip_prefix"], + ).contains_exactly(TOOL_VERSIONS[python_version]["strip_prefix"]) + +_tests.append(_test_single_version_override_keeps_strip_prefix) + _ARCHIVE_FLAVOR_MANIFEST = """ 1111111111111111111111111111111111111111111111111111111111111111 20260414/cpython-3.14.4+20260414-x86_64-unknown-linux-gnu-install_only.tar.gz 2222222222222222222222222222222222222222222222222222222222222222 20260414/cpython-3.14.4+20260414-x86_64-unknown-linux-gnu-install_only_stripped.tar.gz