Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 13 additions & 7 deletions packages/next/src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1769,6 +1769,7 @@ export default async function loadConfig(
configFileName = basename(path)

let userConfigModule: any
let loadedConfig: NextConfig
try {
const envBefore = Object.assign({}, process.env)

Expand Down Expand Up @@ -1809,6 +1810,18 @@ export default async function loadConfig(

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) {
// Capture the error for MCP tool reporting
NextInstanceErrorState.nextConfig.push(err)
Expand All @@ -1820,13 +1833,6 @@ export default async function loadConfig(
throw err
}

const loadedConfig = Object.freeze(
(await normalizeConfig(
phase,
interopDefault(userConfigModule)
)) as NextConfig
)

if (loadedConfig.experimental) {
for (const name of Object.keys(
loadedConfig.experimental
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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'
)
})
})
})
3 changes: 3 additions & 0 deletions test/production/config-evaluation-error/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page(props) {
return <p>index page</p>
}
Loading