Skip to content

Commit b187d50

Browse files
Greg Magolangregmagolan
authored andcommitted
fix(builtin): linker fix for when not running in execroot
1 parent 4d2000b commit b187d50

3 files changed

Lines changed: 22 additions & 6 deletions

File tree

internal/linker/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,10 @@ function resolveRoot(root, startCwd, isExecroot, runfiles) {
126126
}
127127
const match = startCwd.match(/(\/bazel-out\/|\/bazel-~1\/x64_wi~1\/)/);
128128
if (!match) {
129-
panic(`No 'bazel-out' folder found in path '${startCwd}'!`);
130-
return '';
129+
if (!root) {
130+
return `${startCwd}/node_modules`;
131+
}
132+
return path.resolve(`${startCwd}/../${root}`);
131133
}
132134
const symlinkRoot = startCwd.slice(0, match.index);
133135
process.chdir(symlinkRoot);

internal/linker/link_node_modules.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,21 @@ async function resolveRoot(
179179
// c:/b/ojvxx6nx/execroot/build_~1/bazel-~1/x64_wi~1/bin/internal/npm_in~1/test
180180
const match = startCwd.match(/(\/bazel-out\/|\/bazel-~1\/x64_wi~1\/)/);
181181
if (!match) {
182-
panic(`No 'bazel-out' folder found in path '${startCwd}'!`);
183-
return '';
182+
// No execroot found. This can happen if we are inside a nodejs_image or a nodejs_binary is
183+
// run manually.
184+
185+
if (!root) {
186+
// If there is no root, which will be the case if there are no third-party modules
187+
// dependencies for this target, simply link to node_modules at the cwd.
188+
return `${startCwd}/node_modules`;
189+
}
190+
191+
// If there is a root then attempt to symlink as if we are in runfiles in a sandbox. This will
192+
// be the case for nodejs_image.
193+
return path.resolve(`${startCwd}/../${root}`)
184194
}
195+
196+
// We've found the execroot
185197
const symlinkRoot = startCwd.slice(0, match.index);
186198
process.chdir(symlinkRoot);
187199

internal/node/launcher.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,11 @@ if [[ "$PWD" == *"/bazel-out/"* ]]; then
276276
readonly execroot=${PWD:0:${index}}
277277
export BAZEL_PATCH_GUARDS="${execroot}/node_modules"
278278
else
279-
# We are in execroot, linker node_modules is in the PWD
279+
# We are in execroot or in some other context all together such as a nodejs_image or a manually
280+
# run nodejs_binary. If this is execroot then linker node_modules is in the PWD. If this another
281+
# context then it is safe to assume the node_modules are there and guard that directory if it exists.
280282
export BAZEL_PATCH_GUARDS="${PWD}/node_modules"
281-
fi
283+
fi
282284
if [[ -n "${BAZEL_NODE_MODULES_ROOT:-}" ]]; then
283285
if [[ "${BAZEL_NODE_MODULES_ROOT}" != "${BAZEL_WORKSPACE}/node_modules" ]]; then
284286
# If BAZEL_NODE_MODULES_ROOT is set and it is not , add it to the list of bazel patch guards

0 commit comments

Comments
 (0)