Skip to content

Commit

Permalink
refactor: default to nobazel_patch_module_resolver
Browse files Browse the repository at this point in the history
This turns off our monkey-patches for require() for nodejs_binary, nodejs_test, and macros like jasmine_node_test.
Note it was previously disabled for npm_package_bin and generated index.bzl binaries in bazel-contrib#1440

BREAKING CHANGE:
we no longer patch the require() function, instead you should rely on the linker to make node modules resolvable at the standard location
if this breaks you, try opting-in to the require patches with templated_args = ["--bazel_patch_module_resolver"]

Fixes bazel-contrib#2125
  • Loading branch information
Alex Eagle committed Dec 2, 2020
1 parent b9dc2c1 commit 01c0665
Show file tree
Hide file tree
Showing 28 changed files with 32 additions and 51 deletions.
2 changes: 0 additions & 2 deletions e2e/nodejs_image/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ nodejs_binary(
"@npm//date-fns",
],
entry_point = "main.js",
# Turn off require() monkey patches so linker is required to run inside docker container
templated_args = ["--nobazel_patch_module_resolver"],
)

# bazel run --platforms=@build_bazel_rules_nodejs//toolchains/node:linux_amd64 //:nodejs_image
Expand Down
1 change: 0 additions & 1 deletion examples/nestjs/src/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ nodejs_binary(
"@npm//minimist",
],
entry_point = ":main.ts",
templated_args = ["--nobazel_patch_module_resolver"],
)

jasmine_node_test(
Expand Down
1 change: 1 addition & 0 deletions examples/user_managed_deps/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ jasmine_node_test(
srcs = glob(["*.spec.js"]),
node_modules = "//:node_modules",
tags = ["no-local-jasmine-deps"],
templated_args = ["--bazel_patch_module_resolver"],
deps = [
":decrement",
":program",
Expand Down
5 changes: 4 additions & 1 deletion internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ nodejs_test(
"//:.bazelversion",
],
entry_point = ":check_bazel_version.js",
templated_args = [BAZEL_VERSION],
templated_args = [
"--bazel_patch_module_resolver",
BAZEL_VERSION,
],
)

# Detect if the build is running under --stamp
Expand Down
5 changes: 4 additions & 1 deletion internal/bazel_integration_test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ nodejs_binary(
configuration_env_vars = ["BAZEL_INTEGRATION_TEST_DEBUG"],
data = ["@npm//tmp"],
entry_point = ":test_runner.js",
templated_args = ["--node_options=--max-old-space-size=1024"],
templated_args = [
"--bazel_patch_module_resolver",
"--node_options=--max-old-space-size=1024",
],
)

filegroup(
Expand Down
1 change: 0 additions & 1 deletion internal/linker/test/local/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ jasmine_node_test(
name = "test",
srcs = ["test.js"],
tags = ["local"],
templated_args = ["--nobazel_patch_module_resolver"],
deps = ["//internal/linker/test/local/fit"],
)
1 change: 0 additions & 1 deletion internal/linker/test/workspace_link/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ jasmine_node_test(
name = "test",
srcs = ["test.js"],
link_workspace_root = True,
templated_args = ["--nobazel_patch_module_resolver"],
deps = [
"//internal/linker/test/workspace_link/bar",
"//internal/linker/test/workspace_link/foo",
Expand Down
6 changes: 2 additions & 4 deletions internal/node/launcher.sh
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ EXIT_CODE_CAPTURE=""

RUN_LINKER=true
NODE_PATCHES=true
# TODO(alex): change the default to false
PATCH_REQUIRE=true
PATCH_REQUIRE=false
for ARG in ${ALL_ARGS[@]+"${ALL_ARGS[@]}"}; do
case "$ARG" in
# Supply custom linker arguments for first-party dependencies
Expand All @@ -193,10 +192,9 @@ for ARG in ${ALL_ARGS[@]+"${ALL_ARGS[@]}"}; do
--bazel_capture_exit_code=*) EXIT_CODE_CAPTURE="${ARG#--bazel_capture_exit_code=}" ;;
# Disable the node_loader.js monkey patches for require()
# Note that this means you need an explicit runfiles helper library
# This flag is now a no-op since the default is also false
--nobazel_patch_module_resolver) PATCH_REQUIRE=false ;;
# Enable the node_loader.js monkey patches for require()
# Currently a no-op, but specifying this makes the behavior unchanged when we update
# the default for PATCH_REQUIRE above
--bazel_patch_module_resolver) PATCH_REQUIRE=true ;;
# Disable the --require node-patches (undocumented and unused; only here as an escape value)
--nobazel_node_patches) NODE_PATCHES=false ;;
Expand Down
11 changes: 2 additions & 9 deletions internal/node/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ nodejs_test(
"@npm//node_resolve_nested_main",
],
entry_point = ":module_resolution.spec.js",
templated_args = ["--bazel_patch_module_resolver"],
)

