Skip to content

Commit b9a2c70

Browse files
authored
fix: flip unused_inputs=1 by default (#432)
### Changes are visible to end-users: yes/no - Searched for relevant documentation and updated as needed: yes - Breaking change (forces users to change their own code or config): no - Suggested release notes appear below: yes ### Test plan - New test cases added
1 parent 509ed22 commit b9a2c70

File tree

9 files changed

+2554
-20
lines changed

9 files changed

+2554
-20
lines changed

MODULE.bazel

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ module(
88

99
# Lower-bound versions of direct dependencies.
1010
# When bumping, add a comment explaining what's required from the newer release.
11-
bazel_dep(name = "aspect_bazel_lib", version = "2.9.1") # py_image_layer requires 2.x for the `tar` rule.
11+
12+
# py_image_layer requires 2.x for the `tar` rule.
13+
# py_image_layer needs compute_unused_inputs attribute
14+
# py_image_layer needs repo_mapping fix.
15+
bazel_dep(name = "aspect_bazel_lib", version = "2.9.4")
1216
bazel_dep(name = "bazel_skylib", version = "1.4.2")
1317
bazel_dep(name = "rules_python", version = "0.29.0")
1418
bazel_dep(name = "platforms", version = "0.0.7")

docs/py_image_layer.md

+8-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

py/private/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ bzl_library(
2727
srcs = ["py_image_layer.bzl"],
2828
deps = [
2929
"@aspect_bazel_lib//lib:tar",
30+
"@aspect_bazel_lib//lib:transitions",
3031
],
3132
)
3233

py/private/py_image_layer.bzl

+29-11
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,17 @@ oci_image(
3131
"""
3232

3333
load("@aspect_bazel_lib//lib:tar.bzl", "mtree_spec", "tar")
34+
load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_filegroup")
3435

3536
default_layer_groups = {
3637
# match *only* external pip like repositories that contain the string "site-packages"
37-
"packages": "\\.runfiles/.*/site-packages",
38+
"packages": "\\\\.runfiles/.*/site-packages",
3839
# match *only* external repositories that begins with the string "python"
3940
# e.g. this will match
4041
# `/hello_world/hello_world_bin.runfiles/rules_python~0.21.0~python~python3_9_aarch64-unknown-linux-gnu/bin/python3`
4142
# but not match
4243
# `/hello_world/hello_world_bin.runfiles/_main/python_app`
43-
"interpreter": "\\.runfiles/python.*-.*/",
44+
"interpreter": "\\\\.runfiles/.*python.*-.*/",
4445
}
4546

4647
def _split_mtree_into_layer_groups(name, root, groups, group_names, **kwargs):
@@ -90,7 +91,7 @@ awk < $< 'BEGIN {
9091
)
9192

9293

93-
def py_image_layer(name, py_binary, root = None, layer_groups = {}, compress = "gzip", tar_args = ["--options", "gzip:!timestamp"], **kwargs):
94+
def py_image_layer(name, binary, root = "/", layer_groups = {}, compress = "gzip", tar_args = ["--options", "gzip:!timestamp"], compute_unused_inputs = 1, platform = None, **kwargs):
9495
"""Produce a separate tar output for each layer of a python app
9596
9697
> Requires `awk` to be installed on the host machine/rbe runner.
@@ -113,11 +114,13 @@ def py_image_layer(name, py_binary, root = None, layer_groups = {}, compress = "
113114
114115
Args:
115116
name: base name for targets
116-
py_binary: a py_binary target
117+
binary: a py_binary target
117118
root: Path to where the layers should be rooted. If not specified, the layers will be rooted at the workspace root.
118119
layer_groups: Additional layer groups to create. They are used to group files into layers based on their path. In the form of: ```{"<name>": "regex_to_match_against_file_paths"}```
119-
compress: Compression algorithm to use. Default is gzip. See: https://github.com/bazel-contrib/bazel-lib/blob/main/docs/tar.md#tar_rule
120-
tar_args: Additional arguments to pass to the tar rule. Default is `["--options", "gzip:!timestamp"]`. See: https://github.com/bazel-contrib/bazel-lib/blob/main/docs/tar.md#tar_rule
120+
compress: Compression algorithm to use. Default is gzip. See: https://github.com/bazel-contrib/bazel-lib/blob/main/docs/tar.md#tar_rule-compress
121+
compute_unused_inputs: Whether to compute unused inputs. Default is 1. See: https://github.com/bazel-contrib/bazel-lib/blob/main/docs/tar.md#tar_rule-compute_unused_inputs
122+
platform: The platform to use for the transition. Default is None. See: https://github.com/bazel-contrib/bazel-lib/blob/main/docs/transitions.md#platform_transition_binary-target_platform
123+
tar_args: Additional arguments to pass to the tar rule. Default is `["--options", "gzip:!timestamp"]`. See: https://github.com/bazel-contrib/bazel-lib/blob/main/docs/tar.md#tar_rule-args
121124
**kwargs: attribute that apply to all targets expanded by the macro
122125
123126
Returns:
@@ -130,7 +133,7 @@ def py_image_layer(name, py_binary, root = None, layer_groups = {}, compress = "
130133
# into fine-grained layers for better pull, push and remote cache performance.
131134
mtree_spec(
132135
name = name + ".manifest",
133-
srcs = [py_binary],
136+
srcs = [binary],
134137
**kwargs
135138
)
136139

@@ -141,17 +144,32 @@ def py_image_layer(name, py_binary, root = None, layer_groups = {}, compress = "
141144
_split_mtree_into_layer_groups(name, root, groups, group_names, **kwargs)
142145

143146
# Finally create layers using the tar rule
144-
result = []
147+
srcs = []
145148
for group_name in group_names:
146149
tar_target = "_{}_{}".format(name, group_name)
147150
tar(
148151
name = tar_target,
149-
srcs = [py_binary],
152+
srcs = [binary],
150153
mtree = "{}.{}.manifest.spec".format(name, group_name),
151154
compress = compress,
155+
compute_unused_inputs = compute_unused_inputs,
152156
args = tar_args,
153157
**kwargs
154158
)
155-
result.append(tar_target)
159+
srcs.append(tar_target)
160+
161+
if platform:
162+
platform_transition_filegroup(
163+
name = name,
164+
srcs = srcs,
165+
target_platform = platform,
166+
**kwargs
167+
)
168+
else:
169+
native.filegroup(
170+
name = name,
171+
srcs = srcs,
172+
**kwargs
173+
)
156174

157-
return result
175+
return srcs

py/repositories.bzl

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ def rules_py_dependencies():
3333
# py_image_layer requires 2.x for the `tar` rule.
3434
http_archive(
3535
name = "aspect_bazel_lib",
36-
sha256 = "f93d386d8d0b0149031175e81df42a488be4267c3ca2249ba5321c23c60bc1f0",
37-
strip_prefix = "bazel-lib-2.9.1",
38-
url = "https://github.com/bazel-contrib/bazel-lib/releases/download/v2.9.1/bazel-lib-v2.9.1.tar.gz",
36+
sha256 = "349aabd3c2b96caeda6181eb0ae1f14f2a1d9f3cd3c8b05d57f709ceb12e9fb3",
37+
strip_prefix = "bazel-lib-2.9.4",
38+
url = "https://github.com/bazel-contrib/bazel-lib/releases/download/v2.9.4/bazel-lib-v2.9.4.tar.gz",
3939
)
4040

4141
http_archive(

py/tests/py_image_layer/BUILD.bazel

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
load("//py:defs.bzl", "py_image_layer", "py_binary")
2+
load("asserts.bzl", "assert_tar_listing")
3+
4+
platform(
5+
name = "linux_amd64",
6+
constraint_values = [
7+
"@platforms//os:linux",
8+
"@platforms//cpu:x86_64",
9+
],
10+
)
11+
12+
13+
# Case 1: Basic usage
14+
py_binary(
15+
name = "my_app_bin",
16+
srcs = ["main.py"],
17+
)
18+
19+
py_image_layer(
20+
name = "my_app_layers",
21+
binary = ":my_app_bin",
22+
platform = ":linux_amd64",
23+
)
24+
25+
assert_tar_listing(
26+
name = "my_app_layers_test",
27+
actual = [":my_app_layers"],
28+
expected = ":my_app_layers.listing",
29+
)

py/tests/py_image_layer/asserts.bzl

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_file")
2+
3+
# buildifier: disable=function-docstring
4+
def assert_tar_listing(name, actual, expected):
5+
actual_listing = "_{}_listing".format(name)
6+
native.genrule(
7+
name = actual_listing,
8+
srcs = actual,
9+
testonly = True,
10+
outs = ["_{}.listing".format(name)],
11+
cmd = 'echo $(SRCS) | TZ="UTC" LC_ALL="en_US.UTF-8" xargs -n 1 $(BSDTAR_BIN) --exclude "*/_repo_mapping" --exclude "**/tools/venv_bin/**" -tvf > $@'.format(actual),
12+
toolchains = ["@bsd_tar_toolchains//:resolved_toolchain"],
13+
)
14+
15+
write_source_file(
16+
name = name,
17+
in_file = actual_listing,
18+
out_file = expected,
19+
testonly = True,
20+
tags = ["skip-on-bazel6"],
21+
)

py/tests/py_image_layer/main.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("Hello!!")

0 commit comments

Comments
 (0)