Skip to content

Commit bb975a4

Browse files
acozzetteclaude
andcommitted
fix: allow js_run_binary to hoist runfiles in the exec configuration
The `js_run_binary` macro has a `use_execroot_entry_point` option which is enabled by default and which hoists out the runfiles into the execroot. Currently this hoisting effectively rebuilds the runfiles in the target platform config, but they should really be in the exec config since they consist of code that is going to run during the build action. This change fixes that problem by making sure we hoist the runfiles in the exec config. I also updated the launcher script, since it needs to be prepared to look for the entry point in a different directory. This is all guarded by the `hoist_runfiles_to_exec_cfg` option on `js_run_binary`, which effectively defaults to False for now. The default is actually `None` but only for technical reasons. The default is controlled by a new flag `//js:_hoist_runfiles_to_exec_cfg`, but this is intended only for our own CI purposes, allowing us to run most tests against both behaviors. There are a handful of `js_run_binary` targets that require one behavior or the other, so I updated those ones to explicitly specify the flag they need. This addresses #2754, #2121, and #2073, but does not automatically fix them yet since the fix is opt-in for now. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
1 parent f10e4fa commit bb975a4

13 files changed

Lines changed: 275 additions & 25 deletions

File tree

.aspect/workflows/config.yaml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,15 @@ workspaces:
123123
tasks: *e2e_tasks
124124
e2e/repo_mapping:
125125
icon: js
126-
tasks: *e2e_tasks
126+
tasks:
127+
- test:
128+
queue: aspect-medium
129+
- format:
130+
without: true
131+
- buildifier:
132+
without: true
133+
- bazel-7:
134+
without: true
127135
e2e/output_paths:
128136
icon: js
129137
tasks: *e2e_tasks
@@ -154,7 +162,7 @@ tasks:
154162
name: Bazel 7
155163
id: bazel-7
156164
bazel:
157-
flags: ['--test_tag_filters=-skip-on-bazel7']
165+
flags: ['--test_tag_filters=-skip-on-bazel7', '--@aspect_rules_js//js:_hoist_runfiles_to_exec_cfg=true']
158166
env:
159167
USE_BAZEL_VERSION: '7.x'
160168
- test:

MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ bazel_dep(name = "rules_nodejs", version = "6.7.3")
1919

2020
# Changes ensured by rules_js:
2121
# 3.2.2: https://github.com/bazel-contrib/bazel-lib/commit/cac2d7855949d1b222fa26888892fbbe1d31015d
22-
bazel_dep(name = "bazel_lib", version = "3.2.2")
22+
bazel_dep(name = "bazel_lib", version = "3.3.1")
2323

2424
# NB: LOWER BOUND on earliest BCR release of protobuf module, to avoid upgrading the root module by accident
2525
bazel_dep(name = "protobuf", version = "3.19.6")

contrib/nextjs/defs.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ def nextjs_build(name, config, srcs, next_js_binary, data = [], **kwargs):
202202
chdir = native.package_name(),
203203
mnemonic = "NextJs",
204204
progress_message = "Compile Next.js app %{label}",
205+
hoist_runfiles_to_exec_cfg = False,
205206
**kwargs
206207
)
207208

@@ -327,6 +328,7 @@ def nextjs_standalone_build(name, config, srcs, next_js_binary, data = [], **kwa
327328
chdir = native.package_name(),
328329
mnemonic = "NextJs",
329330
progress_message = "Compile Next.js standalone app %{label}",
331+
hoist_runfiles_to_exec_cfg = False,
330332
**kwargs
331333
)
332334

examples/.aspect/rules/external_repository_action_cache/npm_translate_lock_LTE4Nzc1MDcwNjU=

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ npm_package/packages/pkg_d/package.json=1146506398
2020
npm_package/packages/pkg_e/package.json=2046864123
2121
package.json=-2075121688
2222
package_json_module/package.json=-1167380556
23-
pnpm-lock.yaml=-657827575
23+
pnpm-lock.yaml=559561144
2424
pnpm-workspace.yaml=-1653994035
2525
runfiles/package.json=-1545884645
2626
stack_traces/package.json=2011229626

js/BUILD.bazel

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
"Public API"
22

