Unverified Commit b8eba4f7 authored by Jarred Sumner's avatar Jarred Sumner Committed by GitHub

Mark bun builtin modules as external (fixes #75220) (#77616)

Fixes https://github.com/vercel/next.js/issues/75220

This marks Bun's builtin modules as external, following the same pattern
as for Node.

- `"bun:ffi"`
- `"bun:jsc"`
- `"bun:sqlite"`
- `"bun:test"`
- `"bun:wrap"`
- `"bun"`

I have not manually tested this change yet, nor attempted to write tests
for this. I did check for other places in the code that should be
updated and _maybe_
`externals/crates/next-custom-transforms/src/transforms/warn_for_edge_runtime.rs`
should as well, but I'll leave that up to you whether or not that makes
sense. Opening as a draft until this can be at least manually tested.

By the way, node's `sqlite` module is missing from `NODE_EXTERNALS`.
That seemed out of scope but worth mentioning.

Current error message:

![image](https://github.com/user-attachments/assets/0538fd35-a1ce-44fe-af7f-1db696cd3e7b)

---------
Co-authored-by: 's avatarAlistair Smith <hi@alistair.sh>
parent b07bf2a0
......@@ -188,6 +188,11 @@ export function makeExternalHandler({
return `commonjs ${request}`
}
// Handle Bun builtins as external modules
if (request === 'bun' || request.startsWith('bun:')) {
return `commonjs ${request}`
}
const notExternalModules =
/^(?:private-next-pages\/|next\/(?:dist\/pages\/|(?:app|cache|document|link|form|head|image|legacy\/image|constants|dynamic|script|navigation|headers|router|compat\/router|server)$)|string-hash|private-next-rsc-action-validate|private-next-rsc-action-client-wrapper|private-next-rsc-server-reference|private-next-rsc-cache-wrapper|private-next-rsc-track-dynamic-import$)/
if (notExternalModules.test(request)) {
......
......@@ -913,6 +913,15 @@ export default async function getBaseWebpackConfig(
const builtinModules = (require('module') as typeof import('module'))
.builtinModules
const bunExternals = [
'bun:ffi',
'bun:jsc',
'bun:sqlite',
'bun:test',
'bun:wrap',
'bun',
]
const shouldEnableSlowModuleDetection =
!!config.experimental.slowModuleDetection && dev
......@@ -986,6 +995,7 @@ export default async function getBaseWebpackConfig(
]
: [
...builtinModules,
...bunExternals,
({
context,
request,
......
......@@ -288,6 +288,10 @@ function isNodeJsModule(moduleName: string) {
)
}
function isBunModule(moduleName: string) {
return moduleName === 'bun' || moduleName.startsWith('bun:')
}
function isDynamicCodeEvaluationAllowed(
fileName: string,
middlewareConfig?: MiddlewareConfig,
......@@ -521,12 +525,13 @@ function getCodeAnalyzer(params: {
if (
!dev &&
isNodeJsModule(importedModule) &&
(isNodeJsModule(importedModule) || isBunModule(importedModule)) &&
!SUPPORTED_NATIVE_MODULES.includes(importedModule)
) {
const isBun = isBunModule(importedModule)
compilation.warnings.push(
buildWebpackError({
message: `A Node.js module is loaded ('${importedModule}' at line ${node.loc.start.line}) which is not supported in the Edge Runtime.
message: `A ${isBun ? 'Bun' : 'Node.js'} module is loaded ('${importedModule}' at line ${node.loc.start.line}) which is not supported in the Edge Runtime.
Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime`,
compilation,
parser,
......@@ -921,7 +926,7 @@ export async function handleWebpackExternalForEdgeRuntime({
if (
(contextInfo.issuerLayer === WEBPACK_LAYERS.middleware ||
contextInfo.issuerLayer === WEBPACK_LAYERS.apiEdge) &&
isNodeJsModule(request) &&
(isNodeJsModule(request) || isBunModule(request)) &&
!supportedEdgePolyfills.has(request)
) {
// allows user to provide and use their polyfills, as we do with buffer.
......
import { NextResponse } from 'next/server'
export async function GET() {
const results: Record<string, string> = {}
const modules = [
{ name: 'bunFfi', module: 'bun:ffi' },
{ name: 'bunJsc', module: 'bun:jsc' },
{ name: 'bunSqlite', module: 'bun:sqlite' },
{ name: 'bunTest', module: 'bun:test' },
{ name: 'bunWrap', module: 'bun:wrap' },
{ name: 'bun', module: 'bun' },
]
for (const { name, module } of modules) {
try {
require(module)
results[name] = 'loaded'
} catch (e: any) {
// Expected: Cannot find module error when not in Bun runtime
// This confirms the module was externalized (not bundled)
results[name] = e.message.includes('Cannot find module')
? 'external'
: 'error'
}
}
return NextResponse.json(results)
}
import { NextResponse } from 'next/server'
export const runtime = 'edge'
// Edge runtime should not allow Bun imports
// This test verifies that Bun modules are handled correctly in edge runtime
export async function GET() {
const modules = [
{ name: 'bunFfi', module: 'bun:ffi' },
{ name: 'bunJsc', module: 'bun:jsc' },
{ name: 'bunSqlite', module: 'bun:sqlite' },
{ name: 'bunTest', module: 'bun:test' },
{ name: 'bunWrap', module: 'bun:wrap' },
{ name: 'bun', module: 'bun' },
]
try {
for (const { module } of modules) {
await import(module)
}
return NextResponse.json('Did not throw')
} catch (e) {
return NextResponse.json(String(e))
}
}
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
export default function Page() {
// These should be handled as external modules
// When not running in Bun, require() will throw "Cannot find module"
let bunFfiStatus = 'not loaded'
let bunJscStatus = 'not loaded'
let bunSqliteStatus = 'not loaded'
let bunTestStatus = 'not loaded'
let bunWrapStatus = 'not loaded'
let bunStatus = 'not loaded'
try {
require('bun:ffi')
bunFfiStatus = 'loaded successfully'
} catch (e: any) {
// Expected error when not running in Bun
bunFfiStatus = e.message.includes('Cannot find module')
? 'external (not found)'
: 'error: ' + e.message
}
try {
require('bun:jsc')
bunJscStatus = 'loaded successfully'
} catch (e: any) {
bunJscStatus = e.message.includes('Cannot find module')
? 'external (not found)'
: 'error: ' + e.message
}
try {
require('bun:sqlite')
bunSqliteStatus = 'loaded successfully'
} catch (e: any) {
bunSqliteStatus = e.message.includes('Cannot find module')
? 'external (not found)'
: 'error: ' + e.message
}
try {
require('bun:test')
bunTestStatus = 'loaded successfully'
} catch (e: any) {
bunTestStatus = e.message.includes('Cannot find module')
? 'external (not found)'
: 'error: ' + e.message
}
try {
require('bun:wrap')
bunWrapStatus = 'loaded successfully'
} catch (e: any) {
bunWrapStatus = e.message.includes('Cannot find module')
? 'external (not found)'
: 'error: ' + e.message
}
try {
require('bun')
bunStatus = 'loaded successfully'
} catch (e: any) {
bunStatus = e.message.includes('Cannot find module')
? 'external (not found)'
: 'error: ' + e.message
}
return (
<div>
<h1>Bun Externals Test</h1>
<div id="bun-ffi">{bunFfiStatus}</div>
<div id="bun-jsc">{bunJscStatus}</div>
<div id="bun-sqlite">{bunSqliteStatus}</div>
<div id="bun-test">{bunTestStatus}</div>
<div id="bun-wrap">{bunWrapStatus}</div>
<div id="bun">{bunStatus}</div>
</div>
)
}
'use server'
export async function testBunExternals() {
const modules = [
'bun:ffi',
'bun:jsc',
'bun:sqlite',
'bun:test',
'bun:wrap',
'bun',
]
const results: Record<string, string> = {}
for (const mod of modules) {
try {
require(mod)
results[mod] = 'loaded'
} catch (e: any) {
// Expected: Cannot find module error when not in Bun runtime
results[mod] = e.message.includes('Cannot find module')
? 'external (not found)'
: 'error'
}
}
// All modules should be external (not found when not in Bun)
const allExternal = Object.values(results).every(
(r) => r === 'external (not found)' || r === 'loaded'
)
return allExternal
? 'All Bun modules are external'
: 'Some modules were not properly externalized'
}
'use client'
import { useState } from 'react'
import { testBunExternals } from './actions'
export default function ServerActionPage() {
const [result, setResult] = useState('')
async function handleClick() {
const res = await testBunExternals()
setResult(res)
}
return (
<div>
<h1>Server Action Test</h1>
<button id="test-action" onClick={handleClick}>
Test Bun Externals in Server Action
</button>
{result && <div id="action-result">{result}</div>}
</div>
)
}
import { nextTestSetup } from 'e2e-utils'
describe('app-dir - bun externals', () => {
const { next, isNextDev, isTurbopack, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
})
if (skipped) {
return
}
it('should handle bun builtins as external modules', async () => {
const $ = await next.render$('/')
// When not running in Bun, these should throw "Cannot find module" errors
expect($('#bun-ffi').text()).toBe('external (not found)')
expect($('#bun-jsc').text()).toBe('external (not found)')
expect($('#bun-sqlite').text()).toBe('external (not found)')
expect($('#bun-test').text()).toBe('external (not found)')
expect($('#bun-wrap').text()).toBe('external (not found)')
expect($('#bun').text()).toBe('external (not found)')
})
it('should handle bun builtins in server actions', async () => {
const browser = await next.browser('/server-action')
await browser.elementByCss('#test-action').click()
await browser.waitForElementByCss('#action-result')
const result = await browser.elementByCss('#action-result').text()
expect(result).toContain('All Bun modules are external')
})
it('should handle bun builtins in route handlers', async () => {
const response = await next.fetch('/api/bun-externals')
const data = await response.json()
expect(data.bunFfi).toBe('external')
expect(data.bunJsc).toBe('external')
expect(data.bunSqlite).toBe('external')
expect(data.bunTest).toBe('external')
expect(data.bunWrap).toBe('external')
expect(data.bun).toBe('external')
})
it('should handle bun builtins in edge runtime', async () => {
const response = await next.fetch('/api/edge-bun-externals')
expect(await response.json()).toMatch(
/(Error: Cannot find module 'bun.*'|Error: Failed to load external module bun.*)/
)
})
if (!isTurbopack && !isNextDev) {
it('should not bundle bun builtins in server bundles', async () => {
await next.fetch('/')
const rscBundle = await next.readFile('.next/server/app/page.js')
expect(rscBundle).not.toContain('bun:ffi implementation')
expect(rscBundle).not.toContain('bun:jsc implementation')
expect(rscBundle).not.toContain('bun:sqlite implementation')
expect(rscBundle).not.toContain('bun:test implementation')
expect(rscBundle).not.toContain('bun:wrap implementation')
expect(rscBundle).toMatch(/require\(["']bun:ffi["']\)/)
expect(rscBundle).toMatch(/require\(["']bun:jsc["']\)/)
expect(rscBundle).toMatch(/require\(["']bun:sqlite["']\)/)
expect(rscBundle).toMatch(/require\(["']bun:test["']\)/)
expect(rscBundle).toMatch(/require\(["']bun:wrap["']\)/)
expect(rscBundle).toMatch(/require\(["']bun["']\)/)
})
}
})
// This page tests that Bun modules are properly externalized
export default function Page() {
return <div>Webpack Bun Externals Test</div>
}
export async function getServerSideProps() {
const results = {}
try {
require('bun:ffi')
results['bun:ffi'] = 'loaded'
} catch (e) {
results['bun:ffi'] = 'external (not found)'
}
try {
require('bun:jsc')
results['bun:jsc'] = 'loaded'
} catch (e) {
results['bun:jsc'] = 'external (not found)'
}
try {
require('bun:sqlite')
results['bun:sqlite'] = 'loaded'
} catch (e) {
results['bun:sqlite'] = 'external (not found)'
}
try {
require('bun:test')
results['bun:test'] = 'loaded'
} catch (e) {
results['bun:test'] = 'external (not found)'
}
try {
require('bun:wrap')
results['bun:wrap'] = 'loaded'
} catch (e) {
results['bun:wrap'] = 'external (not found)'
}
try {
require('bun')
results['bun'] = 'loaded'
} catch (e) {
results['bun'] = 'external (not found)'
}
return { props: { results } }
}
/* eslint-env jest */
import fs from 'fs-extra'
import { nextBuild } from 'next-test-utils'
import { join } from 'path'
const appDir = join(__dirname, '../')
const IS_TURBOPACK = process.env.IS_TURBOPACK_TEST === '1'
;(IS_TURBOPACK ? describe.skip : describe)('Webpack - Bun Externals', () => {
let buildResult
beforeAll(async () => {
buildResult = await nextBuild(appDir, [], {
stdout: true,
stderr: true,
})
})
it('should successfully build with Bun module imports', () => {
// The build should succeed even with Bun module imports
expect(buildResult.code).toBe(0)
})
it('should externalize Bun builtins in server bundles', async () => {
// Check the server bundle
const serverBundle = await fs.readFile(
join(appDir, '.next/server/pages/index.js'),
'utf8'
)
// Bun modules should be treated as external
// When modules are external, webpack preserves the require() calls
const bunModules = [
'bun:ffi',
'bun:jsc',
'bun:sqlite',
'bun:test',
'bun:wrap',
'bun',
]
bunModules.forEach((mod) => {
// Check that the module name appears in require() calls
// This indicates it was externalized, not bundled
const escapedMod = mod.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
expect(serverBundle).toInclude(`require("${escapedMod}")`)
})
})
it('should not bundle Bun module implementations', async () => {
const serverBundle = await fs.readFile(
join(appDir, '.next/server/pages/index.js'),
'utf8'
)
// These patterns would indicate the modules were bundled, which we don't want
expect(serverBundle).not.toContain('__webpack_require__.resolve("bun")')
})
})
......@@ -84,6 +84,15 @@ const NODE_EXTERNALS: [&str; 64] = [
const EDGE_NODE_EXTERNALS: [&str; 5] = ["buffer", "events", "assert", "util", "async_hooks"];
const BUN_EXTERNALS: [&str; 6] = [
"bun:ffi",
"bun:jsc",
"bun:sqlite",
"bun:test",
"bun:wrap",
"bun",
];
#[turbo_tasks::function]
async fn base_resolve_options(
fs: ResolvedVc<Box<dyn FileSystem>>,
......@@ -98,11 +107,15 @@ async fn base_resolve_options(
} else {
opt.enable_node_externals
};
if node_externals || opt.enable_edge_node_externals {
let untraced_external_cell =
ImportMapping::External(None, ExternalType::CommonJs, ExternalTraced::Untraced)
.resolved_cell();
let untraced_external_cell =
ImportMapping::External(None, ExternalType::CommonJs, ExternalTraced::Untraced)
.resolved_cell();
for req in BUN_EXTERNALS {
direct_mappings.insert(AliasPattern::exact(req), untraced_external_cell);
}
if node_externals || opt.enable_edge_node_externals {
if node_externals {
for req in NODE_EXTERNALS {
direct_mappings.insert(AliasPattern::exact(req), untraced_external_cell);
......@@ -112,6 +125,7 @@ async fn base_resolve_options(
);
}
}
if opt.enable_edge_node_externals {
for req in EDGE_NODE_EXTERNALS {
direct_mappings.insert(
......
// Test that Bun builtins are treated as external
import bunFfi from 'bun:ffi'
import bunJsc from 'bun:jsc'
import bunSqlite from 'bun:sqlite'
import bunTest from 'bun:test'
import bunWrap from 'bun:wrap'
import bun from 'bun'
console.log('bun:ffi:', bunFfi)
console.log('bun:jsc:', bunJsc)
console.log('bun:sqlite:', bunSqlite)
console.log('bun:test:', bunTest)
console.log('bun:wrap:', bunWrap)
console.log('bun:', bun)
(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/[root-of-the-server]__e251cfe1._.js", {
"[externals]/bun:ffi [external] (bun:ffi, cjs)": ((__turbopack_context__) => {
var { m: module, e: exports } = __turbopack_context__;
{
const mod = __turbopack_context__.x("bun:ffi", () => require("bun:ffi"));
module.exports = mod;
}}),
"[externals]/bun:jsc [external] (bun:jsc, cjs)": ((__turbopack_context__) => {
var { m: module, e: exports } = __turbopack_context__;
{
const mod = __turbopack_context__.x("bun:jsc", () => require("bun:jsc"));
module.exports = mod;
}}),
"[externals]/bun:sqlite [external] (bun:sqlite, cjs)": ((__turbopack_context__) => {
var { m: module, e: exports } = __turbopack_context__;
{
const mod = __turbopack_context__.x("bun:sqlite", () => require("bun:sqlite"));
module.exports = mod;
}}),
"[externals]/bun:test [external] (bun:test, cjs)": ((__turbopack_context__) => {
var { m: module, e: exports } = __turbopack_context__;
{
const mod = __turbopack_context__.x("bun:test", () => require("bun:test"));
module.exports = mod;
}}),
"[externals]/bun:wrap [external] (bun:wrap, cjs)": ((__turbopack_context__) => {
var { m: module, e: exports } = __turbopack_context__;
{
const mod = __turbopack_context__.x("bun:wrap", () => require("bun:wrap"));
module.exports = mod;
}}),
"[externals]/bun [external] (bun, cjs)": ((__turbopack_context__) => {
var { m: module, e: exports } = __turbopack_context__;
{
const mod = __turbopack_context__.x("bun", () => require("bun"));
module.exports = mod;
}}),
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/node/bun_protocol_external/input/index.js [test] (ecmascript)": ((__turbopack_context__) => {
"use strict";
// Test that Bun builtins are treated as external
__turbopack_context__.s({});
var __TURBOPACK__imported__module__$5b$externals$5d2f$bun$3a$ffi__$5b$external$5d$__$28$bun$3a$ffi$2c$__cjs$29$__ = __turbopack_context__.i("[externals]/bun:ffi [external] (bun:ffi, cjs)");
var __TURBOPACK__imported__module__$5b$externals$5d2f$bun$3a$jsc__$5b$external$5d$__$28$bun$3a$jsc$2c$__cjs$29$__ = __turbopack_context__.i("[externals]/bun:jsc [external] (bun:jsc, cjs)");
var __TURBOPACK__imported__module__$5b$externals$5d2f$bun$3a$sqlite__$5b$external$5d$__$28$bun$3a$sqlite$2c$__cjs$29$__ = __turbopack_context__.i("[externals]/bun:sqlite [external] (bun:sqlite, cjs)");
var __TURBOPACK__imported__module__$5b$externals$5d2f$bun$3a$test__$5b$external$5d$__$28$bun$3a$test$2c$__cjs$29$__ = __turbopack_context__.i("[externals]/bun:test [external] (bun:test, cjs)");
var __TURBOPACK__imported__module__$5b$externals$5d2f$bun$3a$wrap__$5b$external$5d$__$28$bun$3a$wrap$2c$__cjs$29$__ = __turbopack_context__.i("[externals]/bun:wrap [external] (bun:wrap, cjs)");
var __TURBOPACK__imported__module__$5b$externals$5d2f$bun__$5b$external$5d$__$28$bun$2c$__cjs$29$__ = __turbopack_context__.i("[externals]/bun [external] (bun, cjs)");
;
;
;
;
;
;
console.log('bun:ffi:', __TURBOPACK__imported__module__$5b$externals$5d2f$bun$3a$ffi__$5b$external$5d$__$28$bun$3a$ffi$2c$__cjs$29$__["default"]);
console.log('bun:jsc:', __TURBOPACK__imported__module__$5b$externals$5d2f$bun$3a$jsc__$5b$external$5d$__$28$bun$3a$jsc$2c$__cjs$29$__["default"]);
console.log('bun:sqlite:', __TURBOPACK__imported__module__$5b$externals$5d2f$bun$3a$sqlite__$5b$external$5d$__$28$bun$3a$sqlite$2c$__cjs$29$__["default"]);
console.log('bun:test:', __TURBOPACK__imported__module__$5b$externals$5d2f$bun$3a$test__$5b$external$5d$__$28$bun$3a$test$2c$__cjs$29$__["default"]);
console.log('bun:wrap:', __TURBOPACK__imported__module__$5b$externals$5d2f$bun$3a$wrap__$5b$external$5d$__$28$bun$3a$wrap$2c$__cjs$29$__["default"]);
console.log('bun:', __TURBOPACK__imported__module__$5b$externals$5d2f$bun__$5b$external$5d$__$28$bun$2c$__cjs$29$__["default"]);
}),
}]);
//# sourceMappingURL=%5Broot-of-the-server%5D__e251cfe1._.js.map
\ No newline at end of file
{
"version": 3,
"sources": [],
"sections": [
{"offset": {"line": 53, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/node/bun_protocol_external/input/index.js"],"sourcesContent":["// Test that Bun builtins are treated as external\nimport bunFfi from 'bun:ffi'\nimport bunJsc from 'bun:jsc'\nimport bunSqlite from 'bun:sqlite'\nimport bunTest from 'bun:test'\nimport bunWrap from 'bun:wrap'\nimport bun from 'bun'\n\nconsole.log('bun:ffi:', bunFfi)\nconsole.log('bun:jsc:', bunJsc)\nconsole.log('bun:sqlite:', bunSqlite)\nconsole.log('bun:test:', bunTest)\nconsole.log('bun:wrap:', bunWrap)\nconsole.log('bun:', bun)\n"],"names":[],"mappings":"AAAA,iDAAiD;;AACjD;AACA;AACA;AACA;AACA;AACA;;;;;;;AAEA,QAAQ,GAAG,CAAC,YAAY,wHAAM;AAC9B,QAAQ,GAAG,CAAC,YAAY,wHAAM;AAC9B,QAAQ,GAAG,CAAC,eAAe,8HAAS;AACpC,QAAQ,GAAG,CAAC,aAAa,0HAAO;AAChC,QAAQ,GAAG,CAAC,aAAa,0HAAO;AAChC,QAAQ,GAAG,CAAC,QAAQ,0GAAG"}}]
}
\ No newline at end of file
(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([
"output/a5fc1_turbopack-tests_tests_snapshot_node_bun_protocol_external_input_index_e993d79b.js",
{},
{"otherChunks":["output/[root-of-the-server]__e251cfe1._.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/node/bun_protocol_external/input/index.js [test] (ecmascript)"]}
]);
// Dummy runtime
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment