From 0eb00687b4e7c4cb17c1f9b9e921cfd1141783a5 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Mon, 20 Apr 2026 18:33:14 +0900 Subject: [PATCH 1/4] fix(pypi): build the environment on the fly At some point when we started using pipstar, we silently started building the python environment with system python interpreter. This is fine because the environment in those code paths would be unused, but this would break if python was not present on the machine. This PR fixes this by creating the environment on the fly with a little bit of code duplication. Fixes #3712 --- CHANGELOG.md | 2 ++ python/private/pypi/whl_library.bzl | 9 ++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89b8356f00..becd89f2b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -126,6 +126,8 @@ Other changes: ) ``` Fixes [#3676](https://github.com/bazel-contrib/rules_python/issues/3676). +* (pypi) Fixes wheel extraction on hosts without python installed, + Fixes [#3712](https://github.com/bazel-contrib/rules_python/issues/3712). {#v2-0-0-added} ### Added diff --git a/python/private/pypi/whl_library.bzl b/python/private/pypi/whl_library.bzl index 31da3b94cb..10a031ac6d 100644 --- a/python/private/pypi/whl_library.bzl +++ b/python/private/pypi/whl_library.bzl @@ -306,9 +306,6 @@ def _whl_library_impl(rctx): extra_pip_args = [] extra_pip_args.extend(rctx.attr.extra_pip_args) - # Manually construct the PYTHONPATH since we cannot use the toolchain here - environment = _create_repository_execution_environment(rctx, python_interpreter, logger = logger) - whl_path = None sdist_filename = None if rctx.attr.whl_file: @@ -377,7 +374,8 @@ def _whl_library_impl(rctx): python = python_interpreter, op = op_tmpl.format(name = rctx.attr.name, requirement = rctx.attr.requirement.split(" ", 1)[0]), arguments = args, - environment = environment, + # Manually construct the PYTHONPATH since we cannot use the toolchain here + environment = _create_repository_execution_environment(rctx, python_interpreter, logger = logger), srcs = rctx.attr._python_srcs, quiet = rctx.attr.quiet, timeout = rctx.attr.timeout, @@ -414,7 +412,8 @@ def _whl_library_impl(rctx): python_interpreter = python_interpreter, args = args, whl_path = whl_path, - environment = environment, + # Manually construct the PYTHONPATH since we cannot use the toolchain here + environment = _create_repository_execution_environment(rctx, python_interpreter, logger = logger), logger = logger, ) From 9529e092c2ce2b2c9870bdd9fea7bd8a86315343 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Mon, 20 Apr 2026 18:42:16 +0900 Subject: [PATCH 2/4] comment --- python/private/pypi/whl_library.bzl | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/python/private/pypi/whl_library.bzl b/python/private/pypi/whl_library.bzl index 10a031ac6d..8fe1f22afd 100644 --- a/python/private/pypi/whl_library.bzl +++ b/python/private/pypi/whl_library.bzl @@ -358,6 +358,12 @@ def _whl_library_impl(rctx): enable_pipstar = (rp_config.enable_pipstar or whl_path) and rctx.attr.config_load enable_pipstar_extract = enable_pipstar and rp_config.bazel_8_or_later + if enable_pipstar_extract: + environment = {} + else: + # Manually construct the PYTHONPATH since we cannot use the toolchain here + environment = _create_repository_execution_environment(rctx, python_interpreter, logger = logger) + if not whl_path: if rctx.attr.urls: op_tmpl = "whl_library.BuildWheelFromSource({name}, {requirement})" @@ -374,8 +380,7 @@ def _whl_library_impl(rctx): python = python_interpreter, op = op_tmpl.format(name = rctx.attr.name, requirement = rctx.attr.requirement.split(" ", 1)[0]), arguments = args, - # Manually construct the PYTHONPATH since we cannot use the toolchain here - environment = _create_repository_execution_environment(rctx, python_interpreter, logger = logger), + environment = environment, srcs = rctx.attr._python_srcs, quiet = rctx.attr.quiet, timeout = rctx.attr.timeout, @@ -412,8 +417,7 @@ def _whl_library_impl(rctx): python_interpreter = python_interpreter, args = args, whl_path = whl_path, - # Manually construct the PYTHONPATH since we cannot use the toolchain here - environment = _create_repository_execution_environment(rctx, python_interpreter, logger = logger), + environment = environment, logger = logger, ) From a111a5a57448c55e142390aa7eb7babbfb0741a2 Mon Sep 17 00:00:00 2001 From: Richard Levasseur Date: Mon, 20 Apr 2026 08:44:54 -0700 Subject: [PATCH 3/4] comment why env is empty for pipstar --- python/private/pypi/whl_library.bzl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/python/private/pypi/whl_library.bzl b/python/private/pypi/whl_library.bzl index 8fe1f22afd..91093e6049 100644 --- a/python/private/pypi/whl_library.bzl +++ b/python/private/pypi/whl_library.bzl @@ -358,6 +358,8 @@ def _whl_library_impl(rctx): enable_pipstar = (rp_config.enable_pipstar or whl_path) and rctx.attr.config_load enable_pipstar_extract = enable_pipstar and rp_config.bazel_8_or_later + # When pipstar is enabled, Python isn't used, so there's no need + # to setup env vars to run Python. if enable_pipstar_extract: environment = {} else: From ba73f2c84e9a4a3e8272a536fe3c0be2fad47a44 Mon Sep 17 00:00:00 2001 From: Ignas Anikevicius <240938+aignas@users.noreply.github.com> Date: Tue, 21 Apr 2026 08:05:47 +0900 Subject: [PATCH 4/4] fixup for sdists --- python/private/pypi/whl_library.bzl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/private/pypi/whl_library.bzl b/python/private/pypi/whl_library.bzl index 91093e6049..5db4f12d67 100644 --- a/python/private/pypi/whl_library.bzl +++ b/python/private/pypi/whl_library.bzl @@ -359,8 +359,8 @@ def _whl_library_impl(rctx): enable_pipstar_extract = enable_pipstar and rp_config.bazel_8_or_later # When pipstar is enabled, Python isn't used, so there's no need - # to setup env vars to run Python. - if enable_pipstar_extract: + # to setup env vars to run Python, unless we need to build an sdist + if enable_pipstar_extract and whl_path: environment = {} else: # Manually construct the PYTHONPATH since we cannot use the toolchain here