Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: include repo mapping in the sources #2011

Merged
merged 5 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion js/private/js_image_layer.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ def _select_layer(layers, destination, file):
return layers.node_modules
return layers.app

def _repo_mapping_manifest(files_to_run):
return getattr(files_to_run, "repo_mapping_manifest", None)

def _js_image_layer_impl(ctx):
if len(ctx.attr.binary) != 1:
fail("binary attribute has more than one transition")
Expand All @@ -343,7 +346,12 @@ def _js_image_layer_impl(ctx):
real_binary_path = _runfile_path(ctx, binary_default_info.files_to_run.executable, runfiles_dir)
launcher = _write_laucher(ctx, real_binary_path)

all_files = depset(transitive = [binary_default_info.files, binary_default_info.default_runfiles.files])
repo_mapping = _repo_mapping_manifest(binary_default_info.files_to_run)

all_files = depset(
[repo_mapping] if repo_mapping else [],
transitive = [binary_default_info.files, binary_default_info.default_runfiles.files],
)
all_entries = {}

layers = struct(
Expand Down
97 changes: 15 additions & 82 deletions js/private/test/image/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_file")
load("@npm//:defs.bzl", "npm_link_all_packages")
load("//js:defs.bzl", "js_binary")
load(":asserts.bzl", "assert_tar_listing", "make_js_image_layer")
load(":asserts.bzl", "assert_checksum", "assert_js_image_layer_listings", "make_js_image_layer")

npm_link_all_packages(name = "node_modules")

Expand All @@ -21,6 +20,12 @@ platform(
],
)

# All these golden files can be updated with
# bazel run //js/private/test/image:default_test_update_all --enable_bzlmod
# bazel run //js/private/test/image:custom_owner_test_update_all --enable_bzlmod
# bazel run //js/private/test/image:default_test_update_all --noenable_bzlmod
# bazel run //js/private/test/image:custom_owner_test_update_all --noenable_bzlmod

