Skip to content

Commit 98d3321

Browse files
authored
fix(builtin): fix linker issue when running test with "local" tag on osx & linux (#1835)
1 parent 0d62b89 commit 98d3321

8 files changed

Lines changed: 70 additions & 2 deletions

File tree

internal/linker/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ function resolveRoot(root, startCwd, isExecroot, runfiles) {
8989
return fromManifest;
9090
}
9191
else {
92+
const maybe = path.resolve(`${symlinkRoot}/external/${root}`);
93+
if (fs.existsSync(maybe)) {
94+
return maybe;
95+
}
9296
return path.resolve(`${startCwd}/../${root}`);
9397
}
9498
});

internal/linker/link_node_modules.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,17 @@ async function resolveRoot(
142142
if (fromManifest) {
143143
return fromManifest;
144144
} else {
145-
// Under runfiles, the root will be one folder up from the startCwd `runfiles/my_wksp`.
146-
// This is true whether legacy external runfiles are on or off.
145+
const maybe = path.resolve(`${symlinkRoot}/external/${root}`);
146+
if (fs.existsSync(maybe)) {
147+
// Under runfiles, when not in the sandbox we must symlink node_modules down at the execroot
148+
// `execroot/my_wksp/external/npm/node_modules` since `runfiles/npm/node_modules` will be a
149+
// directory and not a symlink back to the root node_modules where we expect
150+
// to resolve from. This case is tested in internal/linker/test/local.
151+
return maybe;
152+
}
153+
// However, when in the sandbox, `execroot/my_wksp/external/npm/node_modules` does not exist,
154+
// so we must symlink into `runfiles/npm/node_modules`. This directory exists whether legacy
155+
// external runfiles are on or off.
147156
return path.resolve(`${startCwd}/../${root}`)
148157
}
149158
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
load("@npm_bazel_jasmine//:index.from_src.bzl", "jasmine_node_test")
2+
3+
jasmine_node_test(
4+
name = "test",
5+
srcs = ["test.js"],
6+
tags = ["local"],
7+
templated_args = ["--nobazel_patch_module_resolver"],
8+
deps = ["//internal/linker/test/local/fit"],
9+
)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
load("@build_bazel_rules_nodejs//:index.bzl", "pkg_npm")
2+
load("@npm//typescript:index.bzl", "tsc")
3+
4+
tsc(
5+
name = "fit_lib",
6+
outs = [
7+
"main.d.ts",
8+
"main.js",
9+
],
10+
args = [
11+
"-p",
12+
"$(execpath tsconfig.json)",
13+
"--outDir",
14+
# $(RULEDIR) is a shorthand for the dist/bin directory where Bazel requires we write outputs
15+
"$(RULEDIR)",
16+
],
17+
data = [
18+
"main.ts",
19+
"tsconfig.json",
20+
],
21+
)
22+
23+
pkg_npm(
24+
name = "fit",
25+
package_name = "fit",
26+
srcs = ["package.json"],
27+
visibility = ["//internal/linker/test/local:__pkg__"],
28+
deps = [":fit_lib"],
29+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const fit: string = 'fit';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "fit",
3+
"main": "main.js",
4+
"typings": "main.d.ts"
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"declaration": true
4+
}
5+
}

internal/linker/test/local/test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
describe('linker', () => {
2+
it('should work when job run with "local" tag', () => {
3+
const fit = require('fit');
4+
expect(fit.fit).toBe('fit');
5+
});
6+
});

0 commit comments

Comments
 (0)