Unverified Commit 4dd01eea authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[sourcemaps] Properly devirtualize `rsc:` URLs (#81554)

parent fcadb684
...@@ -16,6 +16,7 @@ import { ...@@ -16,6 +16,7 @@ import {
import type { Project, TurbopackStackFrame } from '../../build/swc/types' import type { Project, TurbopackStackFrame } from '../../build/swc/types'
import { import {
type ModernSourceMapPayload, type ModernSourceMapPayload,
devirtualizeReactServerURL,
findApplicableSourceMapPayload, findApplicableSourceMapPayload,
} from '../lib/source-maps' } from '../lib/source-maps'
import { getSourceMapFromFile } from './get-source-map-from-file' import { getSourceMapFromFile } from './get-source-map-from-file'
...@@ -121,13 +122,13 @@ async function batchedTraceSource( ...@@ -121,13 +122,13 @@ async function batchedTraceSource(
source, source,
} }
} }
function parseFile(fileParam: string | null): string | undefined { function parseFile(fileParam: string | null): string | undefined {
if (!fileParam) { if (!fileParam) {
return undefined return undefined
} }
// rsc://React/Server/file://<filename>?42 => file://<filename> return devirtualizeReactServerURL(fileParam)
return fileParam.replace(/^rsc:\/\/React\/[^/]+\//, '').replace(/\?\d+$/, '')
} }
function createStackFrames( function createStackFrames(
...@@ -178,8 +179,7 @@ function createStackFrame( ...@@ -178,8 +179,7 @@ function createStackFrame(
async function nativeTraceSource( async function nativeTraceSource(
frame: TurbopackStackFrame frame: TurbopackStackFrame
): Promise<{ frame: IgnorableStackFrame; source: string | null } | undefined> { ): Promise<{ frame: IgnorableStackFrame; source: string | null } | undefined> {
const sourceURL = // TODO(veil): Why are the frames sent encoded? const sourceURL = frame.file
decodeURIComponent(frame.file)
let sourceMapPayload: ModernSourceMapPayload | undefined let sourceMapPayload: ModernSourceMapPayload | undefined
try { try {
sourceMapPayload = findSourceMap(sourceURL)?.payload sourceMapPayload = findSourceMap(sourceURL)?.payload
......
...@@ -5,6 +5,7 @@ import { SourceMapConsumer } from 'next/dist/compiled/source-map08' ...@@ -5,6 +5,7 @@ import { SourceMapConsumer } from 'next/dist/compiled/source-map08'
import type { StackFrame } from 'next/dist/compiled/stacktrace-parser' import type { StackFrame } from 'next/dist/compiled/stacktrace-parser'
import { getSourceMapFromFile } from './get-source-map-from-file' import { getSourceMapFromFile } from './get-source-map-from-file'
import { import {
devirtualizeReactServerURL,
findApplicableSourceMapPayload, findApplicableSourceMapPayload,
sourceMapIgnoreListsEverything, sourceMapIgnoreListsEverything,
type BasicSourceMapPayload, type BasicSourceMapPayload,
...@@ -298,8 +299,7 @@ async function getSource( ...@@ -298,8 +299,7 @@ async function getSource(
let sourceURL = frame.file ?? '' let sourceURL = frame.file ?? ''
const { getCompilations } = options const { getCompilations } = options
// Rspack is now using file:// URLs for source maps. Remove the rsc prefix to produce the file:/// url. sourceURL = devirtualizeReactServerURL(sourceURL)
sourceURL = sourceURL.replace(/(.*)\/(?=file:\/\/)/, '')
let nativeSourceMap: SourceMap | undefined let nativeSourceMap: SourceMap | undefined
try { try {
...@@ -346,13 +346,9 @@ async function getSource( ...@@ -346,13 +346,9 @@ async function getSource(
} }
// webpack-internal:///./src/hello.tsx => ./src/hello.tsx // webpack-internal:///./src/hello.tsx => ./src/hello.tsx
// rsc://React/Server/webpack-internal:///(rsc)/./src/hello.tsx?42 => (rsc)/./src/hello.tsx
// webpack://_N_E/./src/hello.tsx => ./src/hello.tsx // webpack://_N_E/./src/hello.tsx => ./src/hello.tsx
const moduleId = sourceURL const moduleId = sourceURL
.replace( .replace(/^(webpack-internal:\/\/\/|webpack:\/\/(_N_E\/)?)/, '')
/^(rsc:\/\/React\/[^/]+\/)?(webpack-internal:\/\/\/|webpack:\/\/(_N_E\/)?)/,
''
)
.replace(/\?\d+$/, '') .replace(/\?\d+$/, '')
// (rsc)/./src/hello.tsx => ./src/hello.tsx // (rsc)/./src/hello.tsx => ./src/hello.tsx
......
...@@ -156,3 +156,15 @@ export function filterStackFrameDEV( ...@@ -156,3 +156,15 @@ export function filterStackFrameDEV(
return true return true
} }
} }
export function devirtualizeReactServerURL(sourceURL: string): string {
if (sourceURL.startsWith('rsc://React/')) {
// rsc://React/Server/file://<filename>?42 => file://<filename>
const envIdx = sourceURL.indexOf('/', 'rsc://React/'.length)
const suffixIdx = sourceURL.lastIndexOf('?')
if (envIdx > -1 && suffixIdx > -1) {
return decodeURI(sourceURL.slice(envIdx + 1, suffixIdx))
}
}
return sourceURL
}
...@@ -90,6 +90,8 @@ describe('Dynamic IO Dev Errors', () => { ...@@ -90,6 +90,8 @@ describe('Dynamic IO Dev Errors', () => {
next.cliOutput.slice(outputIndex) next.cliOutput.slice(outputIndex)
).replaceAll(`file:` + next.testDir, '<FIXME-file-protocol>') ).replaceAll(`file:` + next.testDir, '<FIXME-file-protocol>')
// TODO(veil): Source mapping breaks due to double-encoding of the square
// brackets.
expect(normalizedCliOutput).toContain( expect(normalizedCliOutput).toContain(
`\nError: Route "/no-accessed-data": ` + `\nError: Route "/no-accessed-data": ` +
`A component accessed data, headers, params, searchParams, or a short-lived cache without a Suspense boundary nor a "use cache" above it. ` + `A component accessed data, headers, params, searchParams, or a short-lived cache without a Suspense boundary nor a "use cache" above it. ` +
...@@ -101,21 +103,6 @@ describe('Dynamic IO Dev Errors', () => { ...@@ -101,21 +103,6 @@ describe('Dynamic IO Dev Errors', () => {
'\n 3 | return <p>Page</p>' + '\n 3 | return <p>Page</p>' +
'\n 4 | }' '\n 4 | }'
) )
// TODO(veil): Source mapping breaks due to double-encoding of the square
// brackets.
await expect(browser).toDisplayCollapsedRedbox(`
{
"description": "Route "/no-accessed-data": A component accessed data, headers, params, searchParams, or a short-lived cache without a Suspense boundary nor a "use cache" above it. See more info: https://nextjs.org/docs/messages/next-prerender-missing-suspense",
"environmentLabel": "Server",
"label": "Console Error",
"source": null,
"stack": [
"<FIXME-file-protocol>",
"LogSafely <anonymous>",
],
}
`)
} else { } else {
expect(stripAnsi(next.cliOutput.slice(outputIndex))).toContain( expect(stripAnsi(next.cliOutput.slice(outputIndex))).toContain(
`\nError: Route "/no-accessed-data": ` + `\nError: Route "/no-accessed-data": ` +
...@@ -128,22 +115,22 @@ describe('Dynamic IO Dev Errors', () => { ...@@ -128,22 +115,22 @@ describe('Dynamic IO Dev Errors', () => {
'\n 3 | return <p>Page</p>' + '\n 3 | return <p>Page</p>' +
'\n 4 | }' '\n 4 | }'
) )
await expect(browser).toDisplayCollapsedRedbox(`
{
"description": "Route "/no-accessed-data": A component accessed data, headers, params, searchParams, or a short-lived cache without a Suspense boundary nor a "use cache" above it. See more info: https://nextjs.org/docs/messages/next-prerender-missing-suspense",
"environmentLabel": "Server",
"label": "Console Error",
"source": "app/no-accessed-data/page.js (1:31) @ Page
> 1 | export default async function Page() {
| ^",
"stack": [
"Page app/no-accessed-data/page.js (1:31)",
"LogSafely <anonymous>",
],
}
`)
} }
await expect(browser).toDisplayCollapsedRedbox(`
{
"description": "Route "/no-accessed-data": A component accessed data, headers, params, searchParams, or a short-lived cache without a Suspense boundary nor a "use cache" above it. See more info: https://nextjs.org/docs/messages/next-prerender-missing-suspense",
"environmentLabel": "Server",
"label": "Console Error",
"source": "app/no-accessed-data/page.js (1:31) @ Page
> 1 | export default async function Page() {
| ^",
"stack": [
"Page app/no-accessed-data/page.js (1:31)",
"LogSafely <anonymous>",
],
}
`)
}) })
it('should clear segment errors after correcting them', async () => { it('should clear segment errors after correcting them', async () => {
......
...@@ -2,7 +2,7 @@ import { nextTestSetup } from 'e2e-utils' ...@@ -2,7 +2,7 @@ import { nextTestSetup } from 'e2e-utils'
import { assertNoRedbox } from '../../../lib/next-test-utils' import { assertNoRedbox } from '../../../lib/next-test-utils'
describe('use-cache-errors', () => { describe('use-cache-errors', () => {
const { next, isTurbopack } = nextTestSetup({ const { isTurbopack, next } = nextTestSetup({
files: __dirname, files: __dirname,
}) })
const isRspack = Boolean(process.env.NEXT_RSPACK) const isRspack = Boolean(process.env.NEXT_RSPACK)
...@@ -20,18 +20,18 @@ describe('use-cache-errors', () => { ...@@ -20,18 +20,18 @@ describe('use-cache-errors', () => {
await browser.elementById('action-button').click() await browser.elementById('action-button').click()
if (isTurbopack) { if (isTurbopack) {
// TODO(veil): The wrong stack frame is used for the source snippet. // TODO(veil): Inconsistent cursor position
await expect(browser).toDisplayRedbox(` await expect(browser).toDisplayRedbox(`
{ {
"description": "Attempted to call useStuff() from the server but useStuff is on the client. It's not possible to invoke a client function from the server, it can only be rendered as a Component or passed to props of a Client Component.", "description": "Attempted to call useStuff() from the server but useStuff is on the client. It's not possible to invoke a client function from the server, it can only be rendered as a Component or passed to props of a Client Component.",
"environmentLabel": "Cache", "environmentLabel": "Cache",
"label": "Runtime Error", "label": "Runtime Error",
"source": "app/page.tsx (22:10) @ Page "source": "app/module-with-use-cache.ts (16:17) @ useCachedStuff
> 22 | return <OtherClientComponent getCachedStuff={useCachedStuff} /> > 16 | return useStuff()
| ^", | ^",
"stack": [ "stack": [
"<FIXME-file-protocol>", "<FIXME-file-protocol>",
"<FIXME-file-protocol>", "useCachedStuff app/module-with-use-cache.ts (16:17)",
"Page app/page.tsx (22:10)", "Page app/page.tsx (22:10)",
], ],
} }
......
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