Skip to content

Commit 1641136

Browse files
authored
fix(builtin): remove unnecessary loader script (#3495)
1 parent 12a768b commit 1641136

8 files changed

Lines changed: 51 additions & 87 deletions

File tree

examples/angular/WORKSPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ http_archive(
3838
patches = [
3939
# Updates @bazel/work dep to 4.0.0 inside rules_sass so it is compatible
4040
"//:io_bazel_rules_sass.patch",
41+
# Add missing extension to entrypoint (https://github.com/bazelbuild/rules_sass/pull/142)
42+
"//:io_bazel_rules_sass_entrypoint.patch",
4143
],
4244
sha256 = "5313032124ff191eed68efcfbdc6ee9b5198093b2b80a8e640ea34feabbffc69",
4345
strip_prefix = "rules_sass-1.34.0",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
index cfa8b8b..394f6c2 100644
2+
--- a/sass/BUILD
3+
+++ b/sass/BUILD
4+
@@ -11,7 +11,7 @@ exports_files([
5+
# Executable for the sass_binary rule
6+
nodejs_binary(
7+
name = "sass",
8+
- entry_point = "sass_wrapper",
9+
+ entry_point = "sass_wrapper.js",
10+
data = [
11+
":sass_wrapper.js",
12+
"@build_bazel_rules_sass_deps//sass",

internal/node/BUILD.bazel

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package(default_visibility = ["//visibility:public"])
2020
exports_files([
2121
"node_patches.cjs",
2222
"require_patch.cjs",
23-
"loader.cjs",
2423
])
2524

2625
bzl_library(
@@ -40,7 +39,6 @@ exports_files([
4039
"node.bzl", # Exported to be consumed for generating stardoc.
4140
"node_repositories.bzl", # Exported to be consumed for generating stardoc.
4241
"launcher.sh",
43-
"loader.cjs",
4442
])
4543

4644
filegroup(

internal/node/launcher.sh

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,11 @@ for ARG in ${ALL_ARGS[@]+"${ALL_ARGS[@]}"}; do
199199
--bazel_capture_exit_code=*) EXIT_CODE_CAPTURE="${PWD}/${ARG#--bazel_capture_exit_code=}" ;;
200200
# Outputs nothing on success
201201
--bazel_silent_on_success=*) SILENT_ON_SUCCESS=true ;;
202-
# Disable the node_loader.cjs monkey patches for require()
202+
# Disable the require_patch.cjs monkey patches for require()
203203
# Note that this means you need an explicit runfiles helper library
204204
# This flag is now a no-op since the default is also false
205205
--nobazel_patch_module_resolver) PATCH_REQUIRE=false ;;
206-
# Enable the node_loader.cjs monkey patches for require()
206+
# Enable the require_patch.cjs monkey patches for require()
207207
--bazel_patch_module_resolver) PATCH_REQUIRE=true ;;
208208
# Disable the --require node-patches (undocumented and unused; only here as an escape value)
209209
--nobazel_node_patches) NODE_PATCHES=false ;;
@@ -331,26 +331,24 @@ if [ "$PATCH_REQUIRE" = true ]; then
331331
* ) require_patch_script="${PWD}/${require_patch_script}" ;;
332332
esac
333333
LAUNCHER_NODE_OPTIONS+=( "--require" "$require_patch_script" )
334-
# Change the entry point to be the loader.cjs script so we run code before node
335-
MAIN=$(rlocation "TEMPLATED_loader_script")
336-
else
337-
# Entry point is the user-supplied script
338-
MAIN="${PWD}/"TEMPLATED_entry_point_execroot_path
339-
# TODO: after we link-all-bins we should not need this extra lookup
340-
if [[ ! -e "$MAIN" ]]; then
341-
if [ "$FROM_EXECROOT" = true ]; then
342-
MAIN="$EXECROOT/"TEMPLATED_entry_point_execroot_path
343-
else
344-
MAIN=TEMPLATED_entry_point_manifest_path
345-
fi
346-
fi
347-
# Always set up source-map-support using our vendored copy, just like the require_patch_script
348-
register_source_map_support=$(rlocation build_bazel_rules_nodejs/third_party/github.com/source-map-support/register.js)
349-
LAUNCHER_NODE_OPTIONS+=( "--require" "${register_source_map_support}" )
350-
if [[ -n "TEMPLATED_entry_point_main" ]]; then
351-
MAIN="${MAIN}/"TEMPLATED_entry_point_main
334+
fi
335+
336+
# Entry point is the user-supplied script
337+
MAIN="${PWD}/"TEMPLATED_entry_point_execroot_path
338+
# TODO: after we link-all-bins we should not need this extra lookup
339+
if [[ ! -e "$MAIN" ]]; then
340+
if [ "$FROM_EXECROOT" = true ]; then
341+
MAIN="$EXECROOT/"TEMPLATED_entry_point_execroot_path
342+
else
343+
MAIN=TEMPLATED_entry_point_manifest_path
352344
fi
353345
fi
346+
# Always set up source-map-support using our vendored copy, just like the require_patch_script
347+
register_source_map_support=$(rlocation build_bazel_rules_nodejs/third_party/github.com/source-map-support/register.js)
348+
LAUNCHER_NODE_OPTIONS+=( "--require" "${register_source_map_support}" )
349+
if [[ -n "TEMPLATED_entry_point_main" ]]; then
350+
MAIN="${MAIN}/"TEMPLATED_entry_point_main
351+
fi
354352

355353
if [ "${SILENT_ON_SUCCESS:-}" = true ]; then
356354
if [[ -z "${STDOUT_CAPTURE}" ]]; then
@@ -484,3 +482,4 @@ if [[ -n "${EXIT_CODE_CAPTURE}" ]]; then
484482
else
485483
exit ${RESULT}
486484
fi
485+

internal/node/loader.cjs

Lines changed: 0 additions & 39 deletions
This file was deleted.

internal/node/node.bzl

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,6 @@ def _get_entry_point_file(ctx):
117117
return ctx.attr.entry_point[DirectoryFilePathInfo].directory
118118
fail("entry_point must either be a file, or provide DirectoryFilePathInfo")
119119

120-
def _write_loader_script(ctx):
121-
substitutions = {}
122-
substitutions["TEMPLATED_entry_point_path"] = _ts_to_js(_to_manifest_path(ctx, _get_entry_point_file(ctx)))
123-
if DirectoryFilePathInfo in ctx.attr.entry_point:
124-
substitutions["TEMPLATED_entry_point_main"] = ctx.attr.entry_point[DirectoryFilePathInfo].path
125-
else:
126-
substitutions["TEMPLATED_entry_point_main"] = ""
127-
128-
ctx.actions.expand_template(
129-
template = ctx.file._loader_template,
130-
output = ctx.outputs.loader_script,
131-
substitutions = substitutions,
132-
is_executable = True,
133-
)
134-
135120
# Avoid using non-normalized paths (workspace/../other_workspace/path)
136121
def _to_manifest_path(ctx, file):
137122
if file.short_path.startswith("../"):
@@ -208,8 +193,6 @@ def _nodejs_binary_impl(ctx, data = [], runfiles = [], expanded_args = []):
208193
node_modules_root = "build_bazel_rules_nodejs/node_modules"
209194
_write_require_patch_script(ctx, data, node_modules_root)
210195

211-
_write_loader_script(ctx)
212-
213196
# Provide the target name as an environment variable avaiable to all actions for the
214197
# runfiles helpers to use.
215198
env_vars = "export BAZEL_TARGET=%s\n" % ctx.label
@@ -276,7 +259,6 @@ fi
276259
runfiles = runfiles[:]
277260
runfiles.extend(node_tool_files)
278261
runfiles.extend(ctx.files._bash_runfile_helper)
279-
runfiles.append(ctx.outputs.loader_script)
280262
runfiles.append(ctx.outputs.require_patch_script)
281263

282264
# First replace any instances of "$(rlocation " with "$$(rlocation " to preserve
@@ -331,7 +313,6 @@ if (process.cwd() !== __dirname) {
331313
"TEMPLATED_expected_exit_code": str(expected_exit_code),
332314
"TEMPLATED_lcov_merger_script": _to_manifest_path(ctx, ctx.file._lcov_merger_script),
333315
"TEMPLATED_link_modules_script": _to_manifest_path(ctx, ctx.file._link_modules_script),
334-
"TEMPLATED_loader_script": _to_manifest_path(ctx, ctx.outputs.loader_script),
335316
"TEMPLATED_modules_manifest": _to_manifest_path(ctx, node_modules_manifest),
336317
"TEMPLATED_node_patches_script": _to_manifest_path(ctx, ctx.file._node_patches_script),
337318
"TEMPLATED_require_patch_script": _to_manifest_path(ctx, ctx.outputs.require_patch_script),
@@ -378,7 +359,6 @@ if (process.cwd() !== __dirname) {
378359
runfiles = ctx.runfiles(
379360
transitive_files = depset(runfiles),
380361
files = node_tool_files + [
381-
ctx.outputs.loader_script,
382362
ctx.outputs.require_patch_script,
383363
] + ctx.files._source_map_support_files +
384364

@@ -608,10 +588,6 @@ Predefined genrule variables are not supported in this context.
608588
default = Label("//internal/linker:index.js"),
609589
allow_single_file = True,
610590
),
611-
"_loader_template": attr.label(
612-
default = Label("//internal/node:loader.cjs"),
613-
allow_single_file = True,
614-
),
615591
"toolchain": attr.label(),
616592
"_node_args": attr.label(default = "@rules_nodejs//nodejs:default_args"),
617593
"_node_patches_script": attr.label(
@@ -642,7 +618,6 @@ Predefined genrule variables are not supported in this context.
642618

643619
_NODEJS_EXECUTABLE_OUTPUTS = {
644620
"launcher_sh": "%{name}.sh",
645-
"loader_script": "%{name}_loader.cjs",
646621
"require_patch_script": "%{name}_require_patch.cjs",
647622
}
648623

internal/node/test/esm/BUILD.bazel

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary")
1+
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary", "npm_package_bin")
22

33
nodejs_binary(
44
name = "has_deps",
@@ -12,3 +12,16 @@ nodejs_binary(
1212
name = "no_deps",
1313
entry_point = ":no-deps.mjs",
1414
)
15+
16+
nodejs_binary(
17+
name = "no_deps_linker_disabled",
18+
entry_point = ":no-deps-create-file.mjs",
19+
templated_args = ["--nobazel_run_linker"],
20+
)
21+
22+
npm_package_bin(
23+
name = "run_with_linker_disabled",
24+
outs = ["out.txt"],
25+
args = ["$@"],
26+
tool = ":no_deps_linker_disabled",
27+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {writeFileSync} from "fs";
2+
3+
const outputFile = process.argv[2];
4+
writeFileSync(outputFile, "foobar\n");

0 commit comments

Comments
 (0)