Commit 1589416e authored by Luke Sandberg's avatar Luke Sandberg Committed by Jiachi Liu

[turbopack] change server source maps in production to use relative paths (#85576)

Co-authored-by: 's avatarSebastian Sebbie Silbermann <sebastian.silbermann@vercel.com>
parent 02bd4b5c
...@@ -1063,8 +1063,7 @@ pub async fn get_server_chunking_context_with_client_assets( ...@@ -1063,8 +1063,7 @@ pub async fn get_server_chunking_context_with_client_assets(
builder = builder.source_map_source_type(if next_mode.is_development() { builder = builder.source_map_source_type(if next_mode.is_development() {
SourceMapSourceType::AbsoluteFileUri SourceMapSourceType::AbsoluteFileUri
} else { } else {
// TODO(lukesandberg): switch to relative once next is compatible. SourceMapSourceType::RelativeUri
SourceMapSourceType::TurbopackUri
}); });
if next_mode.is_production() { if next_mode.is_production() {
builder = builder builder = builder
...@@ -1149,8 +1148,7 @@ pub async fn get_server_chunking_context( ...@@ -1149,8 +1148,7 @@ pub async fn get_server_chunking_context(
builder = builder.source_map_source_type(SourceMapSourceType::AbsoluteFileUri); builder = builder.source_map_source_type(SourceMapSourceType::AbsoluteFileUri);
} else { } else {
builder = builder builder = builder
// TODO(lukesandberg): switch to relative once next is compatible. .source_map_source_type(SourceMapSourceType::RelativeUri)
.source_map_source_type(SourceMapSourceType::TurbopackUri)
.chunking_config( .chunking_config(
Vc::<EcmascriptChunkType>::default().to_resolved().await?, Vc::<EcmascriptChunkType>::default().to_resolved().await?,
ChunkingConfig { ChunkingConfig {
......
...@@ -196,9 +196,16 @@ function getSourcemappedFrameIfPossible( ...@@ -196,9 +196,16 @@ function getSourcemappedFrameIfPossible(
} }
sourceMapPayload = maybeSourceMapPayload sourceMapPayload = maybeSourceMapPayload
try { try {
// Pass the source map URL as the second parameter so that the consumer
// can resolve relative paths in the source map's `sources` array.
// This is a guess! Turbopack places .map files as siblings to the chunks so this is sufficient to compute
// relative paths but is actually wrong (the chunk and sourcemap have different content hashes).
// We are using the node API to read the sourcemap and it doesn't give us access to the URI.
const sourceMapURL = sourceURL + '.map'
sourceMapConsumer = new SyncSourceMapConsumer( sourceMapConsumer = new SyncSourceMapConsumer(
// @ts-expect-error -- Module.SourceMap['version'] is number but SyncSourceMapConsumer wants a string sourceMapPayload,
sourceMapPayload // @ts-expect-error: our typings don't include this parameter but it is here.
sourceMapURL
) )
} catch (cause) { } catch (cause) {
// We should not log an actual error instance here because that will re-enter // We should not log an actual error instance here because that will re-enter
......
...@@ -111,9 +111,6 @@ export function getPrerenderOutput( ...@@ -111,9 +111,6 @@ export function getPrerenderOutput(
) )
.replace(/at \d+ \(/, replaceNumericModuleId) .replace(/at \d+ \(/, replaceNumericModuleId)
.replace(/digest: '\d+'/, "digest: '<error-digest>'") .replace(/digest: '\d+'/, "digest: '<error-digest>'")
// TODO(veil): Bundler protocols should not appear in stack frames.
.replace('webpack:///', 'bundler:///')
.replace('turbopack:///[project]/', 'bundler:///')
lines.push(convertModuleFunctionSequenceExpression(line)) lines.push(convertModuleFunctionSequenceExpression(line))
} }
......
...@@ -8,7 +8,6 @@ function normalizeCliOutput(output: string) { ...@@ -8,7 +8,6 @@ function normalizeCliOutput(output: string) {
stripAnsi(output) stripAnsi(output)
// TODO(veil): Should not appear in sourcemapped stackframes. // TODO(veil): Should not appear in sourcemapped stackframes.
.replaceAll('webpack:///', 'bundler:///') .replaceAll('webpack:///', 'bundler:///')
.replaceAll('turbopack:///[project]/', 'bundler:///')
.replaceAll(/at [a-zA-Z] \(/g, 'at <mangled> (') .replaceAll(/at [a-zA-Z] \(/g, 'at <mangled> (')
) )
} }
...@@ -55,9 +54,9 @@ describe('app-dir - server source maps', () => { ...@@ -55,9 +54,9 @@ describe('app-dir - server source maps', () => {
} else { } else {
if (isTurbopack) { if (isTurbopack) {
// TODO(veil): Sourcemap names // TODO(veil): Sourcemap names
// TODO(veil): relative paths // TODO(veil): relative paths in webpack
expect(normalizeCliOutput(next.cliOutput)).toContain( expect(normalizeCliOutput(next.cliOutput)).toContain(
'(bundler:///app/rsc-error-log/page.js:4:17)' '(app/rsc-error-log/page.js:4:17)'
) )
expect(normalizeCliOutput(next.cliOutput)).toContain( expect(normalizeCliOutput(next.cliOutput)).toContain(
'' + '' +
...@@ -106,10 +105,10 @@ describe('app-dir - server source maps', () => { ...@@ -106,10 +105,10 @@ describe('app-dir - server source maps', () => {
// TODO(veil): Sourcemap names // TODO(veil): Sourcemap names
// TODO(veil): relative paths // TODO(veil): relative paths
expect(normalizeCliOutput(next.cliOutput)).toContain( expect(normalizeCliOutput(next.cliOutput)).toContain(
'(bundler:///app/rsc-error-log-cause/page.js:2:17)' '(app/rsc-error-log-cause/page.js:2:17)'
) )
expect(normalizeCliOutput(next.cliOutput)).toContain( expect(normalizeCliOutput(next.cliOutput)).toContain(
'(bundler:///app/rsc-error-log-cause/page.js:7:17)' '(app/rsc-error-log-cause/page.js:7:17)'
) )
expect(normalizeCliOutput(next.cliOutput)).toContain( expect(normalizeCliOutput(next.cliOutput)).toContain(
'' + '' +
...@@ -230,7 +229,7 @@ describe('app-dir - server source maps', () => { ...@@ -230,7 +229,7 @@ describe('app-dir - server source maps', () => {
// TODO(veil): Sourcemap names // TODO(veil): Sourcemap names
// TODO(veil): relative paths // TODO(veil): relative paths
expect(normalizeCliOutput(next.cliOutput)).toContain( expect(normalizeCliOutput(next.cliOutput)).toContain(
'(bundler:///app/ssr-error-log-ignore-listed/page.js:9:17)' '(app/ssr-error-log-ignore-listed/page.js:9:17)'
) )
expect(normalizeCliOutput(next.cliOutput)).toContain( expect(normalizeCliOutput(next.cliOutput)).toContain(
'\n' + '\n' +
...@@ -295,7 +294,7 @@ describe('app-dir - server source maps', () => { ...@@ -295,7 +294,7 @@ describe('app-dir - server source maps', () => {
// TODO(veil): Sourcemap names // TODO(veil): Sourcemap names
// TODO(veil): relative paths // TODO(veil): relative paths
expect(normalizeCliOutput(next.cliOutput)).toContain( expect(normalizeCliOutput(next.cliOutput)).toContain(
'at <unknown> (bundler:///app/rsc-error-log-ignore-listed/page.js:8:17)' 'at <unknown> (app/rsc-error-log-ignore-listed/page.js:8:17)'
) )
expect(normalizeCliOutput(next.cliOutput)).toContain( expect(normalizeCliOutput(next.cliOutput)).toContain(
'' + '' +
...@@ -528,7 +527,7 @@ describe('app-dir - server source maps', () => { ...@@ -528,7 +527,7 @@ describe('app-dir - server source maps', () => {
'' + '' +
'\nError: module-evaluation' + '\nError: module-evaluation' +
// TODO(veil): Turbopack internals. Feel free to update. Tracked in https://linear.app/vercel/issue/NEXT-4362 // TODO(veil): Turbopack internals. Feel free to update. Tracked in https://linear.app/vercel/issue/NEXT-4362
'\n at module evaluation (bundler:///app/module-evaluation/module.js:1:22)' '\n at module evaluation (app/module-evaluation/module.js:1:22)'
) )
expect(normalizeCliOutput(next.cliOutput)).toContain( expect(normalizeCliOutput(next.cliOutput)).toContain(
'' + '' +
......
...@@ -67,7 +67,7 @@ describe('build-output-prerender', () => { ...@@ -67,7 +67,7 @@ describe('build-output-prerender', () => {
// TODO(veil): Why is the location incomplete unless we enable --no-mangling? // TODO(veil): Why is the location incomplete unless we enable --no-mangling?
expect(getPrerenderOutput(next.cliOutput)).toMatchInlineSnapshot(` expect(getPrerenderOutput(next.cliOutput)).toMatchInlineSnapshot(`
"Error: Route "/client" used \`new Date()\` inside a Client Component without a Suspense boundary above it. See more info here: https://nextjs.org/docs/messages/next-prerender-current-time-client "Error: Route "/client" used \`new Date()\` inside a Client Component without a Suspense boundary above it. See more info here: https://nextjs.org/docs/messages/next-prerender-current-time-client
at c (bundler:///app/client/page.tsx:4:28) at c (app/client/page.tsx:4:28)
2 | 2 |
3 | export default function Page() { 3 | export default function Page() {
> 4 | return <p>Current time: {new Date().toISOString()}</p> > 4 | return <p>Current time: {new Date().toISOString()}</p>
...@@ -181,32 +181,62 @@ describe('build-output-prerender', () => { ...@@ -181,32 +181,62 @@ describe('build-output-prerender', () => {
}) })
it('shows all prerender errors with readable stacks and code frames', async () => { it('shows all prerender errors with readable stacks and code frames', async () => {
expect(getPrerenderOutput(next.cliOutput)).toMatchInlineSnapshot(` if (isTurbopack) {
"Error: Route "/client" used \`new Date()\` inside a Client Component without a Suspense boundary above it. See more info here: https://nextjs.org/docs/messages/next-prerender-current-time-client expect(getPrerenderOutput(next.cliOutput)).toMatchInlineSnapshot(`
at Page (bundler:///app/client/page.tsx:4:28) "Error: Route "/client" used \`new Date()\` inside a Client Component without a Suspense boundary above it. See more info here: https://nextjs.org/docs/messages/next-prerender-current-time-client
2 | at Page (app/client/page.tsx:4:28)
3 | export default function Page() { 2 |
> 4 | return <p>Current time: {new Date().toISOString()}</p> 3 | export default function Page() {
| ^ > 4 | return <p>Current time: {new Date().toISOString()}</p>
5 | } | ^
6 | 5 | }
To get a more detailed stack trace and pinpoint the issue, start the app in development mode by running \`next dev\`, then open "/client" in your browser to investigate the error. 6 |
Error occurred prerendering page "/client". Read more: https://nextjs.org/docs/messages/prerender-error To get a more detailed stack trace and pinpoint the issue, start the app in development mode by running \`next dev\`, then open "/client" in your browser to investigate the error.
Error: Route "/server" used \`Math.random()\` before accessing either uncached data (e.g. \`fetch()\`) or Request data (e.g. \`cookies()\`, \`headers()\`, \`connection()\`, and \`searchParams\`). Accessing random values synchronously in a Server Component requires reading one of these data sources first. Alternatively, consider moving this expression into a Client Component or Cache Component. See more info here: https://nextjs.org/docs/messages/next-prerender-random Error occurred prerendering page "/client". Read more: https://nextjs.org/docs/messages/prerender-error
at Page (bundler:///app/server/page.tsx:13:27) Error: Route "/server" used \`Math.random()\` before accessing either uncached data (e.g. \`fetch()\`) or Request data (e.g. \`cookies()\`, \`headers()\`, \`connection()\`, and \`searchParams\`). Accessing random values synchronously in a Server Component requires reading one of these data sources first. Alternatively, consider moving this expression into a Client Component or Cache Component. See more info here: https://nextjs.org/docs/messages/next-prerender-random
11 | await cachedDelay() at Page (app/server/page.tsx:13:27)
12 | 11 | await cachedDelay()
> 13 | return <p>Random: {Math.random()}</p> 12 |
| ^ > 13 | return <p>Random: {Math.random()}</p>
14 | } | ^
15 | 14 | }
To get a more detailed stack trace and pinpoint the issue, start the app in development mode by running \`next dev\`, then open "/server" in your browser to investigate the error. 15 |
Error occurred prerendering page "/server". Read more: https://nextjs.org/docs/messages/prerender-error To get a more detailed stack trace and pinpoint the issue, start the app in development mode by running \`next dev\`, then open "/server" in your browser to investigate the error.
Error occurred prerendering page "/server". Read more: https://nextjs.org/docs/messages/prerender-error
> Export encountered errors on following paths:
/client/page: /client
/server/page: /server"
`)
} else {
// TODO(veil): Bundler protocols should not appear in stackframes.
expect(getPrerenderOutput(next.cliOutput)).toMatchInlineSnapshot(`
"Error: Route "/client" used \`new Date()\` inside a Client Component without a Suspense boundary above it. See more info here: https://nextjs.org/docs/messages/next-prerender-current-time-client
at Page (webpack:///app/client/page.tsx:4:28)
2 |
3 | export default function Page() {
> 4 | return <p>Current time: {new Date().toISOString()}</p>
| ^
5 | }
6 |
To get a more detailed stack trace and pinpoint the issue, start the app in development mode by running \`next dev\`, then open "/client" in your browser to investigate the error.
Error occurred prerendering page "/client". Read more: https://nextjs.org/docs/messages/prerender-error
Error: Route "/server" used \`Math.random()\` before accessing either uncached data (e.g. \`fetch()\`) or Request data (e.g. \`cookies()\`, \`headers()\`, \`connection()\`, and \`searchParams\`). Accessing random values synchronously in a Server Component requires reading one of these data sources first. Alternatively, consider moving this expression into a Client Component or Cache Component. See more info here: https://nextjs.org/docs/messages/next-prerender-random
at Page (webpack:///app/server/page.tsx:13:27)
11 | await cachedDelay()
12 |
> 13 | return <p>Random: {Math.random()}</p>
| ^
14 | }
15 |
To get a more detailed stack trace and pinpoint the issue, start the app in development mode by running \`next dev\`, then open "/server" in your browser to investigate the error.
Error occurred prerendering page "/server". Read more: https://nextjs.org/docs/messages/prerender-error
> Export encountered errors on following paths: > Export encountered errors on following paths:
/client/page: /client /client/page: /client
/server/page: /server" /server/page: /server"
`) `)
}
}) })
}) })
}) })
...@@ -386,11 +416,7 @@ function getPrerenderOutput(cliOutput: string): string { ...@@ -386,11 +416,7 @@ function getPrerenderOutput(cliOutput: string): string {
if (foundPrerenderingLine && !line.includes('Generating static pages')) { if (foundPrerenderingLine && !line.includes('Generating static pages')) {
lines.push( lines.push(
line line.replace(/at \w+ \(.next[^)]+\)/, 'at x (<next-dist-dir>)')
.replace(/at \w+ \(.next[^)]+\)/, 'at x (<next-dist-dir>)')
// TODO(veil): Bundler protocols should not appear in stackframes.
.replace('webpack:///', 'bundler:///')
.replace('turbopack:///[project]/', 'bundler:///')
) )
} }
} }
......
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