# Case 0: reproducibility guarantees
make_js_image_layer(
name = "cksum",
Expand All @@ -33,31 +38,9 @@ make_js_image_layer(
root = "/app",
)

genrule(
name = "checksum_gen",
testonly = True,
srcs = [":cksum"],
outs = ["checksum_generated"],
cmd = """
COREUTILS_BIN=$$(realpath $(COREUTILS_BIN)) &&
cd $(BINDIR) && $$COREUTILS_BIN sha256sum $(rootpaths :cksum) > $(rootpaths checksum_generated)
""",
output_to_bindir = True,
toolchains = ["@coreutils_toolchains//:resolved_toolchain"],
)

write_source_file(
assert_checksum(
name = "checksum_test",
testonly = True,
in_file = ":checksum_gen",
out_file = "checksum.expected",
# Under bazel6 bzlmod name differs
tags = ["skip-on-bazel6"],
# Under bzlmod workspace name is a fixed string `_main` which differs from WORKSPACE
target_compatible_with = select({
"@aspect_bazel_lib//lib:bzlmod": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
image_layer = ":cksum",
)

# Case 1: Defaults
Expand All @@ -68,34 +51,9 @@ make_js_image_layer(
root = "/app",
)

assert_tar_listing(
name = "assert_default_node_layer",
actual = "default_node_layer",
expected = "default_node.listing",
)

assert_tar_listing(
name = "assert_default_package_store_3p_layer",
actual = "default_package_store_3p_layer",
expected = "default_package_store_3p.listing",
)

assert_tar_listing(
name = "assert_default_package_store_1p_layer",
actual = "default_package_store_1p_layer",
expected = "default_package_store_1p.listing",
)

assert_tar_listing(
name = "assert_default_node_modules_layer",
actual = "default_node_modules_layer",
expected = "default_node_modules.listing",
)

assert_tar_listing(
name = "assert_default_app_layer",
actual = "default_app_layer",
expected = "default_app.listing",
assert_js_image_layer_listings(
name = "default_test",
js_image_layer = ":default",
)

# Case 2: Change owner
Expand All @@ -107,32 +65,7 @@ make_js_image_layer(
root = "/app",
)

assert_tar_listing(
name = "assert_custom_owner_node_layer",
actual = "custom_owner_node_layer",
expected = "custom_owner_node.listing",
)

assert_tar_listing(
name = "assert_custom_owner_package_store_3p_layer",
actual = "custom_owner_package_store_3p_layer",
expected = "custom_owner_package_store_3p.listing",
)

assert_tar_listing(
name = "assert_custom_owner_package_store_1p_layer",
actual = "custom_owner_package_store_1p_layer",
expected = "custom_owner_package_store_1p.listing",
)

assert_tar_listing(
name = "assert_custom_owner_node_modules_layer",
actual = "custom_owner_node_modules_layer",
expected = "custom_owner_node_modules.listing",
)

assert_tar_listing(
name = "assert_custom_owner_app_layer",
actual = "custom_owner_app_layer",
expected = "custom_owner_app.listing",
assert_js_image_layer_listings(
name = "custom_owner_test",
js_image_layer = ":custom_owner",
)
129 changes: 78 additions & 51 deletions js/private/test/image/asserts.bzl
Original file line number Diff line number Diff line change
@@ -1,8 +1,59 @@
"Make shorter assertions"

load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_file")
load("@aspect_bazel_lib//lib:utils.bzl", "is_bzlmod_enabled")
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_file", "write_source_files")
load("//js:defs.bzl", "js_image_layer")

# buildifier: disable=function-docstring
def assert_tar_listing(name, actual, expected):
actual_listing = "_{}_listing".format(name)
native.genrule(
name = actual_listing,
srcs = [actual],
testonly = True,
outs = ["_{}.listing".format(name)],
cmd = 'TZ="UTC" LC_ALL="en_US.UTF-8" $(BSDTAR_BIN) -tvf $(execpath {}) >$@'.format(actual),
toolchains = ["@bsd_tar_toolchains//:resolved_toolchain"],
)

write_source_file(
name = name,
in_file = actual_listing,
out_file = expected,
testonly = True,
tags = ["skip-on-bazel6"],
)

layers = [
"node",
"package_store_1p",
"package_store_3p",
"node_modules",
# TODO: now that app layer has repo_mapping file in it which is not stable between different operating systems
# we need to exlude it from checksums
# See: https://github.com/aspect-build/rules_js/actions/runs/11749187598/job/32734931009?pr=2011
# "app",
]

# buildifier: disable=function-docstring
def assert_js_image_layer_listings(name, js_image_layer):
for layer in layers:
assert_tar_listing(
name = "assert_{}_{}".format(name, layer),
actual = "{}_{}".format(js_image_layer, layer),
expected = "{}_{}{}.listing".format(name, layer, (".bzlmod" if is_bzlmod_enabled() else ".nobzlmod")),
)

write_source_files(
name = name + "_update_all",
additional_update_targets = [
"assert_{}_{}".format(name, layer)
for layer in layers
],
tags = ["skip-on-bazel6"],
testonly = True,
)

# buildifier: disable=function-docstring
def make_js_image_layer(name, **kwargs):
js_image_layer(
Expand All @@ -16,61 +67,37 @@ def make_js_image_layer(name, **kwargs):
**kwargs
)

native.filegroup(
name = name + "_node_layer",
srcs = [name],
output_group = "node",
testonly = 1,
)

native.filegroup(
name = name + "_package_store_3p_layer",
srcs = [name],
output_group = "package_store_3p",
testonly = 1,
)

native.filegroup(
name = name + "_package_store_1p_layer",
srcs = [name],
output_group = "package_store_1p",
testonly = 1,
)

native.filegroup(
name = name + "_node_modules_layer",
srcs = [name],
output_group = "node_modules",
testonly = 1,
)

native.filegroup(
name = name + "_app_layer",
srcs = [name],
output_group = "app",
testonly = 1,
)
for layer in layers:
native.filegroup(
name = name + "_" + layer,
srcs = [name],
output_group = layer,
testonly = 1,
)

# buildifier: disable=function-docstring
def assert_tar_listing(name, actual, expected):
actual_listing = "_{}_listing".format(name)
def assert_checksum(name, image_layer):
native.genrule(
name = actual_listing,
srcs = [actual],
name = name,
testonly = True,
outs = ["_{}.listing".format(name)],
cmd = 'TZ="UTC" LC_ALL="en_US.UTF-8" $(BSDTAR_BIN) -tvf $(execpath {}) >$@'.format(actual),
toolchains = ["@bsd_tar_toolchains//:resolved_toolchain"],
srcs = [image_layer],
outs = [name + ".checksums"],
# TODO: now that app layer has repo_mapping file in it which is not stable between different operating systems
# we need to exlude it from checksums
# See: https://github.com/aspect-build/rules_js/actions/runs/11749187598/job/32734931009?pr=2011
cmd = """
COREUTILS_BIN=$$(realpath $(COREUTILS_BIN)) &&
RESULT="$$($$COREUTILS_BIN sha256sum $(SRCS))"
BINDIR="$(BINDIR)/"
echo "$${RESULT//$$BINDIR/}" | $$COREUTILS_BIN head -n -1 > $@
""",
output_to_bindir = True,
toolchains = ["@coreutils_toolchains//:resolved_toolchain"],
)

write_source_file(
name = name,
in_file = actual_listing,
out_file = expected,
name = name + "_test",
testonly = True,
# TODO: js_image_layer is broken with bzlmod https://github.com/aspect-build/rules_js/issues/1530
target_compatible_with = select({
"@aspect_bazel_lib//lib:bzlmod": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
in_file = name,
out_file = name + ("." if is_bzlmod_enabled() else ".no") + "bzlmod.expected",
tags = ["skip-on-bazel6"],
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
f600a91e4d70057892e2a548d9cc6072a2afce6e091afa97184ee159bd012a13 js/private/test/image/cksum_app.tar
c603eafdacc184f3df28ef966704cd580bf5f53c7d599f77132abaac45c110fc js/private/test/image/cksum_node.tar
fbd40000f29527fb494ba83172bf55ecb01e01b29a8ff4727ead46e4ebf745f1 js/private/test/image/cksum_node_modules.tar
9395e137de4767edfdd663bc6010a597844bdb68663f8b3ecb036175122bcb2f js/private/test/image/cksum_package_store_1p.tar
eec022801c54978ece38859f525f6f7114dcf743ea5e4564ef2a8a522d51aeab js/private/test/image/cksum_package_store_3p.tar
9395e137de4767edfdd663bc6010a597844bdb68663f8b3ecb036175122bcb2f js/private/test/image/cksum_package_store_1p.tar
fbd40000f29527fb494ba83172bf55ecb01e01b29a8ff4727ead46e4ebf745f1 js/private/test/image/cksum_node_modules.tar
4 changes: 4 additions & 0 deletions js/private/test/image/checksum_test.nobzlmod.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
253421c42119a0ce9b5987673f4ba428907751cd2ebbebd0d45e937479105a07 js/private/test/image/cksum_node.tar
bd0bbecdd3f675cb7b986ff97b0008f88a5832800161257bdec68981fbb5f47e js/private/test/image/cksum_package_store_3p.tar
5fdcbbd175505f08c4c227b443ff4bdd6944561a53e2460375193a23d64d79d2 js/private/test/image/cksum_package_store_1p.tar
84a42327058bddc3e8b43a962fb3b575e06c39cc65c4b5bbdf3953f2b6f78918 js/private/test/image/cksum_node_modules.tar
23 changes: 0 additions & 23 deletions js/private/test/image/custom_owner_app.listing

This file was deleted.

17 changes: 17 additions & 0 deletions js/private/test/image/custom_owner_test_node.bzlmod.listing
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
drwxr-xr-x 0 100 0 0 Jan 1 1970 app
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private/node-patches
-r-xr-xr-x 0 100 0 32555 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/fs.cjs
-r-xr-xr-x 0 100 0 1702 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private/node-patches/register.cjs
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/nodejs
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/nodejs/bin
-r-xr-xr-x 0 100 0 80316256 Jan 1 1970 app/js/private/test/image/bin.runfiles/rules_nodejs~~node~nodejs_linux_amd64/bin/nodejs/bin/node
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
drwxr-xr-x 0 100 0 0 Jan 1 1970 app
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/examples
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/examples/npm_package
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/examples/npm_package/packages
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/examples/npm_package/packages/pkg_d
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/examples/npm_package/packages/pkg_d/node_modules
lrwxrwxr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/examples/npm_package/packages/pkg_d/node_modules/acorn -> ../../../../../node_modules/.aspect_rules_js/[email protected]/node_modules/acorn
lrwxrwxr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/examples/npm_package/packages/pkg_d/node_modules/uuid -> ../../../../../node_modules/.aspect_rules_js/[email protected]/node_modules/uuid
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private/test
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private/test/image
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private/test/image/node_modules
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private/test/image/node_modules/@mycorp
lrwxrwxr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private/test/image/node_modules/@mycorp/pkg-d -> ../../../../../../node_modules/.aspect_rules_js/@[email protected]/node_modules/@mycorp/pkg-d
lrwxrwxr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/js/private/test/image/node_modules/acorn -> ../../../../../node_modules/.aspect_rules_js/[email protected]/node_modules/acorn
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
drwxr-xr-x 0 100 0 0 Jan 1 1970 app
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/node_modules
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/node_modules/.aspect_rules_js
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/node_modules/.aspect_rules_js/@[email protected]
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/node_modules/.aspect_rules_js/@[email protected]/node_modules
drwxr-xr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/node_modules/.aspect_rules_js/@[email protected]/node_modules/@mycorp
lrwxrwxr-x 0 100 0 0 Jan 1 1970 app/js/private/test/image/bin.runfiles/_main/node_modules/.aspect_rules_js/@[email protected]/node_modules/@mycorp/pkg-d -> ../../../../../examples/npm_package/packages/pkg_d
Loading
Loading