From b187d5064a7fda0e7aff433311077e7c3323f97b Mon Sep 17 00:00:00 2001 From: Greg Magolan Date: Thu, 16 Jul 2020 00:13:17 -0700 Subject: [PATCH] fix(builtin): linker fix for when not running in execroot --- internal/linker/index.js | 6 ++++-- internal/linker/link_node_modules.ts | 16 ++++++++++++++-- internal/node/launcher.sh | 6 ++++-- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/internal/linker/index.js b/internal/linker/index.js index 1500fe1230..0adba74cde 100644 --- a/internal/linker/index.js +++ b/internal/linker/index.js @@ -126,8 +126,10 @@ function resolveRoot(root, startCwd, isExecroot, runfiles) { } const match = startCwd.match(/(\/bazel-out\/|\/bazel-~1\/x64_wi~1\/)/); if (!match) { - panic(`No 'bazel-out' folder found in path '${startCwd}'!`); - return ''; + if (!root) { + return `${startCwd}/node_modules`; + } + return path.resolve(`${startCwd}/../${root}`); } const symlinkRoot = startCwd.slice(0, match.index); process.chdir(symlinkRoot); diff --git a/internal/linker/link_node_modules.ts b/internal/linker/link_node_modules.ts index f196f8f668..e4e7f92f89 100644 --- a/internal/linker/link_node_modules.ts +++ b/internal/linker/link_node_modules.ts @@ -179,9 +179,21 @@ async function resolveRoot( // c:/b/ojvxx6nx/execroot/build_~1/bazel-~1/x64_wi~1/bin/internal/npm_in~1/test const match = startCwd.match(/(\/bazel-out\/|\/bazel-~1\/x64_wi~1\/)/); if (!match) { - panic(`No 'bazel-out' folder found in path '${startCwd}'!`); - return ''; + // No execroot found. This can happen if we are inside a nodejs_image or a nodejs_binary is + // run manually. + + if (!root) { + // If there is no root, which will be the case if there are no third-party modules + // dependencies for this target, simply link to node_modules at the cwd. + return `${startCwd}/node_modules`; + } + + // If there is a root then attempt to symlink as if we are in runfiles in a sandbox. This will + // be the case for nodejs_image. + return path.resolve(`${startCwd}/../${root}`) } + + // We've found the execroot const symlinkRoot = startCwd.slice(0, match.index); process.chdir(symlinkRoot); diff --git a/internal/node/launcher.sh b/internal/node/launcher.sh index 723486f563..4baa55ecac 100644 --- a/internal/node/launcher.sh +++ b/internal/node/launcher.sh @@ -276,9 +276,11 @@ if [[ "$PWD" == *"/bazel-out/"* ]]; then readonly execroot=${PWD:0:${index}} export BAZEL_PATCH_GUARDS="${execroot}/node_modules" else - # We are in execroot, linker node_modules is in the PWD + # We are in execroot or in some other context all together such as a nodejs_image or a manually + # run nodejs_binary. If this is execroot then linker node_modules is in the PWD. If this another + # context then it is safe to assume the node_modules are there and guard that directory if it exists. export BAZEL_PATCH_GUARDS="${PWD}/node_modules" -fi +fi if [[ -n "${BAZEL_NODE_MODULES_ROOT:-}" ]]; then if [[ "${BAZEL_NODE_MODULES_ROOT}" != "${BAZEL_WORKSPACE}/node_modules" ]]; then # If BAZEL_NODE_MODULES_ROOT is set and it is not , add it to the list of bazel patch guards