Skip to content

Commit 3321ed5

Browse files
alexeaglegregmagolan
authored andcommitted
feat(builtin): wire linker/node-patches to npm-generated bin and test
- nodejs_binary and nodejs_test will always produce a modules manifest needed by the linker - if the program isn't run with a user-specific linker manifest, we use the static one as a default - always pass the flag to opt-out of custom module resolver for generated rules Fixes #1382
1 parent 3c41162 commit 3321ed5

7 files changed

Lines changed: 31 additions & 14 deletions

File tree

internal/node/node.bzl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ load("//internal/common:expand_into_runfiles.bzl", "expand_location_into_runfile
2525
load("//internal/common:module_mappings.bzl", "module_mappings_runtime_aspect")
2626
load("//internal/common:path_utils.bzl", "strip_external")
2727
load("//internal/common:windows_utils.bzl", "create_windows_native_launcher_script", "is_windows")
28+
load("//internal/linker:link_node_modules.bzl", "write_node_modules_manifest")
2829
load("//internal/node:node_repositories.bzl", "BUILT_IN_NODE_PLATFORMS")
2930

3031
def _trim_package_node_modules(package_name):
@@ -132,18 +133,15 @@ def _to_manifest_path(ctx, file):
132133
def _to_execroot_path(ctx, file):
133134
parts = file.path.split("/")
134135

135-
#print("_to_execroot", file.path, file.is_source)
136136
if parts[0] == "external":
137137
if parts[2] == "node_modules":
138138
# external/npm/node_modules -> node_modules/foo
139139
# the linker will make sure we can resolve node_modules from npm
140140
return "/".join(parts[2:])
141-
if file.is_source:
142-
return file.path
143-
144-
return ("<ERROR> _to_execroot_path not yet implemented for " + file.path)
141+
return file.path
145142

146143
def _nodejs_binary_impl(ctx):
144+
node_modules_manifest = write_node_modules_manifest(ctx)
147145
node_modules = depset(ctx.files.node_modules)
148146

149147
# Also include files from npm fine grained deps as inputs.
@@ -155,14 +153,15 @@ def _nodejs_binary_impl(ctx):
155153
# Using a depset will allow us to avoid flattening files and sources
156154
# inside this loop. This should reduce the performances hits,
157155
# since we don't need to call .to_list()
158-
sources = depset()
156+
sources_sets = []
159157

160158
for d in ctx.attr.data:
161159
# TODO: switch to JSModuleInfo when it is available
162160
if JSNamedModuleInfo in d:
163-
sources = depset(transitive = [sources, d[JSNamedModuleInfo].sources])
161+
sources_sets.append(d[JSNamedModuleInfo].sources)
164162
if hasattr(d, "files"):
165-
sources = depset(transitive = [sources, d.files])
163+
sources_sets.append(d.files)
164+
sources = depset(transitive = sources_sets)
166165

167166
_write_loader_script(ctx)
168167

@@ -198,6 +197,7 @@ def _nodejs_binary_impl(ctx):
198197

199198
node_tool_files.append(ctx.file._link_modules_script)
200199
node_tool_files.append(ctx.file._bazel_require_script)
200+
node_tool_files.append(node_modules_manifest)
201201

202202
if not ctx.outputs.templated_args_file:
203203
templated_args = ctx.attr.templated_args
@@ -236,6 +236,7 @@ def _nodejs_binary_impl(ctx):
236236
"TEMPLATED_expected_exit_code": str(expected_exit_code),
237237
"TEMPLATED_link_modules_script": _to_manifest_path(ctx, ctx.file._link_modules_script),
238238
"TEMPLATED_loader_path": script_path,
239+
"TEMPLATED_modules_manifest": _to_manifest_path(ctx, node_modules_manifest),
239240
"TEMPLATED_repository_args": _to_manifest_path(ctx, ctx.file._repository_args),
240241
"TEMPLATED_script_path": _to_execroot_path(ctx, ctx.file.entry_point),
241242
"TEMPLATED_vendored_node": "" if is_builtin else strip_external(ctx.file._node.path),

internal/node/node_launcher.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ for ARG in "${ALL_ARGS[@]}"; do
186186
--nobazel_patch_module_resolver)
187187
MAIN="TEMPLATED_script_path"
188188
NODE_OPTIONS+=( "--require" "$bazel_require_script" )
189+
190+
# In this case we should always run the linker
191+
# For programs which are called with bazel run or bazel test, there will be no additional runtime
192+
# dependencies to link, so we use the default modules_manifest which has only the static dependencies
193+
# of the binary itself
194+
MODULES_MANIFEST=${MODULES_MANIFEST:-$(rlocation "TEMPLATED_modules_manifest")}
189195
;;
190196
--node_options=*) NODE_OPTIONS+=( "${ARG#--node_options=}" ) ;;
191197
*) ARGS+=( "$ARG" )

