throw error if node module does not exist (#3913)

This commit is contained in:
Vlad Sirenko
2023-08-01 05:41:41 +03:00
committed by GitHub
parent 8589ba2f17
commit 7a8f57c4e5
3 changed files with 78 additions and 52 deletions

View File

@@ -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;

View File

@@ -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;

View File

@@ -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 "/);
});