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 {
import type { Project, TurbopackStackFrame } from '../../build/swc/types'
import {
type ModernSourceMapPayload,
devirtualizeReactServerURL,
findApplicableSourceMapPayload,
} from '../lib/source-maps'
import { getSourceMapFromFile } from './get-source-map-from-file'
......@@ -121,13 +122,13 @@ async function batchedTraceSource(
source,
}
}
function parseFile(fileParam: string | null): string | undefined {
if (!fileParam) {
return undefined
}
// rsc://React/Server/file://<filename>?42 => file://<filename>
return fileParam.replace(/^rsc:\/\/React\/[^/]+\//, '').replace(/\?\d+$/, '')
return devirtualizeReactServerURL(fileParam)
}
function createStackFrames(
......@@ -178,8 +179,7 @@ function createStackFrame(
async function nativeTraceSource(
frame: TurbopackStackFrame
): Promise<{ frame: IgnorableStackFrame; source: string | null } | undefined> {
const sourceURL = // TODO(veil): Why are the frames sent encoded?
decodeURIComponent(frame.file)
const sourceURL = frame.file
let sourceMapPayload: ModernSourceMapPayload | undefined
try {
sourceMapPayload = findSourceMap(sourceURL)?.payload
......
......@@ -5,6 +5,7 @@ import { SourceMapConsumer } from 'next/dist/compiled/source-map08'
import type { StackFrame } from 'next/dist/compiled/stacktrace-parser'
import { getSourceMapFromFile } from './get-source-map-from-file'
import {
devirtualizeReactServerURL,
findApplicableSourceMapPayload,
sourceMapIgnoreListsEverything,
type BasicSourceMapPayload,
......@@ -298,8 +299,7 @@ async function getSource(
let sourceURL = frame.file ?? ''
const { getCompilations } = options
// Rspack is now using file:// URLs for source maps. Remove the rsc prefix to produce the file:/// url.
sourceURL = sourceURL.replace(/(.*)\/(?=file:\/\/)/, '')
sourceURL = devirtualizeReactServerURL(sourceURL)
let nativeSourceMap: SourceMap | undefined
try {
......@@ -346,13 +346,9 @@ async function getSource(
}
// 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
const moduleId = sourceURL
.replace(
/^(rsc:\/\/React\/[^/]+\/)?(webpack-internal:\/\/\/|webpack:\/\/(_N_E\/)?)/,
''
)
.replace(/^(webpack-internal:\/\/\/|webpack:\/\/(_N_E\/)?)/, '')
.replace(/\?\d+$/, '')
// (rsc)/./src/hello.tsx => ./src/hello.tsx
......
......@@ -156,3 +156,15 @@ export function filterStackFrameDEV(
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', () => {
next.cliOutput.slice(outputIndex)
).replaceAll(`file:` + next.testDir, '<FIXME-file-protocol>')
// TODO(veil): Source mapping breaks due to double-encoding of the square
// brackets.
expect(normalizedCliOutput).toContain(
`\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. ` +
......@@ -101,21 +103,6 @@ describe('Dynamic IO Dev Errors', () => {
'\n 3 | return <p>Page</p>' +
'\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 {
expect(stripAnsi(next.cliOutput.slice(outputIndex))).toContain(
`\nError: Route "/no-accessed-data": ` +
......@@ -128,22 +115,22 @@ describe('Dynamic IO Dev Errors', () => {
'\n 3 | return <p>Page</p>' +
'\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 () => {
......
......@@ -2,7 +2,7 @@ import { nextTestSetup } from 'e2e-utils'
import { assertNoRedbox } from '../../../lib/next-test-utils'
describe('use-cache-errors', () => {
const { next, isTurbopack } = nextTestSetup({
const { isTurbopack, next } = nextTestSetup({
files: __dirname,
})
const isRspack = Boolean(process.env.NEXT_RSPACK)
......@@ -20,18 +20,18 @@ describe('use-cache-errors', () => {
await browser.elementById('action-button').click()
if (isTurbopack) {
// TODO(veil): The wrong stack frame is used for the source snippet.
// TODO(veil): Inconsistent cursor position
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.",
"environmentLabel": "Cache",
"label": "Runtime Error",
"source": "app/page.tsx (22:10) @ Page
> 22 | return <OtherClientComponent getCachedStuff={useCachedStuff} />
| ^",
"source": "app/module-with-use-cache.ts (16:17) @ useCachedStuff
> 16 | return useStuff()
| ^",
"stack": [
"<FIXME-file-protocol>",
"<FIXME-file-protocol>",
"useCachedStuff app/module-with-use-cache.ts (16:17)",
"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