oven-sh/bun · error · AggregateError
Failed bundling builtin function
Error message
Failed bundling builtin function ${fn.name} from ${basename}.ts What it means
AggregateError thrown after Bun.build reports failure for the temporary file that wraps a single builtin function. The aggregated errors are the build logs (parse/resolve/minify diagnostics) for the generated tmp entry, and the message identifies which function and which builtin module file failed to bundle.
Solutions
- Inspect the AggregateError's errors array — the Bun.build logs pinpoint the syntax or resolve error in the generated source
- Fix the offending builtin function's source or its $directives, then rerun codegen
- If the failure is a bundler bug, minimize the generated tmp file and reproduce with Bun.build directly
Defensive patterns
Strategy: try-catch
When it happens
Trigger: Thrown at src/codegen/bundle-functions.ts:314 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of oven-sh/bun@8c5296ac45 (2026-08-16).
Data as JSON: /api/errors/300123debf779288.
Report an issue: GitHub.
Appendix: source
Thrown at src/codegen/bundle-functions.ts:314
($);
$$capture_start$$(${fn.async ? "async " : ""}${
useThis
? `function(${fn.params.join(",")})`
: `${fn.params.length === 1 ? fn.params[0] : `(${fn.params.join(",")})`}=>`
} {${fn.source}}).$$capture_end$$;
`,
);
await Bun.sleep(1);
const build = await Bun.build({
entrypoints: [tmpFile],
define,
target: "bun",
minify: { syntax: true, whitespace: false, keepNames: true },
});
// TODO: Wait a few versions before removing this
if (!build.success) {
throw new AggregateError(build.logs, "Failed bundling builtin function " + fn.name + " from " + basename + ".ts");
}
if (build.outputs.length !== 1) {
throw new Error("expected one output");
}
let output = (await build.outputs[0].text()).replaceAll("// @bun\n", "");
let usesDebug = output.includes("$debug_log");
let usesAssert = output.includes("$assert");
const captured = output.match(/\$\$capture_start\$\$([\s\S]+)\.\$\$capture_end\$\$/)![1];
const finalReplacement =
captured
.replace(
/function\s*\(.*?\)\s*{/,
'$&"use strict";' +
(usesDebug ? createLogClientJS("BUILTINS", fn.name) : "") +
(usesAssert ? createAssertClientJS(fn.name) : ""),
)
.replace(/^\((async )?function\(/, "($1function (")
.replace(/__intrinsic__/g, "@")View on GitHub (pinned to 8c5296ac45)