aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Vlad Sirenko <sirenkovladd@gmail.com> 2023-08-01 05:41:41 +0300
committerGravatar GitHub <noreply@github.com> 2023-07-31 19:41:41 -0700
commit7a8f57c4e527f7fc84b85a4757723bb97540ebc3 (patch)
treefdbc43040069373b3af33770e00e47ad53b35dc4
parent8589ba2f1712d68199fbef14f5a4ba9005df3065 (diff)
downloadbun-7a8f57c4e527f7fc84b85a4757723bb97540ebc3.tar.gz
bun-7a8f57c4e527f7fc84b85a4757723bb97540ebc3.tar.zst
bun-7a8f57c4e527f7fc84b85a4757723bb97540ebc3.zip
throw error if node module does not exist (#3913)
-rw-r--r--src/linker.zig117
-rw-r--r--src/resolver/resolver.zig6
-rw-r--r--test/js/node/missing-module.test.js7
3 files changed, 78 insertions, 52 deletions
diff --git a/src/linker.zig b/src/linker.zig
index 744c5eabc..900f02e88 100644
--- a/src/linker.zig
+++ b/src/linker.zig
@@ -206,6 +206,62 @@ pub const Linker = struct {
return linkAllowImportingFromBundle(linker, file_path, result, origin, import_path_format, ignore_runtime, true, is_bun);
}
+ fn whenModuleNotFound(
+ linker: *ThisLinker,
+ import_record: *ImportRecord,
+ result: *_bundler.ParseResult,
+ comptime is_bun: bool,
+ ) !bool {
+ if (import_record.handles_import_errors) {
+ import_record.path.is_disabled = true;
+ return false;
+ }
+
+ if (comptime is_bun) {
+ // make these happen at runtime
+ if (import_record.kind == .require or import_record.kind == .require_resolve) {
+ return false;
+ }
+ }
+
+ if (import_record.path.text.len > 0 and Resolver.isPackagePath(import_record.path.text)) {
+ if (linker.options.target.isWebLike() and Options.ExternalModules.isNodeBuiltin(import_record.path.text)) {
+ try linker.log.addResolveError(
+ &result.source,
+ import_record.range,
+ linker.allocator,
+ "Could not resolve: \"{s}\". Try setting --target=\"node\"",
+ .{import_record.path.text},
+ import_record.kind,
+ error.ModuleNotFound,
+ );
+ } else {
+ try linker.log.addResolveError(
+ &result.source,
+ import_record.range,
+ linker.allocator,
+ "Could not resolve: \"{s}\". Maybe you need to \"bun install\"?",
+ .{import_record.path.text},
+ import_record.kind,
+ error.ModuleNotFound,
+ );
+ }
+ } else {
+ try linker.log.addResolveError(
+ &result.source,
+ import_record.range,
+ linker.allocator,
+ "Could not resolve: \"{s}\"",
+ .{
+ import_record.path.text,
+ },
+ import_record.kind,
+ error.ModuleNotFound,
+ );
+ }
+ return true;
+ }
+
pub fn linkAllowImportingFromBundle(
linker: *ThisLinker,
file_path: Fs.Path,
@@ -280,6 +336,14 @@ pub const Linker = struct {
continue;
}
}
+ if (strings.startsWith(import_record.path.text, "node:")) {
+ // if a module is not found here, it is not found at all
+ // so we can just disable it
+ had_resolve_errors = try whenModuleNotFound(linker, import_record, result, is_bun);
+
+ if (had_resolve_errors) return error.ResolveMessage;
+ continue;
+ }
if (JSC.DisabledModule.has(import_record.path.text)) {
import_record.path.is_disabled = true;
@@ -719,58 +783,7 @@ pub const Linker = struct {
continue;
},
error.ModuleNotFound => {
- if (import_record.handles_import_errors) {
- import_record.path.is_disabled = true;
- continue;
- }
-
- if (comptime is_bun) {
- // make these happen at runtime
- if (import_record.kind == .require or import_record.kind == .require_resolve) {
- continue;
- }
- }
-
- had_resolve_errors = true;
-
- if (import_record.path.text.len > 0 and Resolver.isPackagePath(import_record.path.text)) {
- if (linker.options.target.isWebLike() and Options.ExternalModules.isNodeBuiltin(import_record.path.text)) {
- try linker.log.addResolveError(
- &result.source,
- import_record.range,
- linker.allocator,
- "Could not resolve: \"{s}\". Try setting --target=\"node\"",
- .{import_record.path.text},
- import_record.kind,
- err,
- );
- continue;
- } else {
- try linker.log.addResolveError(
- &result.source,
- import_record.range,
- linker.allocator,
- "Could not resolve: \"{s}\". Maybe you need to \"bun install\"?",
- .{import_record.path.text},
- import_record.kind,
- err,
- );
- continue;
- }
- } else {
- try linker.log.addResolveError(
- &result.source,
- import_record.range,
- linker.allocator,
- "Could not resolve: \"{s}\"",
- .{
- import_record.path.text,
- },
- import_record.kind,
- err,
- );
- continue;
- }
+ had_resolve_errors = try whenModuleNotFound(linker, import_record, result, is_bun);
},
else => {
had_resolve_errors = true;
diff --git a/src/resolver/resolver.zig b/src/resolver/resolver.zig
index 49ce9ec11..8735981fc 100644
--- a/src/resolver/resolver.zig
+++ b/src/resolver/resolver.zig
@@ -1262,6 +1262,12 @@ pub const Resolver = struct {
const had_node_prefix = strings.hasPrefixComptime(import_path, "node:");
const import_path_without_node_prefix = if (had_node_prefix) import_path["node:".len..] else import_path;
+ if (had_node_prefix) {
+ // because all node modules are already checked in ../linker.zig (JSC.HardcodedModule.Aliases.get) if module is not found here, it is not found at all
+ // so we can just return not_found
+ return .{ .not_found = {} };
+ }
+
if (NodeFallbackModules.Map.get(import_path_without_node_prefix)) |*fallback_module| {
result.path_pair.primary = fallback_module.path;
result.module_type = .cjs;
diff --git a/test/js/node/missing-module.test.js b/test/js/node/missing-module.test.js
new file mode 100644
index 000000000..fd772441a
--- /dev/null
+++ b/test/js/node/missing-module.test.js
@@ -0,0 +1,7 @@
+import { expect, test } from "bun:test";
+
+test("not implemented yet module masquerades as undefined and throws an error", () => {
+ const missingModule = "node:missing" + "";
+ expect(() => require(missingModule)).toThrow(/^Cannot find package "node:missing" from "/);
+ expect(() => import(missingModule)).toThrow(/^Cannot find package "node:missing" from "/);
+});