From b10d230ff927736b88267998b17f501e71db0276 Mon Sep 17 00:00:00 2001 From: Greg Magolan Date: Fri, 10 Jan 2020 15:21:47 -0800 Subject: [PATCH] =?UTF-8?q?fix:=20separate=20nodejs=20require=20patches=20?= =?UTF-8?q?from=20loader=20and=20=E2=80=94require=20them=20first?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/BUILD.bazel | 2 +- internal/linker/link_node_modules.bzl | 2 +- .../linker/test/integration/run_program.sh | 4 +- internal/node/BUILD.bazel | 6 +- .../node/{node_launcher.sh => launcher.sh} | 36 ++++++--- internal/node/loader.js | 37 ++++++++++ internal/node/node.bzl | 74 +++++++++++-------- ...azel_require_script.js => node_patches.js} | 0 .../node/{node_loader.js => require_patch.js} | 68 ++++++++--------- internal/node/test/BUILD.bazel | 2 +- 10 files changed, 143 insertions(+), 88 deletions(-) rename internal/node/{node_launcher.sh => launcher.sh} (87%) create mode 100644 internal/node/loader.js rename internal/node/{bazel_require_script.js => node_patches.js} (100%) rename internal/node/{node_loader.js => require_patch.js} (90%) diff --git a/examples/BUILD.bazel b/examples/BUILD.bazel index 6eb132a96f..1676f256de 100644 --- a/examples/BUILD.bazel +++ b/examples/BUILD.bazel @@ -64,7 +64,7 @@ example_integration_test( name = "examples_react_webpack", # TODO: add some tests in the example bazel_commands = ["build ..."], - # TODO(alexeagle): somehow this is broken by the new node-patches based bazel_require_script + # TODO(alexeagle): somehow this is broken by the new node-patches based node_patches script # ERROR: D:/temp/tmp-6900sejcsrcttpdb/BUILD.bazel:37:1: output 'app.bundle.js' was not created tags = ["no-bazelci-windows"], ) diff --git a/internal/linker/link_node_modules.bzl b/internal/linker/link_node_modules.bzl index d3d2dcb597..01d4b1dddc 100644 --- a/internal/linker/link_node_modules.bzl +++ b/internal/linker/link_node_modules.bzl @@ -69,7 +69,7 @@ def write_node_modules_manifest(ctx, extra_data = []): mappings[k] = v # Write the result to a file, and use the magic node option --bazel_node_modules_manifest - # The node_launcher.sh will peel off this argument and pass it to the linker rather than the program. + # The launcher.sh will peel off this argument and pass it to the linker rather than the program. modules_manifest = ctx.actions.declare_file("_%s.module_mappings.json" % ctx.label.name) content = { "bin": ctx.bin_dir.path, diff --git a/internal/linker/test/integration/run_program.sh b/internal/linker/test/integration/run_program.sh index 4a85b7a822..2fb0c77ea5 100755 --- a/internal/linker/test/integration/run_program.sh +++ b/internal/linker/test/integration/run_program.sh @@ -13,8 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -# This shell script is a minimal fixture for the node_launcher.sh script -# with a critical difference: instead of calling the node_loader.js script +# This shell script is a minimal fixture for the launcher.sh script +# with a critical difference: instead of calling the loader.js script # with the users program passed as an argument (allowing us to patch the node # loader), this one just runs vanilla node with the users program as the argument # which lets us assert that the linker is the reason the program works. diff --git a/internal/node/BUILD.bazel b/internal/node/BUILD.bazel index 9cbccb5bfa..01f1454dd3 100644 --- a/internal/node/BUILD.bazel +++ b/internal/node/BUILD.bazel @@ -30,8 +30,8 @@ bzl_library( exports_files([ "node.bzl", # Exported to be consumed for generating stardoc. "node_repositories.bzl", # Exported to be consumed for generating stardoc. - "node_launcher.sh", - "node_loader.js", + "launcher.sh", + "loader.js", ]) filegroup( @@ -49,5 +49,5 @@ filegroup( golden_file_test( name = "checked_in_node_patches", actual = "//packages/node-patches:bundle", - golden = "bazel_require_script.js", + golden = "node_patches.js", ) diff --git a/internal/node/node_launcher.sh b/internal/node/launcher.sh similarity index 87% rename from internal/node/node_launcher.sh rename to internal/node/launcher.sh index bdc98f8cbe..7df9fae5ad 100644 --- a/internal/node/node_launcher.sh +++ b/internal/node/launcher.sh @@ -143,38 +143,50 @@ fi # Export the location of the runfiles helpers script export BAZEL_NODE_RUNFILES_HELPER=$(rlocation "TEMPLATED_runfiles_helper_script") -# Export the location of the loader script as it can be used to boostrap +# Export the location of the require patch script as it can be used to boostrap # node require patch if needed -export BAZEL_NODE_PATCH_REQUIRE=$(rlocation "TEMPLATED_loader_path") +export BAZEL_NODE_PATCH_REQUIRE=$(rlocation "TEMPLATED_require_patch_script") + +# The main entry point +MAIN=$(rlocation "TEMPLATED_loader_script") readonly repository_args=$(rlocation "TEMPLATED_repository_args") -MAIN=$(rlocation "TEMPLATED_loader_path") readonly link_modules_script=$(rlocation "TEMPLATED_link_modules_script") -bazel_require_script=$(rlocation "TEMPLATED_bazel_require_script") +node_patches_script=$(rlocation "TEMPLATED_node_patches_script") +require_patch_script=${BAZEL_NODE_PATCH_REQUIRE} # Node's --require option assumes that a non-absolute path not starting with `.` is # a module, so that you can do --require=source-map-support/register # So if the require script is not absolute, we must make it so -case "${bazel_require_script}" in +case "${node_patches_script}" in + # Absolute path on unix + /* ) ;; + # Absolute path on Windows, e.g. C:/path/to/thing + [a-zA-Z]:/* ) ;; + # Otherwise it needs to be made relative + * ) node_patches_script="./${node_patches_script}" ;; +esac +case "${require_patch_script}" in # Absolute path on unix /* ) ;; # Absolute path on Windows, e.g. C:/path/to/thing [a-zA-Z]:/* ) ;; # Otherwise it needs to be made relative - * ) bazel_require_script="./${bazel_require_script}" ;; + * ) require_patch_script="./${require_patch_script}" ;; esac source $repository_args ARGS=() -NODE_OPTIONS=() +LAUNCHER_NODE_OPTIONS=( "--require" "$require_patch_script" ) +USER_NODE_OPTIONS=() ALL_ARGS=(TEMPLATED_args $NODE_REPOSITORY_ARGS "$@") for ARG in "${ALL_ARGS[@]:-}"; do case "$ARG" in --bazel_node_modules_manifest=*) MODULES_MANIFEST="${ARG#--bazel_node_modules_manifest=}" ;; --nobazel_patch_module_resolver) MAIN="TEMPLATED_script_path" - NODE_OPTIONS+=( "--require" "$bazel_require_script" ) + LAUNCHER_NODE_OPTIONS=( "--require" "$node_patches_script" ) # In this case we should always run the linker # For programs which are called with bazel run or bazel test, there will be no additional runtime @@ -182,7 +194,7 @@ for ARG in "${ALL_ARGS[@]:-}"; do # of the binary itself MODULES_MANIFEST=${MODULES_MANIFEST:-$(rlocation "TEMPLATED_modules_manifest")} ;; - --node_options=*) NODE_OPTIONS+=( "${ARG#--node_options=}" ) ;; + --node_options=*) USER_NODE_OPTIONS+=( "${ARG#--node_options=}" ) ;; *) ARGS+=( "$ARG" ) esac done @@ -192,7 +204,7 @@ if [[ -n "${MODULES_MANIFEST:-}" ]]; then "${node}" "${link_modules_script}" "${MODULES_MANIFEST:-}" fi -# Tell the bazel_require_script that programs should not escape the execroot +# Tell the node_patches_script that programs should not escape the execroot # Bazel always sets the PWD to execroot/my_wksp so we go up one directory. export BAZEL_PATCH_ROOT=$(dirname $PWD) @@ -206,12 +218,12 @@ if [ "${EXPECTED_EXIT_CODE}" -eq "0" ]; then # handled by the node process. # If we had merely forked a child process here, we'd be responsible # for forwarding those OS interactions. - exec "${node}" "${NODE_OPTIONS[@]:-}" "${MAIN}" "${ARGS[@]:-}" + exec "${node}" "${LAUNCHER_NODE_OPTIONS[@]:-}" "${USER_NODE_OPTIONS[@]:-}" "${MAIN}" "${ARGS[@]:-}" # exec terminates execution of this shell script, nothing later will run. fi set +e -"${node}" "${NODE_OPTIONS[@]:-}" "${MAIN}" "${ARGS[@]:-}" +"${node}" "${LAUNCHER_NODE_OPTIONS[@]:-}" "${USER_NODE_OPTIONS[@]:-}" "${MAIN}" "${ARGS[@]:-}" RESULT="$?" set -e diff --git a/internal/node/loader.js b/internal/node/loader.js new file mode 100644 index 0000000000..e696a223ec --- /dev/null +++ b/internal/node/loader.js @@ -0,0 +1,37 @@ +/** + * @license + * Copyright 2017 The Bazel Authors. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @fileoverview NodeJS module loader for bazel. + */ +'use strict'; + +// Ensure that node is added to the path for any subprocess calls +process.env.PATH = [require('path').dirname(process.execPath), process.env.PATH].join( + /^win/i.test(process.platform) ? ';' : ':'); + +if (require.main === module) { + // Set the actual entry point in the arguments list. + // argv[0] == node, argv[1] == entry point. + // NB: 'TEMPLATED_entry_point' below is replaced during the build process. + var mainScript = process.argv[1] = 'TEMPLATED_entry_point'; + try { + module.constructor._load(mainScript, this, /*isMain=*/true); + } catch (e) { + console.error(e.stack || e); + process.exit(1); + } +} diff --git a/internal/node/node.bzl b/internal/node/node.bzl index dde8b81cc7..08bcf22422 100644 --- a/internal/node/node.bzl +++ b/internal/node/node.bzl @@ -78,7 +78,7 @@ def _compute_node_modules_root(ctx): ] if f]) return node_modules_root -def _write_loader_script(ctx): +def _write_require_patch_script(ctx): # Generates the JavaScript snippet of module roots mappings, with each entry # in the form: # {module_name: /^mod_name\b/, module_root: 'path/to/mod_name'} @@ -92,6 +92,22 @@ def _write_loader_script(ctx): node_modules_root = _compute_node_modules_root(ctx) + ctx.actions.expand_template( + template = ctx.file._require_patch_template, + output = ctx.outputs.require_patch_script, + substitutions = { + "TEMPLATED_bin_dir": ctx.bin_dir.path, + "TEMPLATED_gen_dir": ctx.genfiles_dir.path, + "TEMPLATED_install_source_map_support": str(ctx.attr.install_source_map_support).lower(), + "TEMPLATED_module_roots": "\n " + ",\n ".join(module_mappings), + "TEMPLATED_node_modules_root": node_modules_root, + "TEMPLATED_target": str(ctx.label), + "TEMPLATED_user_workspace_name": ctx.workspace_name, + }, + is_executable = True, + ) + +def _write_loader_script(ctx): if len(ctx.attr.entry_point.files.to_list()) != 1: fail("labels in entry_point must contain exactly one file") @@ -106,17 +122,8 @@ def _write_loader_script(ctx): ctx.actions.expand_template( template = ctx.file._loader_template, - output = ctx.outputs.loader, - substitutions = { - "TEMPLATED_bin_dir": ctx.bin_dir.path, - "TEMPLATED_entry_point": entry_point_path, - "TEMPLATED_gen_dir": ctx.genfiles_dir.path, - "TEMPLATED_install_source_map_support": str(ctx.attr.install_source_map_support).lower(), - "TEMPLATED_module_roots": "\n " + ",\n ".join(module_mappings), - "TEMPLATED_node_modules_root": node_modules_root, - "TEMPLATED_target": str(ctx.label), - "TEMPLATED_user_workspace_name": ctx.workspace_name, - }, + output = ctx.outputs.loader_script, + substitutions = {"TEMPLATED_entry_point": entry_point_path}, is_executable = True, ) @@ -165,6 +172,7 @@ def _nodejs_binary_impl(ctx): sources_depsets.append(d.files) sources = depset(transitive = sources_depsets) + _write_require_patch_script(ctx) _write_loader_script(ctx) env_vars = "export BAZEL_TARGET=%s\n" % ctx.label @@ -191,7 +199,7 @@ def _nodejs_binary_impl(ctx): node_tool_files.append(ctx.file._link_modules_script) node_tool_files.append(ctx.file._runfiles_helper_script) - node_tool_files.append(ctx.file._bazel_require_script) + node_tool_files.append(ctx.file._node_patches_script) node_tool_files.append(node_modules_manifest) is_builtin = ctx.attr._node.label.workspace_name in ["nodejs_%s" % p for p in BUILT_IN_NODE_PLATFORMS] @@ -201,20 +209,21 @@ def _nodejs_binary_impl(ctx): expand_location_into_runfiles(ctx, a, ctx.attr.data) for a in ctx.attr.templated_args ]), - "TEMPLATED_bazel_require_script": _to_manifest_path(ctx, ctx.file._bazel_require_script), "TEMPLATED_env_vars": env_vars, "TEMPLATED_expected_exit_code": str(expected_exit_code), "TEMPLATED_link_modules_script": _to_manifest_path(ctx, ctx.file._link_modules_script), - "TEMPLATED_loader_path": _to_manifest_path(ctx, ctx.outputs.loader), + "TEMPLATED_loader_script": _to_manifest_path(ctx, ctx.outputs.loader_script), "TEMPLATED_modules_manifest": _to_manifest_path(ctx, node_modules_manifest), + "TEMPLATED_node_patches_script": _to_manifest_path(ctx, ctx.file._node_patches_script), "TEMPLATED_repository_args": _to_manifest_path(ctx, ctx.file._repository_args), + "TEMPLATED_require_patch_script": _to_manifest_path(ctx, ctx.outputs.require_patch_script), "TEMPLATED_runfiles_helper_script": _to_manifest_path(ctx, ctx.file._runfiles_helper_script), "TEMPLATED_script_path": _to_execroot_path(ctx, ctx.file.entry_point), "TEMPLATED_vendored_node": "" if is_builtin else strip_external(ctx.file._node.path), } ctx.actions.expand_template( template = ctx.file._launcher_template, - output = ctx.outputs.script, + output = ctx.outputs.launcher_sh, substitutions = substitutions, is_executable = True, ) @@ -222,14 +231,15 @@ def _nodejs_binary_impl(ctx): runfiles = [] runfiles.extend(node_tool_files) runfiles.extend(ctx.files._bash_runfile_helpers) - runfiles.append(ctx.outputs.loader) + runfiles.append(ctx.outputs.loader_script) + runfiles.append(ctx.outputs.require_patch_script) runfiles.append(ctx.file._repository_args) if is_windows(ctx): - runfiles.append(ctx.outputs.script) - executable = create_windows_native_launcher_script(ctx, ctx.outputs.script) + runfiles.append(ctx.outputs.launcher_sh) + executable = create_windows_native_launcher_script(ctx, ctx.outputs.launcher_sh) else: - executable = ctx.outputs.script + executable = ctx.outputs.launcher_sh # entry point is only needed in runfiles if it is a .js file if ctx.file.entry_point.extension == "js": @@ -241,7 +251,8 @@ def _nodejs_binary_impl(ctx): runfiles = ctx.runfiles( transitive_files = depset(runfiles), files = node_tool_files + [ - ctx.outputs.loader, + ctx.outputs.loader_script, + ctx.outputs.require_patch_script, ] + ctx.files._source_map_support_files + # We need this call to the list of Files. @@ -426,12 +437,8 @@ jasmine_node_test( """, ), "_bash_runfile_helpers": attr.label(default = Label("@bazel_tools//tools/bash/runfiles")), - "_bazel_require_script": attr.label( - default = Label("//internal/node:bazel_require_script.js"), - allow_single_file = True, - ), "_launcher_template": attr.label( - default = Label("//internal/node:node_launcher.sh"), + default = Label("//internal/node:launcher.sh"), allow_single_file = True, ), "_link_modules_script": attr.label( @@ -439,17 +446,25 @@ jasmine_node_test( allow_single_file = True, ), "_loader_template": attr.label( - default = Label("//internal/node:node_loader.js"), + default = Label("//internal/node:loader.js"), allow_single_file = True, ), "_node": attr.label( default = Label("@nodejs//:node_bin"), allow_single_file = True, ), + "_node_patches_script": attr.label( + default = Label("//internal/node:node_patches.js"), + allow_single_file = True, + ), "_repository_args": attr.label( default = Label("@nodejs//:bin/node_repo_args.sh"), allow_single_file = True, ), + "_require_patch_template": attr.label( + default = Label("//internal/node:require_patch.js"), + allow_single_file = True, + ), "_runfiles_helper_script": attr.label( default = Label("//internal/linker:runfiles_helper.js"), allow_single_file = True, @@ -465,8 +480,9 @@ jasmine_node_test( } _NODEJS_EXECUTABLE_OUTPUTS = { - "loader": "%{name}_loader.js", - "script": "%{name}.sh", + "launcher_sh": "%{name}.sh", + "loader_script": "%{name}_loader.js", + "require_patch_script": "%{name}_require_patch.js", } # The name of the declared rule appears in diff --git a/internal/node/bazel_require_script.js b/internal/node/node_patches.js similarity index 100% rename from internal/node/bazel_require_script.js rename to internal/node/node_patches.js diff --git a/internal/node/node_loader.js b/internal/node/require_patch.js similarity index 90% rename from internal/node/node_loader.js rename to internal/node/require_patch.js index c354fc171c..e0b251d28c 100644 --- a/internal/node/node_loader.js +++ b/internal/node/require_patch.js @@ -25,11 +25,12 @@ var path = require('path'); var fs = require('fs'); -const isWindows = /^win/i.test(process.platform); // Ensure that node is added to the path for any subprocess calls +const isWindows = /^win/i.test(process.platform); process.env.PATH = [path.dirname(process.execPath), process.env.PATH].join(isWindows ? ';' : ':'); const VERBOSE_LOGS = !!process.env['VERBOSE_LOGS']; + // If you're really in trouble debugging a module resolution, change this to true const SILLY_VERBOSE = false; @@ -41,32 +42,28 @@ function log_verbose(...m) { /** * The module roots as pairs of a RegExp to match the require path, and a * module_root to substitute for the require path. - * Ordered by regex length, longest to smallest. + * Ordered by regex length, longest to smallest. * @type {!Array<{module_name: RegExp, module_root: string}>} */ -var MODULE_ROOTS = [ - TEMPLATED_module_roots -].sort((a, b) => b.module_name.toString().length - a.module_name.toString().length); +var MODULE_ROOTS = [TEMPLATED_module_roots].sort( + (a, b) => b.module_name.toString().length - a.module_name.toString().length); const USER_WORKSPACE_NAME = 'TEMPLATED_user_workspace_name'; const NODE_MODULES_ROOT = 'TEMPLATED_node_modules_root'; const BIN_DIR = 'TEMPLATED_bin_dir'; -const ENTRY_POINT = 'TEMPLATED_entry_point'; const GEN_DIR = 'TEMPLATED_gen_dir'; const INSTALL_SOURCE_MAP_SUPPORT = TEMPLATED_install_source_map_support; const TARGET = 'TEMPLATED_target'; -log_verbose(`running ${TARGET} with +log_verbose(`patching require for ${TARGET} cwd: ${process.cwd()} - runfiles: ${process.env.RUNFILES} - + RUNFILES: ${process.env.RUNFILES} + TARGET: ${TARGET} BIN_DIR: ${BIN_DIR} - ENTRY_POINT: ${ENTRY_POINT} GEN_DIR: ${GEN_DIR} INSTALL_SOURCE_MAP_SUPPORT: ${INSTALL_SOURCE_MAP_SUPPORT} MODULE_ROOTS: ${JSON.stringify(MODULE_ROOTS, undefined, 2)} NODE_MODULES_ROOT: ${NODE_MODULES_ROOT} - TARGET: ${TARGET} USER_WORKSPACE_NAME: ${USER_WORKSPACE_NAME} `); @@ -155,9 +152,9 @@ function loadRunfilesManifest(manifestPath) { log_verbose(`using genRoot ${genRoot}`); log_verbose(`using localWorkspacePath ${localWorkspacePath}`); - return { runfilesManifest, reverseRunfilesManifest, binRoot, genRoot, localWorkspacePath }; + return {runfilesManifest, reverseRunfilesManifest, binRoot, genRoot, localWorkspacePath}; } -const { runfilesManifest, reverseRunfilesManifest, binRoot, genRoot, localWorkspacePath } = +const {runfilesManifest, reverseRunfilesManifest, binRoot, genRoot, localWorkspacePath} = // On Windows, Bazel sets RUNFILES_MANIFEST_ONLY=1. // On every platform, Bazel also sets RUNFILES_MANIFEST_FILE, but on Linux // and macOS it's faster to use the symlinks in RUNFILES_DIR rather than resolve @@ -306,16 +303,16 @@ function resolveRunfiles(parent, ...pathSegments) { if (parentRunfile) { runfilesEntry = path.join(path.dirname(parentRunfile), runfilesEntry); } - } else if (runfilesEntry.startsWith(binRoot) || runfilesEntry.startsWith(genRoot) - || runfilesEntry.startsWith(localWorkspacePath)) { + } else if ( + runfilesEntry.startsWith(binRoot) || runfilesEntry.startsWith(genRoot) || + runfilesEntry.startsWith(localWorkspacePath)) { // For absolute paths, replace binRoot, genRoot or localWorkspacePath with // USER_WORKSPACE_NAME to enable lookups. // It's OK to do multiple replacements because all of these are absolute paths with drive // names (e.g. C:\), and on Windows you can't have drive names in the middle of paths. - runfilesEntry = runfilesEntry - .replace(binRoot, `${USER_WORKSPACE_NAME}/`) - .replace(genRoot, `${USER_WORKSPACE_NAME}/`) - .replace(localWorkspacePath, `${USER_WORKSPACE_NAME}/`); + runfilesEntry = runfilesEntry.replace(binRoot, `${USER_WORKSPACE_NAME}/`) + .replace(genRoot, `${USER_WORKSPACE_NAME}/`) + .replace(localWorkspacePath, `${USER_WORKSPACE_NAME}/`); } // Normalize and replace path separators to conform to the ones in the manifest. @@ -354,7 +351,9 @@ function resolveRunfiles(parent, ...pathSegments) { } var originalResolveFilename = module.constructor._resolveFilename; -module.constructor._resolveFilename = function(request, parent, isMain, options) { + +module.constructor._resolveFilename = + function(request, parent, isMain, options) { const parentFilename = (parent && parent.filename) ? parent.filename : undefined; if (SILLY_VERBOSE) log_verbose(`resolve ${request} from ${parentFilename}`); @@ -362,7 +361,7 @@ module.constructor._resolveFilename = function(request, parent, isMain, options) // Attempt to resolve to module root. // This should be the first attempted resolution because: - // - it's fairly cheap to check (regex over a small array); + // - it's fairly cheap to check (regex over a small array); // - it is be very common when there are a lot of packages built from source; if (!isMain) { // Don't resolve to module root if this is the main entry point @@ -422,7 +421,8 @@ module.constructor._resolveFilename = function(request, parent, isMain, options) // If the import is not a built-in module, an absolute, relative import or a // dependency of an npm package, attempt to resolve against the runfiles location try { - const resolved = originalResolveFilename(resolveRunfiles(parentFilename, request), parent, isMain, options); + const resolved = + originalResolveFilename(resolveRunfiles(parentFilename, request), parent, isMain, options); log_verbose(`resolved ${request} within runfiles to ${resolved} from ${parentFilename}`); return resolved; } catch (e) { @@ -444,7 +444,9 @@ module.constructor._resolveFilename = function(request, parent, isMain, options) const parentSegments = relativeParentFilename.split('/'); if (parentSegments[0] !== USER_WORKSPACE_NAME) { try { - const resolved = originalResolveFilename(resolveRunfiles(undefined, parentSegments[0], 'node_modules', request), parent, isMain, options); + const resolved = originalResolveFilename( + resolveRunfiles(undefined, parentSegments[0], 'node_modules', request), parent, isMain, + options); log_verbose( `resolved ${request} within node_modules ` + `(${parentSegments[0]}/node_modules) to ${resolved} from ${relativeParentFilename}`); @@ -458,7 +460,8 @@ module.constructor._resolveFilename = function(request, parent, isMain, options) // If import was not resolved above then attempt to resolve // within the node_modules filegroup in use try { - const resolved = originalResolveFilename(resolveRunfiles(undefined, NODE_MODULES_ROOT, request), parent, isMain, options); + const resolved = originalResolveFilename( + resolveRunfiles(undefined, NODE_MODULES_ROOT, request), parent, isMain, options); log_verbose( `resolved ${request} within node_modules (${NODE_MODULES_ROOT}) to ` + `${resolved} from ${parentFilename}`); @@ -489,8 +492,8 @@ module.constructor._resolveFilename = function(request, parent, isMain, options) // source-map-support. if (INSTALL_SOURCE_MAP_SUPPORT) { try { - const sourcemap_support_package = path.resolve(process.cwd(), - '../build_bazel_rules_nodejs/third_party/github.com/source-map-support'); + const sourcemap_support_package = path.resolve( + process.cwd(), '../build_bazel_rules_nodejs/third_party/github.com/source-map-support'); require(sourcemap_support_package).install(); } catch (_) { log_verbose(`WARNING: source-map-support module not installed. @@ -498,16 +501,3 @@ if (INSTALL_SOURCE_MAP_SUPPORT) { Set install_source_map_support = False in ${TARGET} to turn off this warning.`); } } - -if (require.main === module) { - // Set the actual entry point in the arguments list. - // argv[0] == node, argv[1] == entry point. - // NB: entry_point below is replaced during the build process. - var mainScript = process.argv[1] = ENTRY_POINT; - try { - module.constructor._load(mainScript, this, /*isMain=*/true); - } catch (e) { - console.error(e.stack || e); - process.exit(1); - } -} diff --git a/internal/node/test/BUILD.bazel b/internal/node/test/BUILD.bazel index 3f7058bebd..3769ac77c5 100644 --- a/internal/node/test/BUILD.bazel +++ b/internal/node/test/BUILD.bazel @@ -183,7 +183,7 @@ copy_file( ) # This rule creates a file that alphabetically comes before any source file in this -# package. This genfile can be then set up as runfile to verify that the "node_loader.js" +# package. This genfile can be then set up as runfile to verify that the "loader.js" # properly determines the local workspace path without incorrectly using the genfile as base # for the local workspace path. See: https://github.com/bazelbuild/rules_nodejs/issues/352 write_file(