Unverified Commit 0114ea24 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

Eval next.config export functions in the try catch (#93884)

Move next.config evaluation inside the try catch where we import and
transform it

This ensures we print an appropriate help link if the export function
throws
parent 15d2272c
...@@ -1805,6 +1805,7 @@ export default async function loadConfig( ...@@ -1805,6 +1805,7 @@ export default async function loadConfig(
configFileName = basename(path) configFileName = basename(path)
let userConfigModule: any let userConfigModule: any
let loadedConfig: NextConfig
try { try {
const envBefore = Object.assign({}, process.env) const envBefore = Object.assign({}, process.env)
...@@ -1845,6 +1846,18 @@ export default async function loadConfig( ...@@ -1845,6 +1846,18 @@ export default async function loadConfig(
return userConfigModule return userConfigModule
} }
// `normalizeConfig` invokes the user's exported config function (or
// awaits its returned promise) if it is one. Errors thrown from that
// call belong to the same "failed to load config" category as parse
// errors from `import()` above, so we keep them inside this try/catch
// to attach the same framing message.
loadedConfig = Object.freeze(
(await normalizeConfig(
phase,
interopDefault(userConfigModule)
)) as NextConfig
)
} catch (err) { } catch (err) {
// Capture the error for MCP tool reporting // Capture the error for MCP tool reporting
NextInstanceErrorState.nextConfig.push(err) NextInstanceErrorState.nextConfig.push(err)
...@@ -1856,13 +1869,6 @@ export default async function loadConfig( ...@@ -1856,13 +1869,6 @@ export default async function loadConfig(
throw err throw err
} }
const loadedConfig = Object.freeze(
(await normalizeConfig(
phase,
interopDefault(userConfigModule)
)) as NextConfig
)
if (loadedConfig.experimental) { if (loadedConfig.experimental) {
for (const name of Object.keys( for (const name of Object.keys(
loadedConfig.experimental loadedConfig.experimental
......
import { nextTestSetup } from 'e2e-utils'
describe('next.config evaluation error', () => {
describe('production mode', () => {
const { next, skipped } = nextTestSetup({
files: __dirname,
skipStart: true,
skipDeployment: true,
})
if (skipped) return
async function buildAndGetOutput(): Promise<string> {
const start = next.cliOutput.length
await next.build()
return next.cliOutput.slice(start)
}
it('should report a helpful error when the config function throws synchronously', async () => {
await next.patchFile(
'next.config.js',
`
module.exports = () => {
return { foo: new Uint8Array(5_000_000_000) }
}
`
)
const output = await buildAndGetOutput()
expect(output).toContain('Invalid typed array length')
expect(output).toContain(
'Failed to load next.config.js, see more info here https://nextjs.org/docs/messages/next-config-error'
)
})
it('should report a helpful error when the config module throws at the top level', async () => {
await next.patchFile(
'next.config.js',
`
const buf = new Uint8Array(5_000_000_000)
module.exports = { foo: buf }
`
)
const output = await buildAndGetOutput()
expect(output).toContain('Invalid typed array length')
expect(output).toContain(
'Failed to load next.config.js, see more info here https://nextjs.org/docs/messages/next-config-error'
)
})
it('should report a helpful error when the config function rejects', async () => {
await next.patchFile(
'next.config.js',
`
module.exports = async () => {
throw new Error('boom from async config plugin')
}
`
)
const output = await buildAndGetOutput()
expect(output).toContain('boom from async config plugin')
expect(output).toContain(
'Failed to load next.config.js, see more info here https://nextjs.org/docs/messages/next-config-error'
)
})
})
})
export default function Page(props) {
return <p>index page</p>
}
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