Skip to content

Commit 82a7e1d

Browse files
committed
fix: make js_run_binary 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, which defaults to False for now. Ideally we should make it True by default, but that will be a breaking change. This addresses #2754, #2121, and #2073, but does not automatically fix them yet since the fix is opt-in for now.
1 parent f10e4fa commit 82a7e1d

7 files changed

Lines changed: 48 additions & 22 deletions

File tree

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/private/js_binary.sh.tpl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,16 @@ function logf_debug {
9696
}
9797

9898
function resolve_execroot_bin_path {
99+
if [ "${JS_BINARY__HOIST_RUNFILES_TO_EXEC_CFG:-}" ]; then
100+
local bindir="$JS_BINARY__EXECROOT/$JS_BINARY__BINDIR"
101+
else
102+
local bindir="$JS_BINARY__EXECROOT/$BAZEL_BINDIR"
103+
fi
99104
local short_path="$1"
100105
if [[ "$short_path" == ../* ]]; then
101-
echo "$JS_BINARY__EXECROOT/${BAZEL_BINDIR:-$JS_BINARY__BINDIR}/external/${short_path:3}"
106+
echo "$bindir/external/${short_path:3}"
102107
else
103-
echo "$JS_BINARY__EXECROOT/${BAZEL_BINDIR:-$JS_BINARY__BINDIR}/$short_path"
108+
echo "$bindir/$short_path"
104109
fi
105110
}
106111

js/private/js_run_binary.bzl

Lines changed: 15 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 = True,
3435
copy_srcs_to_bin = True,
3536
include_sources = True,
3637
include_types = False,
@@ -149,6 +150,13 @@ 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+
152160
copy_srcs_to_bin: When True, all srcs files are copied to the output tree that are not already there.
153161
154162
include_sources: see `js_info_files` documentation
@@ -246,6 +254,7 @@ def js_run_binary(
246254
fail("Use srcs instead of deps in js_run_binary: https://docs.aspect.build/rules/aspect_rules_js/docs/js_run_binary#srcs")
247255

248256
extra_srcs = []
257+
tools = []
249258

250259
# Hoist js provider files to DefaultInfo
251260
make_js_info_files_target = (include_transitive_sources or
@@ -380,14 +389,19 @@ See https://github.com/aspect-build/rules_js/tree/main/docs#using-binaries-publi
380389
# Always propagate the testonly attribute
381390
testonly = kwargs.get("testonly", False),
382391
)
383-
extra_srcs.append(":{}".format(js_runfiles_name))
392+
if hoist_runfiles_to_exec_cfg:
393+
fixed_env["JS_BINARY__HOIST_RUNFILES_TO_EXEC_CFG"] = "1"
394+
tools.append(":{}".format(js_runfiles_name))
395+
else:
396+
extra_srcs.append(":{}".format(js_runfiles_name))
384397

385398
if allow_execroot_entry_point_with_no_copy_data_to_bin:
386399
fixed_env["JS_BINARY__ALLOW_EXECROOT_ENTRY_POINT_WITH_NO_COPY_DATA_TO_BIN"] = "1"
387400

388401
_run_binary(
389402
name = name,
390403
tool = tool,
404+
tools = tools,
391405
env = fixed_env | env,
392406
srcs = srcs + extra_srcs,
393407
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/snapshots/launcher.sh

Lines changed: 7 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)