Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
14 changes: 7 additions & 7 deletions src/runtime/bake/hmr-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,14 @@
const mod = loadModuleSync(id, true, this);
return mod.esm ? (mod.cjs ??= toCommonJS(mod.exports)) : mod.cjs.exports;
} catch (e: any) {
// The user-facing error is a new, plain Error so that the `require()`
// calls it propagates out of (and the `failure` a module records, which
// is rethrown on every later load) pass it along unchanged, like any
// other error thrown while evaluating a module.
Comment thread
robobun marked this conversation as resolved.
Resolved
if (e instanceof AsyncImportError) {
e.message = `Cannot require "${id}" because "${e.asyncId}" uses top-level await, but 'require' is a synchronous operation.`;
throw new Error(
`Cannot require "${id}" because "${e.asyncId}" uses top-level await, but 'require' is a synchronous operation.`,
);

Check warning on line 137 in src/runtime/bake/hmr-module.ts

View check run for this annotation

Claude / Claude Code Review

AsyncImportError's name override is now dead code

The `Object.defineProperty(this, "name", { value: "Error" })` override in `AsyncImportError`'s constructor (hmr-module.ts:864) is now dead — it existed so the instance would display as `Error: ...` when it reached user code, and after this change `require()` throws a fresh `new Error(...)` so the `AsyncImportError` instance never escapes the loader. This is the same class of cleanup as the `declare global` block already removed in fdb315da; per REVIEW.md ("delete dead code in the same PR that ma
Comment thread
claude[bot] marked this conversation as resolved.
Resolved
Comment thread
robobun marked this conversation as resolved.
Resolved
}
throw e;
}
Expand Down Expand Up @@ -938,12 +944,6 @@

function implicitAcceptFunction() {}

declare global {
interface Error {
asyncId?: string;
}
}

// bun:bake/server, bun:bake/client, and bun:wrap are
// provided by this file instead of the bundler
registerSynthetic("bun:wrap", {
Expand Down
76 changes: 76 additions & 0 deletions test/bake/dev/esm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,82 @@ devTest("cannot require a module with top level await", {
});
},
});
devTest("the error for requiring a module with top level await names the require call that failed", {
// `require()` used to produce this message by rewriting, in place, the error
// the loader threw. Every enclosing `require()` the error then propagated
// through renamed it again, including the error a failed module records and
// rethrows on every later load of it.
framework: minimalFramework,
files: {
"tla.ts": `
await 1;
export const value = "tla";
`,
"imports-tla.ts": `
import "./tla";
export const value = "unreachable";
`,
"outer.ts": `
require("./inner");
export const value = "unreachable";
`,
"inner.ts": `
require("./tla");
export const value = "unreachable";
`,
"requires-tla.ts": `
require("./tla");
export const value = "unreachable";
`,
"imports-requires-tla.ts": `
import "./requires-tla";
export const value = "unreachable";
`,
"routes/index.ts": `
async function attempt(load) {
try {
await load();
return "loaded";
} catch (e) {
return e.message;
}
}
export default async function () {
return Response.json({
requireTla: await attempt(() => require("../tla")),
requireImportsTla: await attempt(() => require("../imports-tla")),
requireOuter: await attempt(() => require("../outer")),
importRequiresTla: await attempt(() => import("../requires-tla")),
requireRequiresTla: await attempt(() => require("../requires-tla")),
requireImportsRequiresTla: await attempt(() => require("../imports-requires-tla")),
importRequiresTlaAgain: await attempt(() => import("../requires-tla")),
});
}
`,
},
async test(dev) {
const cannotRequire = (id: string, asyncId: string) =>
`Cannot require "${id}" because "${asyncId}" uses top-level await, but 'require' is a synchronous operation.`;
expect(await dev.fetch("/").json()).toEqual({
// The required module, or one of its static imports, has top-level await.
requireTla: cannotRequire("tla.ts", "tla.ts"),
requireImportsTla: cannotRequire("imports-tla.ts", "tla.ts"),
// outer.ts and inner.ts can be required. The call that failed is the
// require("./tla") in inner.ts, and its error propagates out of the two
// enclosing require() calls unchanged, like any other error thrown while
// evaluating a required module. (This used to name outer.ts.)
requireOuter: cannotRequire("tla.ts", "tla.ts"),
// The import() records the error on requires-tla.ts. The two require()
// calls rethrow that recorded error (directly, then through a static
// importer); they used to rename it to requires-tla.ts and then to
// imports-requires-tla.ts, which is what the second import() reported.
importRequiresTla: cannotRequire("tla.ts", "tla.ts"),
requireRequiresTla: cannotRequire("tla.ts", "tla.ts"),
requireImportsRequiresTla: cannotRequire("tla.ts", "tla.ts"),
importRequiresTlaAgain: cannotRequire("tla.ts", "tla.ts"),
});
},
});
devTest("function that is assigned to should become a live binding", {
files: {
"index.html": emptyHtmlFile({
Expand Down