Skip to content

Top-level await in a wrapped ESM import cycle emits a non-async __esm wrapper containing await #4498

Description

@markedwards

Version: 0.28.1

Minimal repro (format: esm, platform: node, bundle: true):

// dep.mjs
export const tla = await Promise.resolve("x");
// a.mjs   (a <-> b form a cycle; a also depends on the TLA module)
import { b } from "./b.mjs";
import { tla } from "./dep.mjs";
export function a() { return b() + tla; }
// b.mjs
import { a } from "./a.mjs";
export function b() { return typeof a; }
// entry.mjs   (dynamic-import boundary forces __esm wrapping)
await import("./main.mjs");
// main.mjs
import { a } from "./a.mjs";
console.log(a());

Expected: either correct async-propagated output, or a build error.
Actual: build succeeds silently (0 warnings); node out.mjs throws SyntaxError: Unexpected reserved word. esbuild marks the init of the module that directly awaits TLA async, but fails to propagate async to the other members of the cyclic SCC, while still emitting await init_a() into their bodies:

var init_b = __esm({ "b.mjs"() { await init_a(); } });          // ← not async, but contains await
var init_a = __esm({ async "a.mjs"() { init_b(); await init_dep(); } });

The unbundled source runs fine under Node (valid ESM: the whole cycle is one async SCC). Two defects: (1) incomplete async-status propagation across import cycles; (2) invalid output emitted silently instead of erroring.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions