From bd4c205989b15546c2d5df2a798cd27ef22c9bb2 Mon Sep 17 00:00:00 2001 From: Julian Gautier <81595639+jgautier-dd@users.noreply.github.com> Date: Thu, 12 Nov 2026 11:10:06 -0800 Subject: [PATCH 1/3] fix(pypi): repack patched wheels with Bazel's zipper when available MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Patching a wheel with `pip.override` repacks it with the host `zip`, so the repository rule fails on a host without a `zip` binary, such as a minimal container. To fix, use Bazel's bundled zipper on non-Windows hosts when it is present, and keep `zip` as the fallback. The file list goes through a params file, because the zipper takes `name=path` entries. Paths it can't express (containing `=` or a newline) use `zip`. The zipper writes a fixed timestamp, so the repacked wheel is also reproducible. * Adds `zipper_params_content` and unit tests for it 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- news/repack-whl-zipper.fixed.md | 1 + python/private/pypi/patch_whl.bzl | 46 ++++++++++++++++++++++-- tests/pypi/patch_whl/patch_whl_tests.bzl | 18 +++++++++- 3 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 news/repack-whl-zipper.fixed.md diff --git a/news/repack-whl-zipper.fixed.md b/news/repack-whl-zipper.fixed.md new file mode 100644 index 0000000000..2098035f32 --- /dev/null +++ b/news/repack-whl-zipper.fixed.md @@ -0,0 +1 @@ +(pypi) Wheels patched with `pip.override` are repacked with Bazel's zipper when it is available, so hosts without a `zip` binary no longer fail. The repacked wheel also has fixed timestamps. diff --git a/python/private/pypi/patch_whl.bzl b/python/private/pypi/patch_whl.bzl index b257f2b4fe..fcf660e71c 100644 --- a/python/private/pypi/patch_whl.bzl +++ b/python/private/pypi/patch_whl.bzl @@ -16,7 +16,8 @@ This replaces the previous approach that depended on running Python to repack patched wheels. Instead, after extracting and patching, we fix -the RECORD file natively in Starlark and repack using system zip. +the RECORD file natively in Starlark and repack using Bazel's zipper, or +system zip when the zipper is not available. """ load("@rules_python_internal//:rules_python_config.bzl", rp_config = "config") @@ -80,7 +81,8 @@ def patch_whl(rctx, *, whl_path, patches): The wheel is extracted, patched, missing RECORD entries are added with dummy sha256 and size values, and finally the wheel is - repacked using system zip. + repacked using Bazel's zipper, or system zip when the zipper is not + available. Args: rctx: repository_ctx @@ -222,14 +224,40 @@ def _collect_files(rctx): return result def _repack_whl(rctx, *, output): - """Repack the current directory into a wheel file using system zip.""" + """Repack the current directory into a wheel file.""" os_name = repo_utils.get_platforms_os_name(rctx) if os_name == "windows": _repack_whl_windows(rctx, output = output) else: _repack_whl_unix(rctx, output = output) +def zipper_params_content(files): + """Returns the params file content that makes Bazel's zipper pack `files`. + + The zipper reads one `=` entry per line and + has no way to quote a name, so a path with `=` or a newline can't be passed. + + Args: + files: {type}`list[str]` file paths relative to the repo root. + + Returns: + {type}`str | None` the content, with the entries sorted so the + result is stable, or `None` if a path can't be expressed. + """ + for path in files: + if "=" in path or "\n" in path: + return None + return "".join(["{0}={0}\n".format(path) for path in sorted(files)]) + def _repack_whl_unix(rctx, *, output): + # Bazel's zipper doesn't need a `zip` binary on the host, and it writes a + # fixed timestamp so the repacked wheel is reproducible. + zipper = rctx.path(Label("@bazel_tools//tools/zip:zipper/zipper")) + params = zipper_params_content(_collect_files(rctx).keys()) if zipper.exists else None + if params != None: + _repack_whl_zipper(rctx, zipper = zipper, params = params, output = output) + return + repo_utils.execute_checked( rctx, op = "PatchWhl", @@ -243,6 +271,18 @@ def _repack_whl_unix(rctx, *, output): ], ) +def _repack_whl_zipper(rctx, *, zipper, params, output): + # The paths go through a params file because a wheel can have more files + # than fit on one command line. + params_path = "zipper_params.txt" + rctx.file(params_path, params) + repo_utils.execute_checked( + rctx, + op = "PatchWhl", + arguments = [str(zipper), "c", str(output), "@" + params_path], + ) + rctx.delete(params_path) + def _repack_whl_windows(rctx, *, output): powershell_exe = rctx.which("powershell.exe") or rctx.which("powershell") if not powershell_exe: diff --git a/tests/pypi/patch_whl/patch_whl_tests.bzl b/tests/pypi/patch_whl/patch_whl_tests.bzl index 5010f03dc0..3030799cb2 100644 --- a/tests/pypi/patch_whl/patch_whl_tests.bzl +++ b/tests/pypi/patch_whl/patch_whl_tests.bzl @@ -15,7 +15,7 @@ "" load("@rules_testing//lib:test_suite.bzl", "test_suite") -load("//python/private/pypi:patch_whl.bzl", "fix_record_content", "patched_whl_name") # buildifier: disable=bzl-visibility +load("//python/private/pypi:patch_whl.bzl", "fix_record_content", "patched_whl_name", "zipper_params_content") # buildifier: disable=bzl-visibility _tests = [] @@ -129,6 +129,22 @@ foo-1.0.dist-info/RECORD,, _tests.append(_test_fix_record_skips_whl_files) +def _test_zipper_params_sorted(env): + got = zipper_params_content(["foo/b.py", "foo-1.0.dist-info/RECORD", "foo/__init__.py"]) + env.expect.that_str(got).equals("""\ +foo-1.0.dist-info/RECORD=foo-1.0.dist-info/RECORD +foo/__init__.py=foo/__init__.py +foo/b.py=foo/b.py +""") + +_tests.append(_test_zipper_params_sorted) + +def _test_zipper_params_unsupported_names(env): + env.expect.that_bool(zipper_params_content(["foo/a=b.py"]) == None).equals(True) + env.expect.that_bool(zipper_params_content(["foo/a\nb.py"]) == None).equals(True) + +_tests.append(_test_zipper_params_unsupported_names) + def patch_whl_test_suite(name): """Create the test suite. From 83e049e740b2cf42bde8b33ab83c4c5102e97a67 Mon Sep 17 00:00:00 2001 From: Richard Levasseur Date: Sat, 14 Nov 2026 14:22:26 -0800 Subject: [PATCH 2/3] refactor(pypi): address style and changelog review feedback Wrap Starlark lines within 80 columns, document the uncompressed `c` mode passed to Bazel's zipper, and condense the news entry to focus on the user-visible outcome. --- news/repack-whl-zipper.fixed.md | 2 +- python/private/pypi/patch_whl.bzl | 21 ++++++++++++++++----- tests/pypi/patch_whl/patch_whl_tests.bzl | 19 +++++++++++++++---- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/news/repack-whl-zipper.fixed.md b/news/repack-whl-zipper.fixed.md index 2098035f32..21bafa835a 100644 --- a/news/repack-whl-zipper.fixed.md +++ b/news/repack-whl-zipper.fixed.md @@ -1 +1 @@ -(pypi) Wheels patched with `pip.override` are repacked with Bazel's zipper when it is available, so hosts without a `zip` binary no longer fail. The repacked wheel also has fixed timestamps. +(pypi) Hosts without a native `zip` binary can now patch wheels. diff --git a/python/private/pypi/patch_whl.bzl b/python/private/pypi/patch_whl.bzl index fcf660e71c..3d004d5110 100644 --- a/python/private/pypi/patch_whl.bzl +++ b/python/private/pypi/patch_whl.bzl @@ -253,10 +253,16 @@ def _repack_whl_unix(rctx, *, output): # Bazel's zipper doesn't need a `zip` binary on the host, and it writes a # fixed timestamp so the repacked wheel is reproducible. zipper = rctx.path(Label("@bazel_tools//tools/zip:zipper/zipper")) - params = zipper_params_content(_collect_files(rctx).keys()) if zipper.exists else None - if params != None: - _repack_whl_zipper(rctx, zipper = zipper, params = params, output = output) - return + if zipper.exists: + params = zipper_params_content(_collect_files(rctx).keys()) + if params != None: + _repack_whl_zipper( + rctx, + zipper = zipper, + params = params, + output = output, + ) + return repo_utils.execute_checked( rctx, @@ -279,7 +285,12 @@ def _repack_whl_zipper(rctx, *, zipper, params, output): repo_utils.execute_checked( rctx, op = "PatchWhl", - arguments = [str(zipper), "c", str(output), "@" + params_path], + arguments = [ + str(zipper), + "c", # Create uncompressed archive (omits C). + str(output), + "@" + params_path, + ], ) rctx.delete(params_path) diff --git a/tests/pypi/patch_whl/patch_whl_tests.bzl b/tests/pypi/patch_whl/patch_whl_tests.bzl index 3030799cb2..9577605c89 100644 --- a/tests/pypi/patch_whl/patch_whl_tests.bzl +++ b/tests/pypi/patch_whl/patch_whl_tests.bzl @@ -15,7 +15,12 @@ "" load("@rules_testing//lib:test_suite.bzl", "test_suite") -load("//python/private/pypi:patch_whl.bzl", "fix_record_content", "patched_whl_name", "zipper_params_content") # buildifier: disable=bzl-visibility +load( + "//python/private/pypi:patch_whl.bzl", + "fix_record_content", + "patched_whl_name", + "zipper_params_content", +) # buildifier: disable=bzl-visibility _tests = [] @@ -130,7 +135,11 @@ foo-1.0.dist-info/RECORD,, _tests.append(_test_fix_record_skips_whl_files) def _test_zipper_params_sorted(env): - got = zipper_params_content(["foo/b.py", "foo-1.0.dist-info/RECORD", "foo/__init__.py"]) + got = zipper_params_content([ + "foo/b.py", + "foo-1.0.dist-info/RECORD", + "foo/__init__.py", + ]) env.expect.that_str(got).equals("""\ foo-1.0.dist-info/RECORD=foo-1.0.dist-info/RECORD foo/__init__.py=foo/__init__.py @@ -140,8 +149,10 @@ foo/b.py=foo/b.py _tests.append(_test_zipper_params_sorted) def _test_zipper_params_unsupported_names(env): - env.expect.that_bool(zipper_params_content(["foo/a=b.py"]) == None).equals(True) - env.expect.that_bool(zipper_params_content(["foo/a\nb.py"]) == None).equals(True) + got_eq = zipper_params_content(["foo/a=b.py"]) + env.expect.that_bool(got_eq == None).equals(True) + got_nl = zipper_params_content(["foo/a\nb.py"]) + env.expect.that_bool(got_nl == None).equals(True) _tests.append(_test_zipper_params_unsupported_names) From 8e95d84990744a29943e971758dcde975a9fe6c2 Mon Sep 17 00:00:00 2001 From: Richard Levasseur Date: Sat, 14 Nov 2026 14:52:08 -0800 Subject: [PATCH 3/3] refactor(pypi): drop dead zipper.exists check on Unix All Unix Bazel builds (including source/distro bazel_nojdk builds) embed @bazel_tools//tools/zip:zipper/zipper in embedded_tools, so drop the unreachable zipper.exists guard and update the docstrings to reflect that host zip is only used when paths contain '=' or newlines. --- python/private/pypi/patch_whl.bzl | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/python/private/pypi/patch_whl.bzl b/python/private/pypi/patch_whl.bzl index 3d004d5110..d70c674b94 100644 --- a/python/private/pypi/patch_whl.bzl +++ b/python/private/pypi/patch_whl.bzl @@ -16,8 +16,8 @@ This replaces the previous approach that depended on running Python to repack patched wheels. Instead, after extracting and patching, we fix -the RECORD file natively in Starlark and repack using Bazel's zipper, or -system zip when the zipper is not available. +the RECORD file natively in Starlark and repack using Bazel's zipper +(falling back to system zip when paths cannot be passed to zipper). """ load("@rules_python_internal//:rules_python_config.bzl", rp_config = "config") @@ -81,8 +81,8 @@ def patch_whl(rctx, *, whl_path, patches): The wheel is extracted, patched, missing RECORD entries are added with dummy sha256 and size values, and finally the wheel is - repacked using Bazel's zipper, or system zip when the zipper is not - available. + repacked using Bazel's zipper (falling back to system zip when paths + cannot be passed to zipper). Args: rctx: repository_ctx @@ -253,16 +253,15 @@ def _repack_whl_unix(rctx, *, output): # Bazel's zipper doesn't need a `zip` binary on the host, and it writes a # fixed timestamp so the repacked wheel is reproducible. zipper = rctx.path(Label("@bazel_tools//tools/zip:zipper/zipper")) - if zipper.exists: - params = zipper_params_content(_collect_files(rctx).keys()) - if params != None: - _repack_whl_zipper( - rctx, - zipper = zipper, - params = params, - output = output, - ) - return + params = zipper_params_content(_collect_files(rctx).keys()) + if params != None: + _repack_whl_zipper( + rctx, + zipper = zipper, + params = params, + output = output, + ) + return repo_utils.execute_checked( rctx,