Compare commits

...

2 Commits

Author SHA1 Message Date
Jarred Sumner
ea660b338d Use a better error label 2023-09-16 22:52:49 -07:00
Jarred Sumner
8c8dd8b69c Make this error message clearer 2023-09-16 19:14:31 -07:00
4 changed files with 80 additions and 8 deletions

View File

@@ -511,8 +511,8 @@ pub const BundleV2 = struct {
source,
import_record.range,
this.graph.allocator,
"Could not resolve Node.js builtin: \"{s}\". To use Node.js builtins, set target to 'node' or 'bun'",
.{path_to_use},
"Browser build cannot {s} Node.js module: \"{s}\". To use Node.js builtins, set target to 'node' or 'bun'",
.{ import_record.kind.errorLabel(), path_to_use },
import_record.kind,
) catch unreachable;
} else {
@@ -1914,8 +1914,8 @@ pub const BundleV2 = struct {
source,
import_record.range,
this.graph.allocator,
"Could not resolve Node.js builtin: \"{s}\". To use Node.js builtins, set target to 'node' or 'bun'",
.{import_record.path.text},
"Browser build cannot {s} Node.js builtin: \"{s}\". To use Node.js builtins, set target to 'node' or 'bun'",
.{ import_record.kind.errorLabel(), import_record.path.text },
import_record.kind,
) catch @panic("unexpected log error");
} else {
@@ -9799,15 +9799,45 @@ const LinkerContext = struct {
// time, so we emit a debug message and rewrite the value to the literal
// "undefined" instead of emitting an error.
symbol.import_item_status = .missing;
c.log.addRangeWarningFmt(
if (c.resolver.opts.target == .browser and JSC.HardcodedModule.Aliases.has(next_source.path.pretty, .bun)) {
c.log.addRangeWarningFmtWithNote(
source,
r,
c.allocator,
"Browser polyfill for module \"{s}\" doesn't have a matching export named \"{s}\"",
.{
next_source.path.pretty,
named_import.alias.?,
},
"Bun's bundler defaults to browser builds instead of node or bun builds. If you want to use node or bun builds, you can set the target to \"node\" or \"bun\" in the bundler options.",
.{},
r,
) catch unreachable;
} else {
c.log.addRangeWarningFmt(
source,
r,
c.allocator,
"Import \"{s}\" will always be undefined because there is no matching export in \"{s}\"",
.{
named_import.alias.?,
next_source.path.pretty,
},
) catch unreachable;
}
} else if (c.resolver.opts.target == .browser and JSC.HardcodedModule.Aliases.has(next_source.path.pretty, .browser)) {
c.log.addRangeErrorFmtWithNote(
source,
r,
c.allocator,
"Import \"{s}\" will always be undefined because there is no matching export in \"{s}\"",
"Browser polyfill for module \"{s}\" doesn't have a matching export named \"{s}\"",
.{
named_import.alias.?,
next_source.path.pretty,
named_import.alias.?,
},
"Bun's bundler defaults to browser builds instead of node or bun builds. If you want to use node or bun builds, you can set the target to \"node\" or \"bun\" in the bundler options.",
.{},
r,
) catch unreachable;
} else {
c.log.addRangeErrorFmt(

View File

@@ -51,10 +51,27 @@ pub const ImportKind = enum(u8) {
break :brk labels;
};
pub const error_labels: Label = brk: {
var labels = Label.initFill("");
labels.set(ImportKind.entry_point, "entry point");
labels.set(ImportKind.stmt, "import");
labels.set(ImportKind.require, "require()");
labels.set(ImportKind.dynamic, "import()");
labels.set(ImportKind.require_resolve, "require.resolve");
labels.set(ImportKind.at, "@import");
labels.set(ImportKind.url, "url()");
labels.set(ImportKind.internal, "<bun internal>");
break :brk labels;
};
pub inline fn label(this: ImportKind) []const u8 {
return all_labels.get(this);
}
pub inline fn errorLabel(this: ImportKind) []const u8 {
return error_labels.get(this);
}
pub inline fn isCommonJS(this: ImportKind) bool {
return switch (this) {
.require, .require_resolve => true,

View File

@@ -1108,6 +1108,31 @@ pub const Log = struct {
});
}
pub fn addRangeErrorFmtWithNote(
log: *Log,
source: ?*const Source,
r: Range,
allocator: std.mem.Allocator,
comptime fmt: string,
args: anytype,
comptime note_fmt: string,
note_args: anytype,
note_range: Range,
) !void {
@setCold(true);
if (!Kind.shouldPrint(.err, log.level)) return;
log.errors += 1;
var notes = try allocator.alloc(Data, 1);
notes[0] = rangeData(source, note_range, allocPrint(allocator, note_fmt, note_args) catch unreachable);
try log.addMsg(.{
.kind = .err,
.data = rangeData(source, r, allocPrint(allocator, fmt, args) catch unreachable),
.notes = notes,
});
}
pub fn addWarning(log: *Log, source: ?*const Source, l: Loc, text: string) !void {
@setCold(true);
if (!Kind.shouldPrint(.warn, log.level)) return;

View File

@@ -87,7 +87,7 @@ pub const ExternalModules = struct {
};
pub fn isNodeBuiltin(str: string) bool {
return NodeBuiltinsMap.has(str);
return bun.JSC.HardcodedModule.Aliases.has(str, .node);
}
const default_wildcard_patterns = &[_]WildcardPattern{