nodejs_test(
Expand Down Expand Up @@ -218,6 +219,7 @@ nodejs_test(
"@npm//node_resolve_nested_main",
],
entry_point = ":module_resolution_built.spec.js",
templated_args = ["--bazel_patch_module_resolver"],
)

nodejs_test(
Expand Down Expand Up @@ -331,14 +333,6 @@ jasmine_node_test(
"dir_output",
"minified.js",
],
# Turn on the linker & turn off require patches so that the external workspace jasmine_node_test
# entry point npm/@bazel/jasmine/jasmine_runner.js's require('@bazel/jasmine') is exercised without
# require patches.
templated_args = select({
# TODO: fix this linker assertion on Windows
"@bazel_tools//src/conditions:host_windows": [],
"//conditions:default": ["--nobazel_patch_module_resolver"],
}),
)

[nodejs_toolchain_test(
Expand Down Expand Up @@ -491,5 +485,4 @@ nodejs_test(
name = "main_test",
data = [":main_lib"],
entry_point = ":main.js",
templated_args = ["--nobazel_patch_module_resolver"],
)
1 change: 1 addition & 0 deletions internal/npm_install/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ nodejs_binary(
"//third_party/npm/node_modules/named-amd",
],
entry_point = ":browserify-wrapped.js",
templated_args = ["--bazel_patch_module_resolver"],
visibility = ["//visibility:public"],
)
9 changes: 3 additions & 6 deletions internal/npm_install/generate_build_file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1108,8 +1108,7 @@ export function printPackageBin(pkg: Dep) {
nodejs_binary(
name = "${name}",
entry_point = "//:node_modules/${pkg._dir}/${path}",
data = [${data.map(p => `"${p}"`).join(', ')}],
templated_args = ["--nobazel_patch_module_resolver"],${additionalAttributes(pkg, name)}
data = [${data.map(p => `"${p}"`).join(', ')}],${additionalAttributes(pkg, name)}
)
`;
}
Expand Down Expand Up @@ -1144,8 +1143,7 @@ def ${name.replace(/-/g, '_')}(**kwargs):
nodejs_binary(
entry_point = "@${WORKSPACE}//:node_modules/${pkg._dir}/${path}",
data = [${data.map(p => `"${p}"`).join(', ')}] + kwargs.pop("data", []),
templated_args = ["--nobazel_patch_module_resolver"] + kwargs.pop("templated_args", []),${
additionalAttributes(pkg, name)}
templated_args = kwargs.pop("templated_args", []),${additionalAttributes(pkg, name)}
**kwargs
)
Expand All @@ -1154,8 +1152,7 @@ def ${name.replace(/-/g, '_')}_test(**kwargs):
nodejs_test(
entry_point = "@${WORKSPACE}//:node_modules/${pkg._dir}/${path}",
data = [${data.map(p => `"${p}"`).join(', ')}] + kwargs.pop("data", []),
templated_args = ["--nobazel_patch_module_resolver"] + kwargs.pop("templated_args", []),${
additionalAttributes(pkg, name)}
templated_args = kwargs.pop("templated_args", []),${additionalAttributes(pkg, name)}
**kwargs
)
`;
Expand Down
7 changes: 3 additions & 4 deletions internal/npm_install/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -673,8 +673,7 @@ function printPackageBin(pkg) {
nodejs_binary(
name = "${name}",
entry_point = "//:node_modules/${pkg._dir}/${path}",
data = [${data.map(p => `"${p}"`).join(', ')}],
templated_args = ["--nobazel_patch_module_resolver"],${additionalAttributes(pkg, name)}
data = [${data.map(p => `"${p}"`).join(', ')}],${additionalAttributes(pkg, name)}
)
`;
}
Expand Down Expand Up @@ -706,7 +705,7 @@ def ${name.replace(/-/g, '_')}(**kwargs):
nodejs_binary(
entry_point = "@${WORKSPACE}//:node_modules/${pkg._dir}/${path}",
data = [${data.map(p => `"${p}"`).join(', ')}] + kwargs.pop("data", []),
templated_args = ["--nobazel_patch_module_resolver"] + kwargs.pop("templated_args", []),${additionalAttributes(pkg, name)}
templated_args = kwargs.pop("templated_args", []),${additionalAttributes(pkg, name)}
**kwargs
)
Expand All @@ -715,7 +714,7 @@ def ${name.replace(/-/g, '_')}_test(**kwargs):
nodejs_test(
entry_point = "@${WORKSPACE}//:node_modules/${pkg._dir}/${path}",
data = [${data.map(p => `"${p}"`).join(', ')}] + kwargs.pop("data", []),
templated_args = ["--nobazel_patch_module_resolver"] + kwargs.pop("templated_args", []),${additionalAttributes(pkg, name)}
templated_args = kwargs.pop("templated_args", []),${additionalAttributes(pkg, name)}
**kwargs
)
`;
Expand Down
2 changes: 2 additions & 0 deletions internal/npm_install/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jasmine_node_test(
"@fine_grained_goldens//:golden_files",
"@npm//unidiff",
],
templated_args = ["--bazel_patch_module_resolver"],
)

nodejs_binary(
Expand Down Expand Up @@ -81,6 +82,7 @@ jasmine_node_test(
"//internal/npm_install:browserify-wrapped",
"@npm//date-fns:date-fns.umd.js",
],
templated_args = ["--bazel_patch_module_resolver"],
)

sh_test(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ nodejs_binary(
name = "test",
entry_point = "//:node_modules/@gregmagolan/test-a/@bin/test.js",
data = ["//@gregmagolan/test-a:test-a"],
templated_args = ["--nobazel_patch_module_resolver"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ def test(**kwargs):
nodejs_binary(
entry_point = "@fine_grained_goldens//:node_modules/@gregmagolan/test-a/@bin/test.js",
data = ["@fine_grained_goldens//@gregmagolan/test-a:test-a"] + kwargs.pop("data", []),
templated_args = ["--nobazel_patch_module_resolver"] + kwargs.pop("templated_args", []),
templated_args = kwargs.pop("templated_args", []),
**kwargs
)
def test_test(**kwargs):
nodejs_test(
entry_point = "@fine_grained_goldens//:node_modules/@gregmagolan/test-a/@bin/test.js",
data = ["@fine_grained_goldens//@gregmagolan/test-a:test-a"] + kwargs.pop("data", []),
templated_args = ["--nobazel_patch_module_resolver"] + kwargs.pop("templated_args", []),
templated_args = kwargs.pop("templated_args", []),
**kwargs
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ nodejs_binary(
name = "jasmine",
entry_point = "//:node_modules/jasmine/bin/jasmine.js",
data = ["//jasmine:jasmine"],
templated_args = ["--nobazel_patch_module_resolver"],
)
4 changes: 2 additions & 2 deletions internal/npm_install/test/golden/jasmine/index.bzl.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ def jasmine(**kwargs):
nodejs_binary(
entry_point = "@fine_grained_goldens//:node_modules/jasmine/bin/jasmine.js",
data = ["@fine_grained_goldens//jasmine:jasmine"] + kwargs.pop("data", []),
templated_args = ["--nobazel_patch_module_resolver"] + kwargs.pop("templated_args", []),
templated_args = kwargs.pop("templated_args", []),
**kwargs
)
def jasmine_test(**kwargs):
nodejs_test(
entry_point = "@fine_grained_goldens//:node_modules/jasmine/bin/jasmine.js",
data = ["@fine_grained_goldens//jasmine:jasmine"] + kwargs.pop("data", []),
templated_args = ["--nobazel_patch_module_resolver"] + kwargs.pop("templated_args", []),
templated_args = kwargs.pop("templated_args", []),
**kwargs
)
1 change: 1 addition & 0 deletions internal/pkg_npm/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ nodejs_binary(
name = "packager",
data = ["//third_party/github.com/gjtorikian/isBinaryFile"],
entry_point = ":packager.js",
templated_args = ["--bazel_patch_module_resolver"],
)

nodejs_binary(
Expand Down
1 change: 0 additions & 1 deletion internal/pkg_npm/test/directory/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ nodejs_test(
":folder_pkg",
],
entry_point = "main.js",
templated_args = ["--nobazel_patch_module_resolver"],
)
1 change: 0 additions & 1 deletion internal/pkg_npm/test/linking/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,4 @@ nodejs_test(
"//internal/pkg_npm/test/linking/fuz:scoped_fuz",
],
entry_point = ":main.js",
templated_args = ["--nobazel_patch_module_resolver"],
)
1 change: 1 addition & 0 deletions internal/pkg_web/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ nodejs_binary(
],
entry_point = ":assembler.js",
node_modules = ":node_modules_none",
templated_args = ["--bazel_patch_module_resolver"],
)

# BEGIN-INTERNAL
Expand Down
1 change: 1 addition & 0 deletions internal/pkg_web/test2/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,5 @@ jasmine_node_test(
tags = [
"fix-windows",
],
templated_args = ["--bazel_patch_module_resolver"],
)
5 changes: 0 additions & 5 deletions internal/providers/node_runtime_deps_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,6 @@ def run_node(ctx, inputs, arguments, executable, **kwargs):
add_arg(arguments, "--bazel_capture_exit_code=%s" % exit_code_file.path)
outputs = outputs + [exit_code_file]

# By using the run_node helper, you suggest that your program
# doesn't implicitly use runfiles to require() things
# To access runfiles, you must use a runfiles helper in the program instead
add_arg(arguments, "--nobazel_patch_module_resolver")

env = kwargs.pop("env", {})

# Always forward the COMPILATION_MODE to node process as an environment variable
Expand Down
2 changes: 0 additions & 2 deletions packages/cypress/internal/template.cypress_web_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ def cypress_web_test(
] + srcs,
entry_point = "@build_bazel_rules_nodejs//packages/cypress:internal/run-cypress.js",
templated_args = [
"--nobazel_patch_module_resolver",
"$(rootpath {config_file})".format(config_file = config_file),
"$(rootpath {cypress_plugin})".format(cypress_plugin = cypress_plugin),
"$(rootpath {cypress_archive})".format(cypress_archive = cypress_archive),
Expand Down Expand Up @@ -154,7 +153,6 @@ def cypress_web_test_global_cache(
] + srcs,
entry_point = "@build_bazel_rules_nodejs//packages/cypress:internal/run-cypress.js",
templated_args = [
"--nobazel_patch_module_resolver",
"$(rootpath {config_file})".format(config_file = config_file),
"$(rootpath {cypress_plugin})".format(cypress_plugin = cypress_plugin),
] + templated_args,
Expand Down
1 change: 0 additions & 1 deletion packages/rollup/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ nodejs_binary(
data = ["@npm//rollup:rollup"],
entry_point = "@npm//:node_modules/rollup/dist/bin/rollup",
templated_args = [
"--nobazel_patch_module_resolver",
"--node_options=--max-old-space-size=<SOME_SIZE>",
],
)
Expand Down
1 change: 0 additions & 1 deletion packages/typescript/internal/ts_project.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,6 @@ def ts_project_macro(
],
entry_point = Label("//packages/typescript/internal/worker:worker_adapter"),
templated_args = [
"--nobazel_patch_module_resolver",
"$(execpath {})".format(Label(worker_tsc_bin)),
"--project",
"$(execpath {})".format(tsconfig),
Expand Down
3 changes: 1 addition & 2 deletions packages/typescript/test/some_module/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ nodejs_binary(

# custom rule using the executable above via run_node
# invoked via bazel build //packages/typescript/test/some_module:test
# as --nobazel_patch_module_resolver is added by default by run_node, the entry_point on the executable is resolved
# to the ts file - writer.ts
# the entry_point on the executable is resolved to the ts file - writer.ts
# this works for bazel run //packages/typescript/test/some_module:writer_bin as this will patch require and resolve the
# ts to js
ts_write_file(
Expand Down
4 changes: 2 additions & 2 deletions packages/typescript/test/ts_project/a/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
// (05:54:49) ERROR: C:/b/bk-windows-b4qr/bazel/rules-nodejs-nodejs/packages/typescript/test/ts_project/b/BUILD.bazel:6:1: Compiling TypeScript project packages/typescript/test/ts_project/b/tsconfig.json failed (Exit 2): tsc.bat failed: error executing command
// cd C:/b/uuxnwop3/execroot/build_bazel_rules_nodejs
// SET COMPILATION_MODE=fastbuild
// bazel-out/host/bin/external/npm/typescript/bin/tsc.bat -p packages/typescript/test/ts_project/b/tsconfig.json --outDir bazel-out/x64_windows-fastbuild/bin/packages/typescript/test/ts_project/b --declarationDir bazel-out/x64_windows-fastbuild/bin/packages/typescript/test/ts_project/b --bazel_node_modules_manifest=bazel-out/x64_windows-fastbuild/bin/packages/typescript/test/ts_project/b/_tsconfig.module_mappings.json --nobazel_patch_module_resolver
// bazel-out/host/bin/external/npm/typescript/bin/tsc.bat -p packages/typescript/test/ts_project/b/tsconfig.json --outDir bazel-out/x64_windows-fastbuild/bin/packages/typescript/test/ts_project/b --declarationDir bazel-out/x64_windows-fastbuild/bin/packages/typescript/test/ts_project/b --bazel_node_modules_manifest=bazel-out/x64_windows-fastbuild/bin/packages/typescript/test/ts_project/b/_tsconfig.module_mappings.json
// Execution platform: @local_config_platform//:host
// packages/typescript/test/ts_project/b/b.ts(1,17): error TS6305: Output file 'C:/b/uuxnwop3/execroot/build_bazel_rules_nodejs/packages/typescript/test/ts_project/a/a.d.ts' has not been built from source file 'C:/b/uuxnwop3/execroot/build_bazel_rules_nodejs/packages/typescript/test/ts_project/a/a.ts'.
"outDir": "../../../../../bazel-out/x64_windows-fastbuild/bin/packages/typescript/test/ts_project/a",
}
}
}

0 comments on commit 01c0665

Please sign in to comment.