Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,18 @@ pub const ExportsKind = enum {
pub fn jsonStringify(self: @This(), writer: anytype) !void {
return try writer.write(@tagName(self));
}

pub fn toModuleType(self: @This()) bun.options.ModuleType {
return switch (self) {
.none => .unknown,
.cjs => .cjs,

.esm_with_dynamic_fallback,
.esm_with_dynamic_fallback_from_cjs,
.esm,
=> .esm,
};
}
Comment on lines +349 to +359

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

Deriving module type from export kind can misclassify ESM modules with no exports.

Mapping .none -> .unknown risks under-detecting ESM “importer” files that have only imports (or just import.meta) and no exports. In such cases input_module_type would be .unknown, and the printer would omit the , 1 flag to __toESM, breaking CJS→ESM interop for those importers.

  • Suggest: derive module type from a stronger signal (e.g., “is ESM syntax present” / module kind) rather than only exports. If Linker/AST already tracks an “is_esm_module” or similar, prefer that. Otherwise, augment the derivation to consider presence of import/export syntax or parser/module mode.

🏁 Script executed:

#!/bin/bash
# Find where ExportsKind.none is assigned and how ESM-ness is determined
rg -nP 'exports_kind\s*=\s*\.none|ExportsKind\.none' -g 'src/**/*.zig' -C3

# Locate any existing "is ESM" signal you could use instead of exports-only
rg -nP '\bis_esm\b|\bmodule_kind\b|\bmodule_type\b' -g 'src/**/*.zig' -C3

Length of output: 64485



🏁 Script executed:

#!/bin/bash
# Find ExportsKind enum definition and its variants
rg -nP 'pub\s+(const\s+)?ExportsKind\s*=|enum.*ExportsKind|ExportsKind\s*\{' -g 'src/**/*.zig' -A10

# Find the toModuleType method implementation
rg -nP 'fn\s+toModuleType|pub\s+fn\s+toModuleType' -g 'src/**/*.zig' -A8

# Check Parser structure for has_es_module_syntax usage
rg -nP '\bhas_es_module_syntax\b' -g 'src/**/*.zig' -C2

Length of output: 5379


Output:

src/bundler/LinkerContext.zig-1327-            .input_module_type = ast.exports_kind.toModuleType(),
src/bundler/LinkerContext.zig-1328-            .module_type = c.options.output_format,
src/bundler/LinkerContext.zig-1329-            .print_dce_annotations = c.options.emit_dce_annotations,
src/ast/P.zig-147-    esm_export_keyword: logger.Range = logger.Range.None,
src/ast/P.zig-148-    esm_import_keyword: logger.Range = logger.Range.None,
src/ast/P.zig-149-    latest_return_had_semicolon: bool = false,
src/ast/P.zig-1911-        // if we had an import statement, set a flag
src/ast/P.zig-1911-        if (p.esm_import_keyword.isEmpty()) {
src/ast/P.zig-1913-            p.esm_import_keyword = p.tokens.items[p.token_index].range;
src/ast/P.zig-1917-            p.esm_import_keyword = p.tokens.items[p.token_index].range;
src/ast/P.zig-1919-            if (!p.esm_export_keyword.isEmpty())

🏁 Script executed:

#!/bin/bash
# Where is toModuleType() actually called?
rg -nP '\.toModuleType\(\)' -g 'src/**/*.zig' -B2 -A2

# Check if esm_import_keyword is accessible in Ast struct
rg -nP 'esm_import_keyword|esm_export_keyword' -g 'src/**/*.zig' -C1

Length of output: 6299


🏁 Script executed:

#!/bin/bash
# Where is toModuleType() actually called?
rg -nP '\.toModuleType\(\)' -g 'src/**/*.zig' -B2 -A2

# Check if esm_import_keyword is accessible in Ast struct
rg -nP 'esm_import_keyword|esm_export_keyword' -g 'src/**/*.zig' -C1

Length of output: 6299


🏁 Script executed:

#!/bin/bash
# Find Ast struct definition (not just Ast.zig file)
rg -nP 'pub\s+struct\s+Ast\s*\{|pub\s+const\s+Ast\s*=\s*struct' -g 'src/**/*.zig' -A30

# Check if has_es_module_syntax is stored in Ast
rg -nP '\bhas_es_module_syntax\b' -g 'src/ast/Ast.zig' -C3

Length of output: 0


🏁 Script executed:

#!/bin/bash
# Search for js_ast.Ast definition
fd -e zig -x grep -l "pub.*struct.*Ast" {} \; | head -20

# Look at what's returned from the parser
rg -nP 'pub\s+fn.*parse.*Ast|fn.*return.*Ast' -g 'src/ast/Parser.zig' -A5

# Find where import_keyword and export_keyword are used in Ast context
rg -nP '\.import_keyword|\.export_keyword' -g 'src/**/*.zig' -B2 -A1

Length of output: 1087


🏁 Script executed:

#!/bin/bash
# Find the function/code around P.zig:6519 to see what struct it returns
sed -n '6500,6550p' src/ast/P.zig

# Find Ast struct definition - look for "pub const Ast" or similar in ast.zig
sed -n '1,100p' src/ast/Ast.zig | grep -A 80 'pub const Ast'

Length of output: 2907


🏁 Script executed:

#!/bin/bash
# Look at full Ast struct definition
rg -nP 'pub const Ast = struct' -g 'src/ast/Ast.zig' -A 50

# Check all exports_kind assignments in Parser.zig
rg -nP 'exports_kind\s*=' -g 'src/ast/Parser.zig' -B2 -A1

Length of output: 3685


🏁 Script executed:

#!/bin/bash
# Check where p.options.module_type gets set and if it's ever non-.unknown
rg -nP 'opts\.module_type\s*=' -g 'src/**/*.zig' -B1 -A1 | head -40

# Verify: what does has_es_module_syntax include?
sed -n '2007,2015p' src/ast/P.zig

Length of output: 2027


Fix the exports_kind derivation to detect ESM-only importers.

The bug is real: files with only ESM imports (no exports, no top-level await) have exports_kind=.none, which maps to module_type=.unknown via toModuleType(). This occurs when module_type is preset and the fix path at Parser.zig:1080 is skipped.

The fix should use the existing has_es_module_syntax signal (which correctly includes import keyword detection per P.zig:2014) instead of checking only exports and await. Change Parser.zig line 1017–1018 from:

} else if (p.esm_export_keyword.len > 0 or p.top_level_await_keyword.len > 0) {
    exports_kind = .esm;

to:

} else if (p.has_es_module_syntax) {
    exports_kind = .esm;

This ensures ESM-only importers are classified as .esm rather than .unknown, preserving the CJS→ESM interop flag.

🤖 Prompt for AI Agents
In Parser.zig around lines 1017–1018 (and note the fix path skipped at ~1080),
the exports_kind derivation only checks esm_export_keyword or
top_level_await_keyword which misses files that only have ESM imports; change
the conditional to check p.has_es_module_syntax instead so exports_kind is set
to .esm for import-only ESM files, preserving correct module_type mapping and
CJS→ESM interop behavior.

};

pub const DeclaredSymbol = struct {
Expand Down
2 changes: 1 addition & 1 deletion src/ast/maybe.zig
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ pub fn AstMaybe(
}

// Inline import.meta properties for Bake
if (p.options.framework != null) {
if (p.options.framework != null or (p.options.bundle and p.options.output_format == .cjs)) {
if (strings.eqlComptime(name, "dir") or strings.eqlComptime(name, "dirname")) {
// Inline import.meta.dir
return p.newExpr(E.String.init(p.source.path.name.dir), name_loc);
Expand Down
1 change: 1 addition & 0 deletions src/bundler/LinkerContext.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,7 @@ pub const LinkerContext = struct {

.minify_whitespace = c.options.minify_whitespace,
.minify_syntax = c.options.minify_syntax,
.input_module_type = ast.exports_kind.toModuleType(),
.module_type = c.options.output_format,
.print_dce_annotations = c.options.emit_dce_annotations,
.has_run_symbol_renamer = true,
Expand Down
3 changes: 2 additions & 1 deletion src/js_printer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ pub const Options = struct {

require_or_import_meta_for_source_callback: RequireOrImportMeta.Callback = .{},

input_module_type: options.ModuleType = .unknown,
module_type: options.Format = .esm,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

// /// Used for cross-module inlining of import items when bundling
Expand Down Expand Up @@ -1711,7 +1712,7 @@ fn NewPrinter(
}

if (wrap_with_to_esm) {
if (module_type.isESM()) {
if (p.options.input_module_type == .esm) {
p.print(",");
p.printSpace();
p.print("1");
Expand Down
Loading