Skip to content

Commit ebb9481

Browse files
Greg Magolanalexeagle
authored andcommitted
fix: multi-linker linking when only output files in sandbox
1 parent fe1dcab commit ebb9481

File tree

11 files changed

+324
-29
lines changed

11 files changed

+324
-29
lines changed

WORKSPACE

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ yarn_install(
9191
yarn_lock = "//packages/cypress/test:yarn.lock",
9292
)
9393

94+
yarn_install(
95+
name = "rollup_test_multi_linker_deps",
96+
package_json = "//packages/rollup/test/multi_linker:package.json",
97+
package_path = "packages/rollup/test/multi_linker",
98+
symlink_node_modules = False,
99+
yarn_lock = "//packages/rollup/test/multi_linker:yarn.lock",
100+
)
101+
94102
# We have a source dependency on build_bazel_rules_typescript
95103
# so we must repeat its transitive toolchain deps
96104
load("@build_bazel_rules_typescript//:package.bzl", "rules_typescript_dev_dependencies")

internal/linker/index.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -420,26 +420,17 @@ function main(args, runfiles) {
420420
const workspaceNodeModules = `${workspacePath}/node_modules`;
421421
if (packagePath) {
422422
if (yield exists(workspaceNodeModules)) {
423-
let resolvedPackagePath;
424-
if (yield exists(packagePath)) {
425-
yield symlinkWithUnlink(workspaceNodeModules, `${packagePath}/node_modules`);
426-
resolvedPackagePath = packagePath;
427-
}
423+
yield mkdirp(packagePath);
424+
yield symlinkWithUnlink(workspaceNodeModules, `${packagePath}/node_modules`);
428425
if (!isExecroot) {
429426
const runfilesPackagePath = `${startCwd}/${packagePath}`;
430427
if (yield exists(runfilesPackagePath)) {
431-
if (resolvedPackagePath) {
432-
yield symlinkWithUnlink(`${resolvedPackagePath}/node_modules`, `${runfilesPackagePath}/node_modules`);
433-
}
434-
else {
435-
yield symlinkWithUnlink(workspaceNodeModules, `${runfilesPackagePath}/node_modules`);
436-
}
437-
resolvedPackagePath = runfilesPackagePath;
428+
yield symlinkWithUnlink(`${packagePath}/node_modules`, `${runfilesPackagePath}/node_modules`);
438429
}
439430
}
440431
const packagePathBin = `${bin}/${packagePath}`;
441-
if (resolvedPackagePath && (yield exists(packagePathBin))) {
442-
yield symlinkWithUnlink(`${resolvedPackagePath}/node_modules`, `${packagePathBin}/node_modules`);
432+
if (yield exists(packagePathBin)) {
433+
yield symlinkWithUnlink(`${packagePath}/node_modules`, `${packagePathBin}/node_modules`);
443434
}
444435
}
445436
}

internal/linker/link_node_modules.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -618,30 +618,30 @@ export async function main(args: string[], runfiles: Runfiles) {
618618
if (packagePath) {
619619
// sub-directory node_modules
620620
if (await exists(workspaceNodeModules)) {
621-
let resolvedPackagePath;
622-
if (await exists(packagePath)) {
623-
await symlinkWithUnlink(workspaceNodeModules, `${packagePath}/node_modules`);
624-
resolvedPackagePath = packagePath;
625-
}
621+
// in some cases packagePath may not exist in sandbox if there are no source deps
622+
// and only generated file deps. we create it so that we that we can link to
623+
// packagePath/node_modules since packagePathBin/node_modules is a symlink to
624+
// packagePath/node_modules and is unguarded in launcher.sh as we allow symlinks to fall
625+
// through to from output tree to source tree to prevent resolving the same npm package to
626+
// multiple locations on disk
627+
await mkdirp(packagePath);
628+
await symlinkWithUnlink(workspaceNodeModules, `${packagePath}/node_modules`);
626629
if (!isExecroot) {
627630
// Under runfiles, we symlink into the package in runfiles as well.
628631
// When inside the sandbox, the execroot location will not exist to symlink to.
629632
const runfilesPackagePath = `${startCwd}/${packagePath}`;
630633
if (await exists(runfilesPackagePath)) {
631-
if (resolvedPackagePath) {
632-
await symlinkWithUnlink(
633-
`${resolvedPackagePath}/node_modules`, `${runfilesPackagePath}/node_modules`);
634-
} else {
635-
await symlinkWithUnlink(workspaceNodeModules, `${runfilesPackagePath}/node_modules`);
636-
}
637-
resolvedPackagePath = runfilesPackagePath;
634+
await symlinkWithUnlink(
635+
`${packagePath}/node_modules`, `${runfilesPackagePath}/node_modules`);
638636
}
639637
}
640638
const packagePathBin = `${bin}/${packagePath}`;
641-
if (resolvedPackagePath && await exists(packagePathBin)) {
639+
if (await exists(packagePathBin)) {
642640
// if bin path exists, symlink bin/package/node_modules -> package/node_modules
643-
await symlinkWithUnlink(
644-
`${resolvedPackagePath}/node_modules`, `${packagePathBin}/node_modules`);
641+
// NB: this location is unguarded in launcher.sh to allow symlinks to fall-throught
642+
// package/node_modules to prevent resolving the same npm package to multiple locations on
643+
// disk
644+
await symlinkWithUnlink(`${packagePath}/node_modules`, `${packagePathBin}/node_modules`);
645645
}
646646
}
647647
} else {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
load("@build_bazel_rules_nodejs//:index.bzl", "generated_file_test")
2+
load("//packages/rollup:index.bzl", "rollup_bundle")
3+
load("//packages/typescript:index.bzl", "ts_project")
4+
5+
ts_project(
6+
name = "tsconfig",
7+
srcs = glob(["*.ts"]),
8+
deps = ["@rollup_test_multi_linker_deps//@types"],
9+
)
10+
11+
# Intentionally test with only generated files by transpiling with ts_project
12+
# to test multi-linker build action with no source inputs.
13+
# NB: rollup.config.js is used to generated "_{name}.rollup_config.js" in output
14+
# tree. Only the generated config is a input to the rollup build action.
15+
rollup_bundle(
16+
name = "bundle",
17+
config_file = "rollup.config.js",
18+
entry_point = "main.js",
19+
deps = [
20+
"tsconfig",
21+
"@rollup_test_multi_linker_deps//@rollup/plugin-commonjs",
22+
"@rollup_test_multi_linker_deps//@rollup/plugin-node-resolve",
23+
],
24+
)
25+
26+
generated_file_test(
27+
name = "test",
28+
src = "golden.js_",
29+
generated = "bundle.js",
30+
# fails in some non-trivial way on Windows; not particularly important to Windows coverage here
31+
tags = ["fix-windows"],
32+
)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function createCommonjsModule(fn, basedir, module) {
2+
return module = {
3+
path: basedir,
4+
exports: {},
5+
require: function (path, base) {
6+
return commonjsRequire(path, (base === undefined || base === null) ? module.path : base);
7+
}
8+
}, fn(module, module.exports), module.exports;
9+
}
10+
11+
function commonjsRequire () {
12+
throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs');
13+
}
14+
15+
var lib = createCommonjsModule(function (module, exports) {
16+
exports.__esModule = true;
17+
exports.key = 'rollup';
18+
});
19+
20+
var key = lib.key;
21+
console.log(key);
22+
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYnVuZGxlLmpzIiwic291cmNlcyI6WyJsaWIuanMiLCJtYWluLmpzIl0sInNvdXJjZXNDb250ZW50IjpbIlwidXNlIHN0cmljdFwiO1xuZXhwb3J0cy5fX2VzTW9kdWxlID0gdHJ1ZTtcbmV4cG9ydHMua2V5ID0gJ3JvbGx1cCc7XG4iLCJ2YXIga2V5ID0gcmVxdWlyZSgnLi9saWInKS5rZXk7XG5jb25zb2xlLmxvZyhrZXkpO1xuIl0sIm5hbWVzIjpbInJlcXVpcmUkJDAiXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7OztBQUNBLGtCQUFrQixHQUFHLElBQUksQ0FBQztBQUMxQixXQUFXLEdBQUcsUUFBUTs7O0FDRnRCLElBQUksR0FBRyxHQUFHQSxHQUFnQixDQUFDLEdBQUcsQ0FBQztBQUMvQixPQUFPLENBQUMsR0FBRyxDQUFDLEdBQUcsQ0FBQyJ9
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const key: string = 'rollup';
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const key = require('./lib').key;
2+
console.log(key);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"devDependencies": {
3+
"@types/node": "14.14.26",
4+
"@rollup/plugin-commonjs": "14.0.0",
5+
"@rollup/plugin-node-resolve": "8.4.0"
6+
}
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const {nodeResolve} = require('@rollup/plugin-node-resolve')
2+
const commonjs = require('@rollup/plugin-commonjs')
3+
4+
module.exports = {
5+
plugins: [
6+
nodeResolve(),
7+
commonjs(),
8+
],
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"types": ["node"]
4+
}
5+
}

0 commit comments

Comments
 (0)