Skip to content

Commit

Permalink
fix(builtin): fix linker issue when running test with "local" tag on …
Browse files Browse the repository at this point in the history
…osx & linux (#1835)
  • Loading branch information
gregmagolan authored Apr 16, 2020
1 parent 0d62b89 commit 98d3321
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 2 deletions.
4 changes: 4 additions & 0 deletions internal/linker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ function resolveRoot(root, startCwd, isExecroot, runfiles) {
return fromManifest;
}
else {
const maybe = path.resolve(`${symlinkRoot}/external/${root}`);
if (fs.existsSync(maybe)) {
return maybe;
}
return path.resolve(`${startCwd}/../${root}`);
}
});
Expand Down
13 changes: 11 additions & 2 deletions internal/linker/link_node_modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,17 @@ async function resolveRoot(
if (fromManifest) {
return fromManifest;
} else {
// Under runfiles, the root will be one folder up from the startCwd `runfiles/my_wksp`.
// This is true whether legacy external runfiles are on or off.
const maybe = path.resolve(`${symlinkRoot}/external/${root}`);
if (fs.existsSync(maybe)) {
// Under runfiles, when not in the sandbox we must symlink node_modules down at the execroot
// `execroot/my_wksp/external/npm/node_modules` since `runfiles/npm/node_modules` will be a
// directory and not a symlink back to the root node_modules where we expect
// to resolve from. This case is tested in internal/linker/test/local.
return maybe;
}
// However, when in the sandbox, `execroot/my_wksp/external/npm/node_modules` does not exist,
// so we must symlink into `runfiles/npm/node_modules`. This directory exists whether legacy
// external runfiles are on or off.
return path.resolve(`${startCwd}/../${root}`)
}
}
Expand Down
9 changes: 9 additions & 0 deletions internal/linker/test/local/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@npm_bazel_jasmine//:index.from_src.bzl", "jasmine_node_test")

jasmine_node_test(
name = "test",
srcs = ["test.js"],
tags = ["local"],
templated_args = ["--nobazel_patch_module_resolver"],
deps = ["//internal/linker/test/local/fit"],
)
29 changes: 29 additions & 0 deletions internal/linker/test/local/fit/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
load("@build_bazel_rules_nodejs//:index.bzl", "pkg_npm")
load("@npm//typescript:index.bzl", "tsc")

tsc(
name = "fit_lib",
outs = [
"main.d.ts",
"main.js",
],
args = [
"-p",
"$(execpath tsconfig.json)",
"--outDir",
# $(RULEDIR) is a shorthand for the dist/bin directory where Bazel requires we write outputs
"$(RULEDIR)",
],
data = [
"main.ts",
"tsconfig.json",
],
)

pkg_npm(
name = "fit",
package_name = "fit",
srcs = ["package.json"],
visibility = ["//internal/linker/test/local:__pkg__"],
deps = [":fit_lib"],
)
1 change: 1 addition & 0 deletions internal/linker/test/local/fit/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const fit: string = 'fit';
5 changes: 5 additions & 0 deletions internal/linker/test/local/fit/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "fit",
"main": "main.js",
"typings": "main.d.ts"
}
5 changes: 5 additions & 0 deletions internal/linker/test/local/fit/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"declaration": true
}
}
6 changes: 6 additions & 0 deletions internal/linker/test/local/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
describe('linker', () => {
it('should work when job run with "local" tag', () => {
const fit = require('fit');
expect(fit.fit).toBe('fit');
});
});

0 comments on commit 98d3321

Please sign in to comment.