33
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
4+
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
5+
6+
# Underscore prefix signals this is internal — do not rely on it.
7+
# This controls the default value of the hoist_runfiles_to_exec_cfg flag on
8+
# js_run_binary, and is for CI testing only.
9+
bool_flag(
10+
name = "_hoist_runfiles_to_exec_cfg",
11+
build_setting_default = False,
12+
visibility = ["//visibility:public"],
13+
)
14+
15+
config_setting(
16+
name = "_hoist_runfiles_to_exec_cfg_true",
17+
flag_values = {"_hoist_runfiles_to_exec_cfg": "True"},
18+
)
419

520
bzl_library(
621
name = "defs",

js/private/js_binary.sh.tpl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,18 @@ function logf_debug {
9797

9898
function resolve_execroot_bin_path {
9999
local short_path="$1"
100+
local suffix
100101
if [[ "$short_path" == ../* ]]; then
101-
echo "$JS_BINARY__EXECROOT/${BAZEL_BINDIR:-$JS_BINARY__BINDIR}/external/${short_path:3}"
102+
suffix="external/${short_path:3}"
102103
else
103-
echo "$JS_BINARY__EXECROOT/${BAZEL_BINDIR:-$JS_BINARY__BINDIR}/$short_path"
104+
suffix="$short_path"
105+
fi
106+
local exec_path="$JS_BINARY__EXECROOT/$JS_BINARY__BINDIR/$suffix"
107+
local target_path="$JS_BINARY__EXECROOT/${BAZEL_BINDIR:-$JS_BINARY__BINDIR}/$suffix"
108+
if [ -e "$exec_path" ]; then
109+
echo "$exec_path"
110+
else
111+
echo "$target_path"
104112
fi
105113
}
106114

js/private/js_run_binary.bzl

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def js_run_binary(
3131
exit_code_out = None,
3232
silent_on_success = True,
3333
use_execroot_entry_point = True,
34+
hoist_runfiles_to_exec_cfg = None,
3435
copy_srcs_to_bin = True,
3536
include_sources = True,
3637
include_types = False,
@@ -149,6 +150,17 @@ def js_run_binary(
149150
needed by the binary are available in the execroot output tree. This requirement can be turned off with by
150151
setting `allow_execroot_entry_point_with_no_copy_data_to_bin` to True.
151152
153+
hoist_runfiles_to_exec_cfg: Hoist runfiles to the execution platform configuration instead of the target platform
154+
configuration.
155+
156+
This only has an effect when `use_execroot_entry_point` is True. Setting `hoist_runfiles_to_exec_cfg` to True
157+
results in a more correct build by ensuring that code that is executed during a build action is built for the
158+
execution platform, which may not be the same as the target platform.
159+
160+
When set to `None` (the default), the value of the internal `//js:_hoist_runfiles_to_exec_cfg` build flag
161+
is used. That flag is for internal testing only and should not be used. It is temporary and will be removed once
162+
the default is changed to `True`.
163+
152164
copy_srcs_to_bin: When True, all srcs files are copied to the output tree that are not already there.
153165
154166
include_sources: see `js_info_files` documentation
@@ -246,6 +258,7 @@ def js_run_binary(
246258
fail("Use srcs instead of deps in js_run_binary: https://docs.aspect.build/rules/aspect_rules_js/docs/js_run_binary#srcs")
247259

248260
extra_srcs = []
261+
tools = []
249262

250263
# Hoist js provider files to DefaultInfo
251264
make_js_info_files_target = (include_transitive_sources or
@@ -380,14 +393,29 @@ See https://github.com/aspect-build/rules_js/tree/main/docs#using-binaries-publi
380393
# Always propagate the testonly attribute
381394
testonly = kwargs.get("testonly", False),
382395
)
383-
extra_srcs.append(":{}".format(js_runfiles_name))
396+
runfiles_label = ":{}".format(js_runfiles_name)
397+
if hoist_runfiles_to_exec_cfg:
398+
tools.append(runfiles_label)
399+
elif hoist_runfiles_to_exec_cfg == False:
400+
extra_srcs.append(runfiles_label)
401+
else:
402+
# hoist_runfiles_to_exec_cfg is None, so we will defer to the flag.
403+
tools = tools + select({
404+
"@aspect_rules_js//js:_hoist_runfiles_to_exec_cfg_true": [runfiles_label],
405+
"//conditions:default": [],
406+
})
407+
extra_srcs = extra_srcs + select({
408+
"@aspect_rules_js//js:_hoist_runfiles_to_exec_cfg_true": [],
409+
"//conditions:default": [runfiles_label],
410+
})
384411

385412
if allow_execroot_entry_point_with_no_copy_data_to_bin:
386413
fixed_env["JS_BINARY__ALLOW_EXECROOT_ENTRY_POINT_WITH_NO_COPY_DATA_TO_BIN"] = "1"
387414

388415
_run_binary(
389416
name = name,
390417
tool = tool,
418+
tools = tools,
391419
env = fixed_env | env,
392420
srcs = srcs + extra_srcs,
393421
outs = outs + extra_outs,

js/private/test/data/BUILD.bazel

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ js_test(
117117
# TEST: js_test(data = js_run_binary(srcs)) ---------------
118118
js_run_binary(
119119
name = "run-write-srcs",
120+
srcs = ["data.json"],
120121
outs = ["rb0.json"],
121122
args = [
122-
"rb0.json",
123-
"data.json",
123+
"$(rootpath rb0.json)",
124+
"$(rootpath data.json)",
124125
],
125-
chdir = package_name(),
126126
tool = ":write-js_library-srcs",
127127
)
128128

@@ -142,12 +142,12 @@ js_test(
142142
# TEST: js_test(data = js_run_binary(srcs = js_library(srcs))) ----------------
143143
js_run_binary(
144144
name = "run-write-js_library-srcs",
145+
srcs = ["data.json"],
145146
outs = ["rb1.json"],
146147
args = [
147-
"rb1.json",
148-
"data.json",
148+
"$(rootpath rb1.json)",
149+
"$(rootpath data.json)",
149150
],
150-
chdir = package_name(),
151151
tool = ":write-js_library-srcs",
152152
)
153153

@@ -174,12 +174,12 @@ js_binary(
174174

175175
js_run_binary(
176176
name = "run-write-js_library-data",
177+
srcs = ["data.json"],
177178
outs = ["rb2.json"],
178179
args = [
179-
"rb2.json",
180-
"data.json",
180+
"$(rootpath rb2.json)",
181+
"$(rootpath data.json)",
181182
],
182-
chdir = package_name(),
183183
tool = ":write-js_library-data",
184184
)
185185

@@ -199,12 +199,12 @@ js_test(
199199
# TEST: js_test(data = js_run_binary(srcs = genrule())) -----------------------
200200
js_run_binary(
201201
name = "run-write-generated",
202+
srcs = ["data-generated.json"],
202203
outs = ["rb3.json"],
203204
args = [
204-
"rb3.json",
205-
"data-generated.json",
205+
"$(rootpath rb3.json)",
206+
"$(rootpath data-generated.json)",
206207
],
207-
chdir = package_name(),
208208
tool = ":write-genrule",
209209
)
210210

@@ -224,12 +224,12 @@ js_test(
224224
# TEST: js_test(data = js_run_binary(srcs = genrule(srcs = filegroup))) -------
225225
js_run_binary(
226226
name = "run-write-generated-copied",
227+
srcs = ["data-copied.json"],
227228
outs = ["rb4.json"],
228229
args = [
229-
"rb4.json",
230-
"data-copied.json",
230+
"$(rootpath rb4.json)",
231+
"$(rootpath data-copied.json)",
231232
],
232-
chdir = package_name(),
233233
tool = ":write-genrule-filegroup-srcs",
234234
)
235235

js/private/test/image/asserts.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ load("//js:defs.bzl", "js_image_layer")
66
# buildifier: disable=function-docstring
77
def assert_tar_listing(name, actual, expected):
88
# Either of these two file sizes may be observed on a file like /js/private/test/image/bin
9-
sanitize_cmd = "sed -E 's/239[0-9]{2}|24[0-9]{3}/xxxxx/g'"
9+
sanitize_cmd = "sed -E 's/250[0-9]{2}|24[0-9]{3}/xxxxx/g'"
1010
actual_listing = "_{}_listing".format(name)
1111
native.genrule(
1212
name = actual_listing,

0 commit comments

Comments
 (0)