Skip to content

Commit 307a796

Browse files
committed
feat(builtin): linker should resolve workspace-absolute paths
1 parent 1524a5a commit 307a796

7 files changed

Lines changed: 25 additions & 4 deletions

File tree

internal/linker/index.js

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/linker/link_node_modules.bzl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ def register_node_modules_linker(ctx, args, inputs):
3131
inputs: inputs being passed to the program; a linker input will be appended
3232
"""
3333

34-
mappings = {}
34+
mappings = {
35+
# We always map the workspace to itself to support absolute require like
36+
# import from 'my_wksp/path/to/file'
37+
ctx.workspace_name: ctx.workspace_name,
38+
}
3539
node_modules_root = ""
3640

3741
# Look through data/deps attributes to find...

internal/linker/link_node_modules.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ export class Runfiles {
121121
if (!this.manifest) return undefined;
122122

123123
for (const [k, v] of this.manifest) {
124+
// Account for Bazel --legacy_external_runfiles
125+
// which pollutes the workspace with 'my_wksp/external/...'
126+
if (k.startsWith(`${dir}/external`)) continue;
127+
124128
// Entry looks like
125129
// k: npm/node_modules/semver/LICENSE
126130
// v: /path/to/external/npm/node_modules/semver/LICENSE

internal/linker/test/integration/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ sh_binary(
1818
name = "run_program",
1919
srcs = ["run_program_with_node.sh"],
2020
data = [
21+
"absolute_import/index.js",
2122
":program.js",
2223
"//internal/linker:index.js",
2324
"//internal/linker/test/integration/static_linked_pkg",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function addC(str) {
2+
return `${str}_c`;
3+
}
4+
5+
exports.addC = addC;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.2.3_b_a
1+
1.2.3_c_b_a

internal/linker/test/integration/program.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ const a = require('static_linked');
44
// First-party package from ./dynamic_linked_pkg
55
// it should get resolved from the execroot
66
const b = require('dynamic_linked');
7+
// We've always supported `require('my_workspace')` for absolute imports like Google does it
8+
const c = require('build_bazel_rules_nodejs/internal/linker/test/integration/absolute_import');
9+
710
// Third-party package installed in the root node_modules
811
const semver = require('semver');
912

1013
// This output should match what's in the golden.txt file
11-
console.log(a.addA(b.addB(semver.clean(' =v1.2.3 '))));
14+
console.log(a.addA(b.addB(c.addC(semver.clean(' =v1.2.3 ')))));

0 commit comments

Comments
 (0)