Skip to content

Commit 65d8a36

Browse files
committed
feat(builtin): expose the new linker to node programs
If the magic --bazel_node_modules_manifest flag is passed to a nodejs_binary, we peel it off and run the linker first This lets programs avoid having custom resolution logic.
1 parent 827ec95 commit 65d8a36

8 files changed

Lines changed: 42 additions & 0 deletions

File tree

e2e/symlinked_node_modules_npm/WORKSPACE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ local_repository(
1111
path = "../..",
1212
)
1313

14+
# We don't really need to run TypeScript rules, but we'll encounter some during
15+
# the loading phase because some built-in code uses TypeScript
16+
local_repository(
17+
name = "npm_bazel_typescript",
18+
path = "../../tools/mock_npm_bazel_typescript",
19+
)
20+
1421
# rules_nodejs_dev_dependencies() required since we're using local_repository for
1522
# build_bazel_rules_nodejs above.
1623
load("@build_bazel_rules_nodejs//:package.bzl", "rules_nodejs_dev_dependencies")

e2e/symlinked_node_modules_yarn/WORKSPACE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ local_repository(
1111
path = "../..",
1212
)
1313

14+
# We don't really need to run TypeScript rules, but we'll encounter some during
15+
# the loading phase because some built-in code uses TypeScript
16+
local_repository(
17+
name = "npm_bazel_typescript",
18+
path = "../../tools/mock_npm_bazel_typescript",
19+
)
20+
1421
# rules_nodejs_dev_dependencies() required since we're using local_repository for
1522
# build_bazel_rules_nodejs above.
1623
load("@build_bazel_rules_nodejs//:package.bzl", "rules_nodejs_dev_dependencies")

internal/linker/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# BEGIN-INTERNAL
2+
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
23
load("@npm_bazel_typescript//:index.from_src.bzl", "checked_in_ts_library")
34

45
# We can't bootstrap the ts_library rule using the linker itself,
@@ -12,6 +13,12 @@ checked_in_ts_library(
1213
deps = ["@npm//@types/node"],
1314
)
1415

16+
bzl_library(
17+
name = "bzl",
18+
srcs = glob(["*.bzl"]),
19+
visibility = ["//visibility:public"],
20+
)
21+
1522
# END-INTERNAL
1623
exports_files(["index.js"])
1724

internal/node/node.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ def _nodejs_binary_impl(ctx):
169169
# attempts to do bazel run
170170
fail("The node toolchain was not properly configured so %s cannot be executed. Make sure that target_tool_path or target_tool is set." % ctx.attr.name)
171171

172+
node_tool_files.append(ctx.file._link_modules_script)
173+
172174
if not ctx.outputs.templated_args_file:
173175
templated_args = ctx.attr.templated_args
174176
else:
@@ -201,6 +203,7 @@ def _nodejs_binary_impl(ctx):
201203
]),
202204
"TEMPLATED_env_vars": env_vars,
203205
"TEMPLATED_expected_exit_code": str(expected_exit_code),
206+
"TEMPLATED_link_modules_script": _to_manifest_path(ctx, ctx.file._link_modules_script),
204207
"TEMPLATED_node": node_tool_info.target_tool_path,
205208
"TEMPLATED_repository_args": _to_manifest_path(ctx, ctx.file._repository_args),
206209
"TEMPLATED_script_path": script_path,
@@ -425,6 +428,10 @@ The set of default environment variables is:
425428
default = Label("//internal/node:node_launcher.sh"),
426429
allow_single_file = True,
427430
),
431+
"_link_modules_script": attr.label(
432+
default = Label("//internal/linker:index.js"),
433+
allow_single_file = True,
434+
),
428435
"_loader_template": attr.label(
429436
default = Label("//internal/node:node_loader.js"),
430437
allow_single_file = True,

internal/node/node_launcher.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ TEMPLATED_env_vars
117117
readonly node=$(rlocation "TEMPLATED_node")
118118
readonly repository_args=$(rlocation "TEMPLATED_repository_args")
119119
readonly script=$(rlocation "TEMPLATED_script_path")
120+
readonly link_modules_script=$(rlocation "TEMPLATED_link_modules_script")
120121

121122
source $repository_args
122123

@@ -125,11 +126,17 @@ NODE_OPTIONS=()
125126
ALL_ARGS=(TEMPLATED_args $NODE_REPOSITORY_ARGS "$@")
126127
for ARG in "${ALL_ARGS[@]}"; do
127128
case "$ARG" in
129+
--bazel_node_modules_manifest=*) MODULES_MANIFEST="${ARG#--bazel_node_modules_manifest=}" ;;
128130
--node_options=*) NODE_OPTIONS+=( "${ARG#--node_options=}" ) ;;
129131
*) ARGS+=( "$ARG" )
130132
esac
131133
done
132134

135+
# Link the first-party modules into node_modules directory before running the actual program
136+
if [[ -n "$MODULES_MANIFEST" ]]; then
137+
"${node}" "${link_modules_script}" "${MODULES_MANIFEST}"
138+
fi
139+
133140
# The EXPECTED_EXIT_CODE lets us write bazel tests which assert that
134141
# a binary fails to run. Otherwise any failure would make such a test
135142
# fail before we could assert that we expected that failure.

tools/mock_npm_bazel_typescript/BUILD.bazel

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
workspace(name = "npm_bazel_typescript")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"Minimal implementation to make loading phase succeed"
2+
3+
def noop(**kwargs):
4+
pass
5+
6+
checked_in_ts_library = noop

0 commit comments

Comments
 (0)