Skip to content

Commit

Permalink
feat(builtin): linker should resolve workspace-absolute paths
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Sep 25, 2019
1 parent 1524a5a commit 307a796
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 4 deletions.
6 changes: 5 additions & 1 deletion internal/linker/index.js

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion internal/linker/link_node_modules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ def register_node_modules_linker(ctx, args, inputs):
inputs: inputs being passed to the program; a linker input will be appended
"""

mappings = {}
mappings = {
# We always map the workspace to itself to support absolute require like
# import from 'my_wksp/path/to/file'
ctx.workspace_name: ctx.workspace_name,
}
node_modules_root = ""

# Look through data/deps attributes to find...
Expand Down
4 changes: 4 additions & 0 deletions internal/linker/link_node_modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ export class Runfiles {
if (!this.manifest) return undefined;

for (const [k, v] of this.manifest) {
// Account for Bazel --legacy_external_runfiles
// which pollutes the workspace with 'my_wksp/external/...'
if (k.startsWith(`${dir}/external`)) continue;

// Entry looks like
// k: npm/node_modules/semver/LICENSE
// v: /path/to/external/npm/node_modules/semver/LICENSE
Expand Down
1 change: 1 addition & 0 deletions internal/linker/test/integration/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ sh_binary(
name = "run_program",
srcs = ["run_program_with_node.sh"],
data = [
"absolute_import/index.js",
":program.js",
"//internal/linker:index.js",
"//internal/linker/test/integration/static_linked_pkg",
Expand Down
5 changes: 5 additions & 0 deletions internal/linker/test/integration/absolute_import/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function addC(str) {
return `${str}_c`;
}

exports.addC = addC;
2 changes: 1 addition & 1 deletion internal/linker/test/integration/golden.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.3_b_a
1.2.3_c_b_a
5 changes: 4 additions & 1 deletion internal/linker/test/integration/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ const a = require('static_linked');
// First-party package from ./dynamic_linked_pkg
// it should get resolved from the execroot
const b = require('dynamic_linked');
// We've always supported `require('my_workspace')` for absolute imports like Google does it
const c = require('build_bazel_rules_nodejs/internal/linker/test/integration/absolute_import');

// Third-party package installed in the root node_modules
const semver = require('semver');

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

0 comments on commit 307a796

Please sign in to comment.