Skip to content

Commit

Permalink
fix: exports_directories_only causes node to resolve from runfiles/no…
Browse files Browse the repository at this point in the history
…de_modules (#3380)
  • Loading branch information
thesayyn authored Apr 1, 2022
1 parent 74f17dd commit 5bf3782
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 6 deletions.
2 changes: 1 addition & 1 deletion internal/node/launcher.sh
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ else
# Entry point is the user-supplied script
MAIN="${PWD}/"TEMPLATED_entry_point_execroot_path
# TODO: after we link-all-bins we should not need this extra lookup
if [[ ! -f "$MAIN" ]]; then
if [[ ! -e "$MAIN" ]]; then
if [ "$FROM_EXECROOT" = true ]; then
MAIN="$EXECROOT/"TEMPLATED_entry_point_execroot_path
else
Expand Down
19 changes: 14 additions & 5 deletions internal/node/node.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,21 @@ def _to_manifest_path(ctx, file):
return ctx.workspace_name + "/" + file.short_path

def _to_execroot_path(ctx, file):
# Check for special case of external/<npm>/node_modules or
# bazel-out/<platform>/bin/external/<npm>/node_modules.
# TODO: This assumes we are linking this to the root node_modules which is not always the case
# since the linker was updated to support linking to sub-directories since this special case was
# added
parts = file.path.split("/")
if parts[0] == "external":
if parts[2] == "node_modules":
# external/npm/node_modules -> node_modules/foo
# the linker will make sure we can resolve node_modules from npm
return "/".join(parts[2:])
if file.is_directory and not file.is_source:
# Strip the output root to handle the case of a TreeArtifact exported by js_library with
# exports directories only. Since a TreeArtifact is generated by an action, it resides
# inside bazel-out directory.
parts = parts[3:]
if len(parts) > 3 and parts[0] == "external" and parts[2] == "node_modules":
# Transform external/npm/node_modules/foo to node_modules/foo.
# The linker will make sure we can resolve node_modules from npm.
return "/".join(parts[2:])

return file.path

Expand Down
19 changes: 19 additions & 0 deletions internal/node/test/instanceof_test_execroot/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("@npm//pkg_with_bin:index.bzl", "instanceof_execroot_test")
load(
"@npm_directory_artifacts//pkg_with_bin:index.bzl",
instanceof_execroot_test_exports_directories = "instanceof_execroot_test",
)

instanceof_execroot_test(
name = "test",
data = [
"//internal/node/test/instanceof_test_execroot/lib",
],
)

instanceof_execroot_test_exports_directories(
name = "test_exports_directories_only",
data = [
"//internal/node/test/instanceof_test_execroot/lib",
],
)
Empty file.
16 changes: 16 additions & 0 deletions internal/node/test/instanceof_test_execroot/lib/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
load("//:index.bzl", "js_library")

package(default_visibility = ["//internal/node/test/instanceof_test_execroot:__subpackages__"])

js_library(
name = "lib",
# required by tools/npm_packages/pkg_with_bin as third-party dep
package_name = "instanceof_test_execroot_lib",
srcs = [
"index.js",
"package.json",
],
deps = [
"@npm//node_resolve_main",
],
)
20 changes: 20 additions & 0 deletions internal/node/test/instanceof_test_execroot/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// This file will fail when one dependency is resolved from two distinct node_modules
// mostly likely to be one inside runfiles and other from bazel-out
// See: https://github.com/bazelbuild/rules_nodejs/pull/3380 for more.
const assert = require("assert");

const resolved_by_lib = require.resolve("node_resolve_main");
const resolved_by_pkg_with_bin = globalThis["node_resolve_main_resolved_path_by_pkg_with_bin"]
assert.equal(
resolved_by_lib,
resolved_by_pkg_with_bin,
`
Expected to resolve package "node_resolve_main" from the same node_modules but
tools/npm_packages/pkg_with_bin resolved it from ${resolved_by_pkg_with_bin}
internal/node/test/instanceof_test_execroot/lib resolved it from ${resolved_by_lib}
See: https://github.com/bazelbuild/rules_nodejs/pull/3380 for context.
`
);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name": "instanceof_test_execroot_lib"}
4 changes: 4 additions & 0 deletions npm_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def npm_deps():
"//:tools/npm_packages/node_resolve_nested_main/nested/package.json",
"//:tools/npm_packages/testy/index.js",
"//:tools/npm_packages/testy/package.json",
"//:tools/npm_packages/pkg_with_bin/main.js",
"//:tools/npm_packages/pkg_with_bin/package.json",
],
environment = {
"SOME_USER_ENV": "yarn is great!",
Expand Down Expand Up @@ -107,6 +109,8 @@ js_library(
"//:tools/npm_packages/node_resolve_nested_main/nested/package.json",
"//:tools/npm_packages/testy/index.js",
"//:tools/npm_packages/testy/package.json",
"//:tools/npm_packages/pkg_with_bin/main.js",
"//:tools/npm_packages/pkg_with_bin/package.json",
],
environment = {
"SOME_USER_ENV": "yarn is great!",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"node_resolve_main": "file:./tools/npm_packages/node_resolve_main",
"node_resolve_main_2": "file:./tools/npm_packages/node_resolve_main_2",
"node_resolve_nested_main": "file:./tools/npm_packages/node_resolve_nested_main",
"pkg_with_bin": "file:./tools/npm_packages/pkg_with_bin",
"npm": "6.13.7",
"patch-package": "^6.2.2",
"protobufjs": "6.8.8",
Expand Down
3 changes: 3 additions & 0 deletions tools/npm_packages/pkg_with_bin/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
globalThis["node_resolve_main_resolved_path_by_pkg_with_bin"] = require.resolve("node_resolve_main");
require("instanceof_test_execroot_lib");
// See: internal/node/test/instanceof_test_execroot
10 changes: 10 additions & 0 deletions tools/npm_packages/pkg_with_bin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"version": "0.0.1",
"description": "See: internal/node/test/instanceof_test_execroot",
"bin": {
"instanceof_execroot": "main.js"
},
"peerDependencies": {
"node_resolve_main": "*"
}
}
3 changes: 3 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8740,6 +8740,9 @@ pkg-dir@^4.2.0:
dependencies:
find-up "^4.0.0"

"pkg_with_bin@file:./tools/npm_packages/pkg_with_bin":
version "0.0.1"

please-upgrade-node@^3.1.1:
version "3.2.0"
resolved "https://registry.yarnpkg.com/please-upgrade-node/-/please-upgrade-node-3.2.0.tgz#aeddd3f994c933e4ad98b99d9a556efa0e2fe942"
Expand Down

0 comments on commit 5bf3782

Please sign in to comment.