From 132d37314e162ee9a4f414f6f266d76f157ca62e Mon Sep 17 00:00:00 2001 From: Long Ho Date: Mon, 4 May 2026 11:22:47 +0000 Subject: [PATCH] fix: use exec-config runfiles for js_run_binary tools --- js/private/js_binary.sh.tpl | 28 +++++++- js/private/js_run_binary.bzl | 31 ++++++++ .../test/image/custom_owner_test_app.listing | 4 +- .../test/image/default_test_app.listing | 4 +- .../custom_layer_groups_test_app.listing | 4 +- .../image/regex_edge_cases_test_app.listing | 4 +- .../js_run_binary_exec_config/BUILD.bazel | 72 +++++++++++++++++++ js/private/test/snapshots/launcher.sh | 28 +++++++- 8 files changed, 163 insertions(+), 12 deletions(-) create mode 100644 js/private/test/js_run_binary_exec_config/BUILD.bazel diff --git a/js/private/js_binary.sh.tpl b/js/private/js_binary.sh.tpl index ad99a00047..6c2971a9bf 100644 --- a/js/private/js_binary.sh.tpl +++ b/js/private/js_binary.sh.tpl @@ -95,12 +95,36 @@ function logf_debug { fi } +function _normalize_cpu { + case "$1" in + *aarch64* | *arm64*) echo "arm64" ;; + *amd64* | *k8* | *x86_64*) echo "x64" ;; + *) echo "$1" ;; + esac +} + +function _use_exec_config_entry_point { + if [ "$(_normalize_cpu "${BAZEL_TARGET_CPU:-}")" != "$(_normalize_cpu "${JS_BINARY__TARGET_CPU:-}")" ]; then + return 0 + fi + case "${BAZEL_BINDIR:-}" in + bazel-out/*-ST-* | bazel-out/platform-*) return 0 ;; + *) return 1 ;; + esac +} + function resolve_execroot_bin_path { local short_path="$1" + local bindir="${BAZEL_BINDIR:-$JS_BINARY__BINDIR}" + if [ "${JS_BINARY__USE_EXECROOT_ENTRY_POINT:-}" ]; then + if _use_exec_config_entry_point; then + bindir="$JS_BINARY__BINDIR" + fi + fi if [[ "$short_path" == ../* ]]; then - echo "$JS_BINARY__EXECROOT/${BAZEL_BINDIR:-$JS_BINARY__BINDIR}/external/${short_path:3}" + echo "$JS_BINARY__EXECROOT/$bindir/external/${short_path:3}" else - echo "$JS_BINARY__EXECROOT/${BAZEL_BINDIR:-$JS_BINARY__BINDIR}/$short_path" + echo "$JS_BINARY__EXECROOT/$bindir/$short_path" fi } diff --git a/js/private/js_run_binary.bzl b/js/private/js_run_binary.bzl index 844da7f4c1..e222b0dc4e 100644 --- a/js/private/js_run_binary.bzl +++ b/js/private/js_run_binary.bzl @@ -381,6 +381,16 @@ See https://github.com/aspect-build/rules_js/tree/main/docs#using-binaries-publi testonly = kwargs.get("testonly", False), ) extra_srcs.append(":{}".format(js_runfiles_name)) + js_exec_runfiles_name = "{}_exec_runfiles".format(name) + _js_binary_exec_runfiles( + name = js_exec_runfiles_name, + tool = tool, + # Always tag the target manual since we should only build it when the final target is built. + tags = kwargs.get("tags", []) + ["manual"], + # Always propagate the testonly attribute + testonly = kwargs.get("testonly", False), + ) + extra_srcs.append(":{}".format(js_exec_runfiles_name)) if allow_execroot_entry_point_with_no_copy_data_to_bin: fixed_env["JS_BINARY__ALLOW_EXECROOT_ENTRY_POINT_WITH_NO_COPY_DATA_TO_BIN"] = "1" @@ -400,3 +410,24 @@ See https://github.com/aspect-build/rules_js/tree/main/docs#using-binaries-publi use_default_shell_env = use_default_shell_env, **kwargs ) + +def _js_binary_exec_runfiles_impl(ctx): + return DefaultInfo( + files = ctx.attr.tool[DefaultInfo].default_runfiles.files, + ) + +_js_binary_exec_runfiles = rule( + implementation = _js_binary_exec_runfiles_impl, + attrs = { + "tool": attr.label( + doc = """The js_binary tool whose execution-configuration runfiles should be available as action inputs. + + js_run_binary executes its tool in the execution configuration. When use_execroot_entry_point is enabled, + the tool's runfiles must come from the same configuration so native optional npm packages are filtered for + the platform that runs the action, not for the target platform being built. + """, + mandatory = True, + cfg = "exec", + ), + }, +) diff --git a/js/private/test/image/custom_owner_test_app.listing b/js/private/test/image/custom_owner_test_app.listing index 02c40325ff..183ecfd490 100644 --- a/js/private/test/image/custom_owner_test_app.listing +++ b/js/private/test/image/custom_owner_test_app.listing @@ -2,7 +2,7 @@ drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/ drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/ drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/ drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/image/ --r-xr-xr-x 0 100 0 xxxxx Jan 1 1970 ./js/private/test/image/bin +-r-xr-xr-x 0 100 0 25394 Jan 1 1970 ./js/private/test/image/bin drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/ drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/ drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/ @@ -14,7 +14,7 @@ drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/image/bin.runf drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/test/image-fixture-d/ -r-xr-xr-x 0 100 0 128 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/test/image-fixture-d/package.json drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_/ --r-xr-xr-x 0 100 0 xxxxx Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_/bin +-r-xr-xr-x 0 100 0 25394 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_/bin drwxr-xr-x 0 100 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_node_bin/ -r-xr-xr-x 0 100 0 133 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_node_bin/node -r-xr-xr-x 0 100 0 20 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/test/image/main.js diff --git a/js/private/test/image/default_test_app.listing b/js/private/test/image/default_test_app.listing index 55c9c7af9c..3b6cda71c9 100644 --- a/js/private/test/image/default_test_app.listing +++ b/js/private/test/image/default_test_app.listing @@ -2,7 +2,7 @@ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/image/ --r-xr-xr-x 0 0 0 xxxxx Jan 1 1970 ./js/private/test/image/bin +-r-xr-xr-x 0 0 0 25394 Jan 1 1970 ./js/private/test/image/bin drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/ @@ -14,7 +14,7 @@ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/image/bin.runf drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/test/image-fixture-d/ -r-xr-xr-x 0 0 0 128 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/test/image-fixture-d/package.json drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_/ --r-xr-xr-x 0 0 0 xxxxx Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_/bin +-r-xr-xr-x 0 0 0 25394 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_/bin drwxr-xr-x 0 0 0 0 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_node_bin/ -r-xr-xr-x 0 0 0 133 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_node_bin/node -r-xr-xr-x 0 0 0 20 Jan 1 1970 ./js/private/test/image/bin.runfiles/_main/js/private/test/image/main.js diff --git a/js/private/test/image/non_ascii/custom_layer_groups_test_app.listing b/js/private/test/image/non_ascii/custom_layer_groups_test_app.listing index 6418c3c821..4ca042ece4 100644 --- a/js/private/test/image/non_ascii/custom_layer_groups_test_app.listing +++ b/js/private/test/image/non_ascii/custom_layer_groups_test_app.listing @@ -4,7 +4,7 @@ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_ascii/ --r-xr-xr-x 0 0 0 xxxxx Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2 +-r-xr-xr-x 0 0 0 25489 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2 drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/ @@ -14,7 +14,7 @@ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/private/test/image/non_ascii/ -r-xr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/private/test/image/non_ascii/ㅑㅕㅣㅇ.ㄴㅅ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/private/test/image/non_ascii/bin2_/ --r-xr-xr-x 0 0 0 xxxxx Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/private/test/image/non_ascii/bin2_/bin2 +-r-xr-xr-x 0 0 0 25489 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/private/test/image/non_ascii/bin2_/bin2 drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/private/test/image/non_ascii/bin2_node_bin/ -r-xr-xr-x 0 0 0 133 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/private/test/image/non_ascii/bin2_node_bin/node -r-xr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/non_ascii/bin2.runfiles/_main/js/private/test/image/non_ascii/empty empty.ㄴㅅ diff --git a/js/private/test/image/regex_edge_cases_test_app.listing b/js/private/test/image/regex_edge_cases_test_app.listing index ebd1a3497a..c8737461fa 100644 --- a/js/private/test/image/regex_edge_cases_test_app.listing +++ b/js/private/test/image/regex_edge_cases_test_app.listing @@ -3,7 +3,7 @@ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/ --r-xr-xr-x 0 0 0 xxxxx Jan 1 1970 ./app/js/private/test/image/bin +-r-xr-xr-x 0 0 0 25394 Jan 1 1970 ./app/js/private/test/image/bin drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/ @@ -15,7 +15,7 @@ drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin. drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/test/image-fixture-d/ -r-xr-xr-x 0 0 0 128 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/test/image-fixture-d/package.json drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_/ --r-xr-xr-x 0 0 0 xxxxx Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_/bin +-r-xr-xr-x 0 0 0 25394 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_/bin drwxr-xr-x 0 0 0 0 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_node_bin/ -r-xr-xr-x 0 0 0 133 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/test/image/bin_node_bin/node -r-xr-xr-x 0 0 0 20 Jan 1 1970 ./app/js/private/test/image/bin.runfiles/_main/js/private/test/image/main.js diff --git a/js/private/test/js_run_binary_exec_config/BUILD.bazel b/js/private/test/js_run_binary_exec_config/BUILD.bazel new file mode 100644 index 0000000000..a18c25c708 --- /dev/null +++ b/js/private/test/js_run_binary_exec_config/BUILD.bazel @@ -0,0 +1,72 @@ +load("@bazel_lib//lib:testing.bzl", "assert_contains") +load("@bazel_lib//lib:transitions.bzl", "platform_transition_filegroup") +load("@bazel_skylib//rules:write_file.bzl", "write_file") +load("//js:defs.bzl", "js_binary", "js_run_binary") + +config_setting( + name = "linux_arm64_target", + constraint_values = [ + "@platforms//os:linux", + "@platforms//cpu:arm64", + ], +) + +platform( + name = "linux_arm64", + constraint_values = [ + "@platforms//os:linux", + "@platforms//cpu:arm64", + ], +) + +write_file( + name = "write_tool", + out = "tool.mjs", + content = [ + """import { readFileSync } from "node:fs";""", + """import { dirname, join } from "node:path";""", + """import { fileURLToPath } from "node:url";""", + """const here = dirname(fileURLToPath(import.meta.url));""", + """console.log(readFileSync(join(here, "exec_config_marker.txt"), "utf8").trim());""", + ], +) + +write_file( + name = "write_exec_marker", + out = "exec_config_marker.txt", + content = ["exec-config-data"], +) + +write_file( + name = "write_target_marker", + out = "target_config_marker.txt", + content = ["target-config-data"], +) + +js_binary( + name = "tool", + data = select({ + ":linux_arm64_target": [":target_config_marker.txt"], + "//conditions:default": [":exec_config_marker.txt"], + }), + entry_point = "tool.mjs", +) + +js_run_binary( + name = "run_tool", + stdout = "run_tool.out", + tool = ":tool", +) + +platform_transition_filegroup( + name = "run_tool_linux_arm64", + testonly = True, + srcs = [":run_tool"], + target_platform = ":linux_arm64", +) + +assert_contains( + name = "run_tool_uses_exec_config_runfiles", + actual = ":run_tool_linux_arm64", + expected = "exec-config-data", +) diff --git a/js/private/test/snapshots/launcher.sh b/js/private/test/snapshots/launcher.sh index 7fdf331a76..22ef88a06e 100644 --- a/js/private/test/snapshots/launcher.sh +++ b/js/private/test/snapshots/launcher.sh @@ -106,12 +106,36 @@ function logf_debug { fi } +function _normalize_cpu { + case "$1" in + *aarch64* | *arm64*) echo "arm64" ;; + *amd64* | *k8* | *x86_64*) echo "x64" ;; + *) echo "$1" ;; + esac +} + +function _use_exec_config_entry_point { + if [ "$(_normalize_cpu "${BAZEL_TARGET_CPU:-}")" != "$(_normalize_cpu "${JS_BINARY__TARGET_CPU:-}")" ]; then + return 0 + fi + case "${BAZEL_BINDIR:-}" in + bazel-out/*-ST-* | bazel-out/platform-*) return 0 ;; + *) return 1 ;; + esac +} + function resolve_execroot_bin_path { local short_path="$1" + local bindir="${BAZEL_BINDIR:-$JS_BINARY__BINDIR}" + if [ "${JS_BINARY__USE_EXECROOT_ENTRY_POINT:-}" ]; then + if _use_exec_config_entry_point; then + bindir="$JS_BINARY__BINDIR" + fi + fi if [[ "$short_path" == ../* ]]; then - echo "$JS_BINARY__EXECROOT/${BAZEL_BINDIR:-$JS_BINARY__BINDIR}/external/${short_path:3}" + echo "$JS_BINARY__EXECROOT/$bindir/external/${short_path:3}" else - echo "$JS_BINARY__EXECROOT/${BAZEL_BINDIR:-$JS_BINARY__BINDIR}/$short_path" + echo "$JS_BINARY__EXECROOT/$bindir/$short_path" fi }