Skip to content

Commit c0fe512

Browse files
authored
fix(builtin): fix for issue 834 (#847)
Skip module root resolution in node resolver if main entry point. Also don't throw on resolve failure so that resolve does cascade down incase the module_root conflicts with the workspace name for fully resolved requires
1 parent 9a95730 commit c0fe512

File tree

5 files changed

+33
-8
lines changed

5 files changed

+33
-8
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ jobs:
160160
- run: bazel run //internal/node/test:has_deps_legacy
161161
- run: bazel run //internal/node/test:has_deps
162162
- run: bazel run //internal/node/test:has_deps_hybrid
163+
- run: bazel run //internal/node/test:module_name_test
163164
- run: bazel run //internal/e2e/fine_grained_no_bin:index
164165
- run: bazel run @fine_grained_deps_yarn//typescript/bin:tsc
165166
- run: bazel run @bazel_workspace_a//:bin

internal/node/node_loader.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -350,16 +350,23 @@ module.constructor._resolveFilename = function(request, parent, isMain, options)
350350
// Attempt to resolve to module root.
351351
// This should be the first attempted resolution because:
352352
// - it's fairly cheap to check (regex over a small array);
353-
// - will throw if something is wrong and not cascade down;
354353
// - it is be very common when there are a lot of packages built from source;
355-
const moduleRoot = resolveToModuleRoot(request);
356-
if (moduleRoot) {
357-
const moduleRootInRunfiles = resolveRunfiles(undefined, moduleRoot);
358-
const filename = module.constructor._findPath(moduleRootInRunfiles, []);
359-
if (!filename) {
360-
throw new Error(`No file ${request} found in module root ${moduleRoot}`);
354+
if (!isMain) {
355+
// Don't resolve to module root if this is the main entry point
356+
// as the main entry point will always be fully qualified with the
357+
// workspace name and full path.
358+
// See https://github.com/bazelbuild/rules_nodejs/issues/834
359+
const moduleRoot = resolveToModuleRoot(request);
360+
if (moduleRoot) {
361+
const moduleRootInRunfiles = resolveRunfiles(undefined, moduleRoot);
362+
const filename = module.constructor._findPath(moduleRootInRunfiles, []);
363+
if (filename) {
364+
return filename;
365+
} else {
366+
failedResolutions.push(
367+
`module root ${moduleRoot} - No file ${request} found in module root ${moduleRoot}`);
368+
}
361369
}
362-
return filename;
363370
}
364371

365372
// Built-in modules, relative, absolute imports and npm dependencies

internal/node/test/BUILD.bazel

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("//:defs.bzl", "nodejs_binary")
2+
load("//internal/js_library:js_library.bzl", "js_library")
23

34
# You can have a nodejs_binary with no node_modules attribute
45
# and no fine grained deps
@@ -49,3 +50,17 @@ nodejs_binary(
4950
name = "has_entry_file",
5051
entry_point = ":entry_file",
5152
)
53+
54+
# Coverage for issue https://github.com/bazelbuild/rules_nodejs/issues/834
55+
# where module_name is equal to the workspace naem
56+
js_library(
57+
name = "module_name_lib",
58+
srcs = ["module-name.js"],
59+
module_name = "build_bazel_rules_nodejs",
60+
)
61+
62+
nodejs_binary(
63+
name = "module_name_test",
64+
data = [":module_name_lib"],
65+
entry_point = ":module-name.js",
66+
)

internal/node/test/module-name.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('Awesome APP is launched!');

scripts/test_all.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ echo_and_run bazel run //internal/node/test:no_deps
4444
echo_and_run bazel run //internal/node/test:has_deps_legacy
4545
echo_and_run bazel run //internal/node/test:has_deps
4646
echo_and_run bazel run //internal/node/test:has_deps_hybrid
47+
echo_and_run bazel run //internal/node/test:module_name_test
4748
echo_and_run bazel run //internal/e2e/fine_grained_no_bin:index
4849
echo_and_run bazel run @fine_grained_deps_yarn//typescript/bin:tsc
4950

0 commit comments

Comments
 (0)