Skip to content

Commit 5b03c08

Browse files
committed
fix: use exec-config runfiles for js_run_binary tools
1 parent 80d7460 commit 5b03c08

4 files changed

Lines changed: 155 additions & 4 deletions

File tree

js/private/js_binary.sh.tpl

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,36 @@ function logf_debug {
9595
fi
9696
}
9797

98+
function _normalize_cpu {
99+
case "$1" in
100+
*aarch64* | *arm64*) echo "arm64" ;;
101+
*amd64* | *k8* | *x86_64*) echo "x64" ;;
102+
*) echo "$1" ;;
103+
esac
104+
}
105+
106+
function _use_exec_config_entry_point {
107+
if [ "$(_normalize_cpu "${BAZEL_TARGET_CPU:-}")" != "$(_normalize_cpu "${JS_BINARY__TARGET_CPU:-}")" ]; then
108+
return 0
109+
fi
110+
case "${BAZEL_BINDIR:-}" in
111+
bazel-out/*-ST-* | bazel-out/platform-*) return 0 ;;
112+
*) return 1 ;;
113+
esac
114+
}
115+
98116
function resolve_execroot_bin_path {
99117
local short_path="$1"
118+
local bindir="${BAZEL_BINDIR:-$JS_BINARY__BINDIR}"
119+
if [ "${JS_BINARY__USE_EXECROOT_ENTRY_POINT:-}" ]; then
120+
if _use_exec_config_entry_point; then
121+
bindir="$JS_BINARY__BINDIR"
122+
fi
123+
fi
100124
if [[ "$short_path" == ../* ]]; then
101-
echo "$JS_BINARY__EXECROOT/${BAZEL_BINDIR:-$JS_BINARY__BINDIR}/external/${short_path:3}"
125+
echo "$JS_BINARY__EXECROOT/$bindir/external/${short_path:3}"
102126
else
103-
echo "$JS_BINARY__EXECROOT/${BAZEL_BINDIR:-$JS_BINARY__BINDIR}/$short_path"
127+
echo "$JS_BINARY__EXECROOT/$bindir/$short_path"
104128
fi
105129
}
106130

js/private/js_run_binary.bzl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,16 @@ See https://github.com/aspect-build/rules_js/tree/main/docs#using-binaries-publi
381381
testonly = kwargs.get("testonly", False),
382382
)
383383
extra_srcs.append(":{}".format(js_runfiles_name))
384+
js_exec_runfiles_name = "{}_exec_runfiles".format(name)
385+
_js_binary_exec_runfiles(
386+
name = js_exec_runfiles_name,
387+
tool = tool,
388+
# Always tag the target manual since we should only build it when the final target is built.
389+
tags = kwargs.get("tags", []) + ["manual"],
390+
# Always propagate the testonly attribute
391+
testonly = kwargs.get("testonly", False),
392+
)
393+
extra_srcs.append(":{}".format(js_exec_runfiles_name))
384394

385395
if allow_execroot_entry_point_with_no_copy_data_to_bin:
386396
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
400410
use_default_shell_env = use_default_shell_env,
401411
**kwargs
402412
)
413+
414+
def _js_binary_exec_runfiles_impl(ctx):
415+
return DefaultInfo(
416+
files = ctx.attr.tool[DefaultInfo].default_runfiles.files,
417+
)
418+
419+
_js_binary_exec_runfiles = rule(
420+
implementation = _js_binary_exec_runfiles_impl,
421+
attrs = {
422+
"tool": attr.label(
423+
doc = """The js_binary tool whose execution-configuration runfiles should be available as action inputs.
424+
425+
js_run_binary executes its tool in the execution configuration. When use_execroot_entry_point is enabled,
426+
the tool's runfiles must come from the same configuration so native optional npm packages are filtered for
427+
the platform that runs the action, not for the target platform being built.
428+
""",
429+
mandatory = True,
430+
cfg = "exec",
431+
),
432+
},
433+
)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
load("@bazel_lib//lib:testing.bzl", "assert_contains")
2+
load("@bazel_lib//lib:transitions.bzl", "platform_transition_filegroup")
3+
load("@bazel_skylib//rules:write_file.bzl", "write_file")
4+
load("//js:defs.bzl", "js_binary", "js_run_binary")
5+
6+
config_setting(
7+
name = "linux_arm64_target",
8+
constraint_values = [
9+
"@platforms//os:linux",
10+
"@platforms//cpu:arm64",
11+
],
12+
)
13+
14+
platform(
15+
name = "linux_arm64",
16+
constraint_values = [
17+
"@platforms//os:linux",
18+
"@platforms//cpu:arm64",
19+
],
20+
)
21+
22+
write_file(
23+
name = "write_tool",
24+
out = "tool.mjs",
25+
content = [
26+
"""import { readFileSync } from "node:fs";""",
27+
"""import { dirname, join } from "node:path";""",
28+
"""import { fileURLToPath } from "node:url";""",
29+
"""const here = dirname(fileURLToPath(import.meta.url));""",
30+
"""console.log(readFileSync(join(here, "exec_config_marker.txt"), "utf8").trim());""",
31+
],
32+
)
33+
34+
write_file(
35+
name = "write_exec_marker",
36+
out = "exec_config_marker.txt",
37+
content = ["exec-config-data"],
38+
)
39+
40+
write_file(
41+
name = "write_target_marker",
42+
out = "target_config_marker.txt",
43+
content = ["target-config-data"],
44+
)
45+
46+
js_binary(
47+
name = "tool",
48+
data = select({
49+
":linux_arm64_target": [":target_config_marker.txt"],
50+
"//conditions:default": [":exec_config_marker.txt"],
51+
}),
52+
entry_point = "tool.mjs",
53+
)
54+
55+
js_run_binary(
56+
name = "run_tool",
57+
stdout = "run_tool.out",
58+
tool = ":tool",
59+
)
60+
61+
platform_transition_filegroup(
62+
name = "run_tool_linux_arm64",
63+
testonly = True,
64+
srcs = [":run_tool"],
65+
target_platform = ":linux_arm64",
66+
)
67+
68+
assert_contains(
69+
name = "run_tool_uses_exec_config_runfiles",
70+
actual = ":run_tool_linux_arm64",
71+
expected = "exec-config-data",
72+
)

js/private/test/snapshots/launcher.sh

Lines changed: 26 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)