internal/npm_install/generate_build_file.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,8 @@ def ${name.replace(/-/g, '_')}(**kwargs):
982982
nodejs_binary(
983983
entry_point = "@${WORKSPACE}//:node_modules/${pkg._dir}/${path}",
984984
install_source_map_support = False,
985-
data = [${data.map(p => `"${p}"`).join(', ')}] + kwargs.pop("data", []),${additionalAttributes(pkg, name)}
985+
data = [${data.map(p => `"${p}"`).join(', ')}] + kwargs.pop("data", []),
986+
templated_args = ["--nobazel_patch_module_resolver"] + kwargs.pop("templated_args", []),${additionalAttributes(pkg, name)}
986987
**kwargs
987988
)
988989
@@ -991,7 +992,8 @@ def ${name.replace(/-/g, '_')}_test(**kwargs):
991992
nodejs_test(
992993
entry_point = "@${WORKSPACE}//:node_modules/${pkg._dir}/${path}",
993994
install_source_map_support = False,
994-
data = [${data.map(p => `"${p}"`).join(', ')}] + kwargs.pop("data", []),${additionalAttributes(pkg, name)}
995+
data = [${data.map(p => `"${p}"`).join(', ')}] + kwargs.pop("data", []),
996+
templated_args = ["--nobazel_patch_module_resolver"] + kwargs.pop("templated_args", []),${additionalAttributes(pkg, name)}
995997
**kwargs
996998
)
997999
`;

internal/npm_install/generate_build_file.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,8 @@ def ${name.replace(/-/g, '_')}(**kwargs):
10841084
nodejs_binary(
10851085
entry_point = "@${WORKSPACE}//:node_modules/${pkg._dir}/${path}",
10861086
install_source_map_support = False,
1087-
data = [${data.map(p => `"${p}"`).join(', ')}] + kwargs.pop("data", []),${
1087+
data = [${data.map(p => `"${p}"`).join(', ')}] + kwargs.pop("data", []),
1088+
templated_args = ["--nobazel_patch_module_resolver"] + kwargs.pop("templated_args", []),${
10881089
additionalAttributes(pkg, name)}
10891090
**kwargs
10901091
)
@@ -1094,7 +1095,8 @@ def ${name.replace(/-/g, '_')}_test(**kwargs):
10941095
nodejs_test(
10951096
entry_point = "@${WORKSPACE}//:node_modules/${pkg._dir}/${path}",
10961097
install_source_map_support = False,
1097-
data = [${data.map(p => `"${p}"`).join(', ')}] + kwargs.pop("data", []),${
1098+
data = [${data.map(p => `"${p}"`).join(', ')}] + kwargs.pop("data", []),
1099+
templated_args = ["--nobazel_patch_module_resolver"] + kwargs.pop("templated_args", []),${
10981100
additionalAttributes(pkg, name)}
10991101
**kwargs
11001102
)

internal/npm_install/test/golden/@gregmagolan/test-a/index.bzl.golden

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ def test(**kwargs):
88
entry_point = "@fine_grained_goldens//:node_modules/@gregmagolan/test-a/@bin/test.js",
99
install_source_map_support = False,
1010
data = ["@fine_grained_goldens//@gregmagolan/test-a:test-a"] + kwargs.pop("data", []),
11+
templated_args = ["--nobazel_patch_module_resolver"] + kwargs.pop("templated_args", []),
1112
**kwargs
1213
)
1314
def test_test(**kwargs):
1415
nodejs_test(
1516
entry_point = "@fine_grained_goldens//:node_modules/@gregmagolan/test-a/@bin/test.js",
1617
install_source_map_support = False,
1718
data = ["@fine_grained_goldens//@gregmagolan/test-a:test-a"] + kwargs.pop("data", []),
19+
templated_args = ["--nobazel_patch_module_resolver"] + kwargs.pop("templated_args", []),
1820
**kwargs
1921
)

internal/npm_install/test/golden/jasmine/index.bzl.golden

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ def jasmine(**kwargs):
88
entry_point = "@fine_grained_goldens//:node_modules/jasmine/bin/jasmine.js",
99
install_source_map_support = False,
1010
data = ["@fine_grained_goldens//jasmine:jasmine"] + kwargs.pop("data", []),
11+
templated_args = ["--nobazel_patch_module_resolver"] + kwargs.pop("templated_args", []),
1112
**kwargs
1213
)
1314
def jasmine_test(**kwargs):
1415
nodejs_test(
1516
entry_point = "@fine_grained_goldens//:node_modules/jasmine/bin/jasmine.js",
1617
install_source_map_support = False,
1718
data = ["@fine_grained_goldens//jasmine:jasmine"] + kwargs.pop("data", []),
19+
templated_args = ["--nobazel_patch_module_resolver"] + kwargs.pop("templated_args", []),
1820
**kwargs
1921
)

packages/node-patches/BUILD.bazel

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_test")
1516
load("@npm_bazel_rollup//:index.bzl", "rollup_bundle")
16-
load("@npm_node_patches//mocha:index.bzl", "mocha_test")
1717
load("@npm_node_patches//typescript:index.bzl", "tsc")
1818

1919
package(default_visibility = ["//:__subpackages__"])
@@ -52,12 +52,14 @@ tsc(
5252
],
5353
)
5454

55-
mocha_test(
55+
# Like the generated mocha_test but we don't want to run the patches before testing the patches
56+
nodejs_test(
5657
name = "unit_test",
5758
args = ["$(location %s)" % s for s in test_js],
5859
data = js + test_js + [
5960
"@npm_node_patches//:node_modules",
6061
],
62+
entry_point = "@npm_node_patches//:node_modules/mocha/bin/mocha",
6163
tags = [
6264
"fix-windows",
6365
],

0 commit comments

Comments
 (0)