mirror of
https://github.com/oven-sh/bun
synced 2026-02-16 13:51:47 +00:00
## Summary - Defer resolution of dynamic `import()` of unknown `node:` modules (like `node:sqlite`) to runtime instead of failing at transpile time - Fix use-after-poison in `addResolveError` by always duping `line_text` from the source so Location data outlives the arena Fixes #25707 ## Root cause When a CJS file is `require()`d, Bun's linker eagerly resolves all import records, including dynamic `import()` expressions. For unknown `node:` prefixed modules, `whenModuleNotFound` was only deferring `.require` and `.require_resolve` to runtime — `.dynamic` imports fell through to the error path, causing the entire `require()` to fail. This broke Next.js builds with turbopack + `cacheComponents: true` + Better Auth, because Kysely's dialect detection code uses `import("node:sqlite")` inside a try/catch that gracefully handles the module not being available. Additionally, when the resolve error was generated, the `Location.line_text` was a slice into arena-allocated source contents. The arena is reset before `processFetchLog` processes the error, causing a use-after-poison when `Location.clone` tries to dupe the freed memory. ## Test plan - [x] New regression test in `test/regression/issue/25707.test.ts` with 3 cases: - CJS require of file with `import("node:sqlite")` inside try/catch (turbopack pattern) - CJS require of file with bare `import("node:sqlite")` (no try/catch) - Runtime error produces correct `ERR_UNKNOWN_BUILTIN_MODULE` code - [x] Test fails with `USE_SYSTEM_BUN=1` (system bun v1.3.9) - [x] Test passes with `bun bd test` - [x] No ASAN use-after-poison crash on debug build 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Bot <claude-bot@bun.sh> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>