Unverified Commit 56e760a2 authored by Gal Schlezinger's avatar Gal Schlezinger Committed by GitHub

[build] validate the exported config values (#38370)

Right now if people will accidentally export a typo like `runtime: 'experimental-egde'` we will fail silently.
This commit ensures we will throw and fail loudly when such typos occur.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`
parent 20c1079e
import type { ServerRuntime } from '../../server/config-shared'
import { isServerRuntime, ServerRuntime } from '../../server/config-shared'
import type { NextConfig } from '../../server/config-shared'
import { tryToExtractExportedConstValue } from './extract-const-value'
import { escapeStringRegexp } from '../../shared/lib/escape-regexp'
......@@ -40,6 +40,24 @@ export async function getPageStaticInfo(params: {
const { ssg, ssr } = checkExports(swcAST)
const config = tryToExtractExportedConstValue(swcAST, 'config') || {}
if (
typeof config.runtime !== 'string' &&
typeof config.runtime !== 'undefined'
) {
throw new Error(`Provided runtime `)
} else if (!isServerRuntime(config.runtime)) {
const options = Object.values(SERVER_RUNTIME).join(', ')
if (typeof config.runtime !== 'string') {
throw new Error(
`The \`runtime\` config must be a string. Please leave it empty or choose one of: ${options}`
)
} else {
throw new Error(
`Provided runtime "${config.runtime}" is not supported. Please leave it empty or choose one of: ${options}`
)
}
}
let runtime =
SERVER_RUNTIME.edge === config?.runtime
? SERVER_RUNTIME.edge
......
......@@ -561,3 +561,9 @@ export async function normalizeConfig(phase: string, config: any) {
// Support `new Promise` and `async () =>` as return values of the config export
return await config
}
export function isServerRuntime(value?: string): value is ServerRuntime {
return (
value === undefined || value === 'nodejs' || value === 'experimental-edge'
)
}
export default function Page() {
return <p>hello world</p>
}
export const config = {
runtime: 'something-odd',
}
import { nextBuild } from 'next-test-utils'
import path from 'path'
describe('Exported runtimes value validation', () => {
test('fails to build on malformed input', async () => {
const result = await nextBuild(
path.resolve(__dirname, './app'),
undefined,
{ stdout: true, stderr: true }
)
expect(result).toMatchObject({
code: 1,
stderr: expect.stringContaining(
`Provided runtime "something-odd" is not supported.`
),
})
})
})
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