Files
next.js/test/e2e/app-dir/cache-components-segment-configs/cache-components-segment-configs.test.ts
1287f9a027 Turbopack: show specific SWC error messages as error titles (#91022)
### What?

Changes Turbopack's error overlay to show specific SWC diagnostic
messages as the error title instead of generic messages like "Parsing
ecmascript source code failed" or "Ecmascript file had an error".

### Why?

Previously, all SWC parse/analysis errors in Turbopack showed a generic
title (e.g. "Parsing ecmascript source code failed") in the redbox
header, with the actual specific error message buried in the description
below the code frame. This made it harder for developers to quickly
understand what went wrong.

**Before:**
```
Parsing ecmascript source code failed
> 1 | export default () => <div/
    |                           ^
Expected '>', got '<eof>'
```

**After:**
```
Expected '>', got '<eof>'
> 1 | export default () => <div/
    |                           ^
Parsing ecmascript source code failed
```

### How?

**Core change** in
`turbopack/crates/turbopack-swc-utils/src/emitter.rs`:

When the `IssueEmitter` has a `self.title` set (the generic title like
"Parsing ecmascript source code failed"), the SWC diagnostic message is
now used as the issue title, and the generic title is demoted to the
description. When `self.title` is not set, the existing behavior is
preserved (first line of message becomes title, rest becomes
description).

**Test updates** across ~15 test files:

Updated all `isTurbopack` branches in test expectations to reflect the
swapped title/description. Only Turbopack-specific branches were
modified; webpack and rspack expectations are unchanged.

**New test suite** (`test/development/app-dir/ecmascript-error-title/`):

Dedicated tests verifying that both syntax errors (e.g. `Expected '>',
got '<eof>'`) and analysis errors (e.g. `the name 'Table' is defined
multiple times`) show the specific SWC message as the redbox title.

**Turbopack snapshot updates:**

4 snapshot files renamed to reflect new titles (e.g. `Parsing ecmascript
source code failed-*.txt` → `Expression expected-*.txt`).

---------

Co-authored-by: Claude <[email protected]>
2026-03-09 08:00:37 +01:00

148 lines
5.1 KiB
TypeScript

import { nextTestSetup } from 'e2e-utils'
import {
retry,
waitForRedbox,
getRedboxDescription,
getRedboxSource,
} from 'next-test-utils'
describe('cache-components-segment-configs', () => {
const { next, skipped, isNextDev, isTurbopack } = nextTestSetup({
files: __dirname + '/fixtures/default',
skipStart: true,
skipDeployment: true,
})
if (skipped) {
return
}
it("it should error when using segment configs that aren't supported by cacheComponents", async () => {
try {
await next.start()
} catch {
// we expect the build to fail
}
if (isNextDev) {
const browser = await next.browser('/revalidate')
await waitForRedbox(browser)
const redbox = {
description: await getRedboxDescription(browser),
source: await getRedboxSource(browser),
}
if (isTurbopack) {
expect(redbox.description).toMatchInlineSnapshot(
`"Route segment config "revalidate" is not compatible with \`nextConfig.cacheComponents\`. Please remove it."`
)
} else {
expect(redbox.description).toMatchInlineSnapshot(
`" x Route segment config "revalidate" is not compatible with \`nextConfig.cacheComponents\`. Please remove it."`
)
}
expect(redbox.source).toContain(
'"revalidate" is not compatible with `nextConfig.cacheComponents`. Please remove it.'
)
} else {
expect(next.cliOutput).toContain('./app/dynamic-params/[slug]/page.tsx')
expect(next.cliOutput).toContain(
'"dynamicParams" is not compatible with `nextConfig.cacheComponents`. Please remove it.'
)
expect(next.cliOutput).toContain('./app/dynamic/nested/page.tsx')
expect(next.cliOutput).toContain('./app/dynamic/page.tsx')
expect(next.cliOutput).toContain(
'"dynamic" is not compatible with `nextConfig.cacheComponents`. Please remove it.'
)
expect(next.cliOutput).toContain('./app/fetch-cache/page.tsx')
expect(next.cliOutput).toContain(
'"fetchCache" is not compatible with `nextConfig.cacheComponents`. Please remove it.'
)
expect(next.cliOutput).toContain('./app/revalidate/page.tsx')
expect(next.cliOutput).toContain(
'"revalidate" is not compatible with `nextConfig.cacheComponents`. Please remove it.'
)
}
})
it('should propagate configurations from layouts to pages', async () => {
// patch the root layout. We expect the "dynamic" segment config to now be part of
// each sub-page that uses this layout.
await next.patchFile(
'app/layout.tsx',
(content) => {
return `
export const runtime = 'nodejs';
${content}
`
},
async () => {
try {
await next.start()
} catch {
// we expect the build to fail
}
if (isNextDev) {
const browser = await next.browser('/revalidate')
await waitForRedbox(browser)
const redbox = {
description: await getRedboxDescription(browser),
source: await getRedboxSource(browser),
}
if (isTurbopack) {
// The page-level error is shown first in the redbox, but
// the layout error is also present in the CLI output.
expect(redbox.description).toMatchInlineSnapshot(
`"Route segment config "revalidate" is not compatible with \`nextConfig.cacheComponents\`. Please remove it."`
)
} else {
expect(redbox.description).toMatchInlineSnapshot(
`" x Route segment config "runtime" is not compatible with \`nextConfig.cacheComponents\`. Please remove it."`
)
}
expect(redbox.source).toContain(
'is not compatible with `nextConfig.cacheComponents`. Please remove it.'
)
// Verify that the "runtime" error from the layout propagation
// is present in the CLI output even if it's not the first error
// shown in the redbox.
expect(next.cliOutput).toContain(
'"runtime" is not compatible with `nextConfig.cacheComponents`. Please remove it.'
)
} else {
await retry(async () => {
expect(next.cliOutput).toContain(
'"runtime" is not compatible with `nextConfig.cacheComponents`. Please remove it.'
)
// the stack trace is different between turbopack/webpack
if (isTurbopack) {
expectLinesToAppearTogether(next.cliOutput, [
'./app/layout.tsx:2:24',
])
} else {
expectLinesToAppearTogether(next.cliOutput, [
'Import trace for requested module:',
'./app/fetch-cache/page.tsx',
'./app/layout.tsx',
])
}
})
}
}
)
})
})
function expectLinesToAppearTogether(output: string, lines: string[]) {
const escapedLines = lines.map((line) =>
line.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
)
const pattern = new RegExp(escapedLines.join('\\s*'), 's')
expect(output).toMatch(pattern)
}