Skip to content

Commit 67ec8d5

Browse files
authored
fix: Fix zipapp compression support for new py_zipapp_binary target (#3653)
The compression level isn't being correctly passed by the zipapp rule to the zipper program, resulting in an error if custom compression is specified. To fix, pass the arg correctly to the zipper. `venv_zipapp_test.py` is updated to also confirm zipapps are properly compressed when configured in the BUILD target. Fixes #3646
1 parent 6c05d2d commit 67ec8d5

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ END_UNRELEASED_TEMPLATE
6363

6464
{#v0-0-0-fixed}
6565
### Fixed
66-
* Nothing fixed.
66+
* (zipapp) Resolve issue passing through compression settings in
67+
`py_zippapp_binary` targets. ([#3646](https://github.com/bazel-contrib/rules_python/issues/3646))
6768

6869
{#v0-0-0-added}
6970
### Added

python/private/zipapp/py_zipapp_rule.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def _create_zip(ctx, py_runtime, py_executable, stage2_bootstrap):
112112
format = "--legacy-external-runfiles=%s",
113113
)
114114
if ctx.attr.compression:
115-
zipper_args.add(ctx.attr.compression, "--compression=%s")
115+
zipper_args.add(ctx.attr.compression, format = "--compression=%s")
116116
zipper_args.add("--runfiles-dir=runfiles")
117117

118118
actions_run(

tests/py_zipapp/BUILD.bazel

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,27 @@ py_test(
3838
target_compatible_with = NOT_WINDOWS,
3939
)
4040

41+
# Create the app with a supported level of compression
42+
py_zipapp_binary(
43+
name = "venv_zipapp_compressed",
44+
binary = ":venv_bin",
45+
compression = "4",
46+
target_compatible_with = NOT_WINDOWS,
47+
)
48+
49+
py_test(
50+
name = "venv_zipapp_compressed_test",
51+
srcs = ["venv_zipapp_test.py"],
52+
data = [":venv_zipapp_compressed"],
53+
env = {
54+
"BZLMOD_ENABLED": str(int(BZLMOD_ENABLED)),
55+
"COMPRESSED": "1",
56+
"TEST_ZIPAPP": "$(location :venv_zipapp_compressed)",
57+
},
58+
main = "venv_zipapp_test.py",
59+
target_compatible_with = NOT_WINDOWS,
60+
)
61+
4162
py_binary(
4263
name = "system_python_bin",
4364
srcs = ["main.py"],

tests/py_zipapp/venv_zipapp_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ def test_zipapp_structure(self):
7171
zipapp_path = os.environ["TEST_ZIPAPP"]
7272

7373
with self._open_zipapp(zipapp_path) as zf:
74+
info = zf.infolist()[0]
75+
if os.getenv("COMPRESSED", "0") == "1":
76+
self.assertEqual(info.compress_type, zipfile.ZIP_DEFLATED)
77+
else:
78+
self.assertEqual(info.compress_type, zipfile.ZIP_STORED)
79+
7480
namelist = zf.namelist()
7581

7682
if self._is_bzlmod_enabled():

0 commit comments

Comments
 (0)