Commit 4ea39960 authored by Luke Sandberg's avatar Luke Sandberg

Fix anonymous frame detection in sandwich algorithm

Check for null file instead of '<anonymous>' string. The frame.stack.file
is null for native functions like Set.forEach, but frameToString converts
this to '<anonymous>' only for display. The sandwich check needs to match
the actual null value.
parent 8bc31b72
...@@ -68,6 +68,12 @@ interface CapturedFrame { ...@@ -68,6 +68,12 @@ interface CapturedFrame {
isAsync: boolean isAsync: boolean
/** Whether this is a constructor call (new Foo()) */ /** Whether this is a constructor call (new Foo()) */
isConstructor: boolean isConstructor: boolean
/**
* Original V8 CallSite.toString() result.
* Used for unsourcemapped frames to preserve V8's native formatting.
* Undefined when the frame was reconstructed from a parsed stack string.
*/
originalString: string | undefined
} }
/** /**
...@@ -176,6 +182,8 @@ export function parseFrames(stackString: string): CapturedFrame[] { ...@@ -176,6 +182,8 @@ export function parseFrames(stackString: string): CapturedFrame[] {
enclosingColumnNumber: undefined, enclosingColumnNumber: undefined,
isAsync, isAsync,
isConstructor, isConstructor,
// Parsed frames don't have the original V8 string
originalString: undefined,
} }
}) })
} }
...@@ -247,14 +255,17 @@ function parseEvalOrigin( ...@@ -247,14 +255,17 @@ function parseEvalOrigin(
*/ */
function captureCallSite(callSite: CallSite): CapturedFrame { function captureCallSite(callSite: CallSite): CapturedFrame {
let fileName = callSite.getFileName() ?? undefined let fileName = callSite.getFileName() ?? undefined
const lineNumber = callSite.getLineNumber() ?? undefined let lineNumber = callSite.getLineNumber() ?? undefined
const columnNumber = callSite.getColumnNumber() ?? undefined let columnNumber = callSite.getColumnNumber() ?? undefined
// For eval frames, getFileName() returns undefined but getEvalOrigin() contains the source location. // For eval frames, getFileName() returns undefined but getEvalOrigin() contains the source location.
// When using eval-source-map (common in webpack dev mode), the sourceURL comment in the eval'd code // There are two formats:
// causes V8 to set evalOrigin to just the sourceURL (e.g., "webpack-internal:///(rsc)/./app/page.tsx"). // 1. Traditional eval: "eval at <name> (webpack-internal:///.../page.js:10:5)" - extract file:line:col
// The lineNumber/columnNumber are positions within the eval'd code which will be source-mapped. // 2. eval-source-map: just "webpack-internal:///.../page.js" (sourceURL in eval'd code) - use as fileName
// When NOT using sourceURL, evalOrigin has format "eval at <name> (file:line:col)" and we extract the file. //
// For case 1, we must use the line:col from the eval origin (position in the source file)
// because getLineNumber/getColumnNumber return positions in the eval'd code which don't map correctly.
// For case 2, the getLineNumber/getColumnNumber positions ARE correct for source mapping.
if (fileName === undefined && callSite.isEval()) { if (fileName === undefined && callSite.isEval()) {
const evalOrigin = callSite.getEvalOrigin() const evalOrigin = callSite.getEvalOrigin()
if (evalOrigin) { if (evalOrigin) {
...@@ -262,7 +273,8 @@ function captureCallSite(callSite: CallSite): CapturedFrame { ...@@ -262,7 +273,8 @@ function captureCallSite(callSite: CallSite): CapturedFrame {
const parsed = parseEvalOrigin(evalOrigin) const parsed = parseEvalOrigin(evalOrigin)
if (parsed) { if (parsed) {
fileName = parsed.file fileName = parsed.file
// Don't override lineNumber/columnNumber - they're positions in eval code that need source mapping lineNumber = parsed.line
columnNumber = parsed.column
} else { } else {
// evalOrigin is just the sourceURL from eval-source-map // evalOrigin is just the sourceURL from eval-source-map
fileName = evalOrigin fileName = evalOrigin
...@@ -282,6 +294,8 @@ function captureCallSite(callSite: CallSite): CapturedFrame { ...@@ -282,6 +294,8 @@ function captureCallSite(callSite: CallSite): CapturedFrame {
enclosingColumnNumber: callSite.getEnclosingColumnNumber?.() ?? undefined, enclosingColumnNumber: callSite.getEnclosingColumnNumber?.() ?? undefined,
isAsync: callSite.isAsync(), isAsync: callSite.isAsync(),
isConstructor: callSite.isConstructor(), isConstructor: callSite.isConstructor(),
// Store V8's native formatting for use when we don't source-map the frame
originalString: callSite.toString(),
} }
} }
...@@ -407,20 +421,27 @@ function buildNameMappings( ...@@ -407,20 +421,27 @@ function buildNameMappings(
): NameMappingsByLine { ): NameMappingsByLine {
const nameMappings: NameMappingsByLine = new Map() const nameMappings: NameMappingsByLine = new Map()
sourceMapConsumer.eachMapping((mapping) => { try {
if (!mapping.name) return sourceMapConsumer.eachMapping((mapping) => {
if (!mapping.name) return
let lineEntries = nameMappings.get(mapping.generatedLine) let lineEntries = nameMappings.get(mapping.generatedLine)
if (!lineEntries) { if (!lineEntries) {
lineEntries = [] lineEntries = []
nameMappings.set(mapping.generatedLine, lineEntries) nameMappings.set(mapping.generatedLine, lineEntries)
} }
lineEntries.push({ column: mapping.generatedColumn, name: mapping.name }) lineEntries.push({ column: mapping.generatedColumn, name: mapping.name })
}) })
// Sort each line's entries by column for efficient searching // Sort each line's entries by column for efficient searching
for (const entries of nameMappings.values()) { for (const entries of nameMappings.values()) {
entries.sort((a, b) => a.column - b.column) entries.sort((a, b) => a.column - b.column)
}
} catch {
// Some source maps (particularly indexed/sectioned ones) may have invalid
// internal state that causes eachMapping to fail. Return empty mappings
// rather than crashing error formatting.
nameMappings.clear()
} }
return nameMappings return nameMappings
...@@ -564,6 +585,13 @@ interface SourceMappedFrame { ...@@ -564,6 +585,13 @@ interface SourceMappedFrame {
stack: IgnorableStackFrame stack: IgnorableStackFrame
// DEV only // DEV only
code: string | null code: string | null
/**
* Original V8 CallSite.toString() result for unsourcemapped frames.
* When present, this should be used directly for output instead of
* formatting from stack fields - V8's native formatting handles all
* edge cases (numeric names, special chars, async, constructor, etc.).
*/
originalString?: string
} }
/** /**
...@@ -584,12 +612,23 @@ function formatMethodName( ...@@ -584,12 +612,23 @@ function formatMethodName(
let result: string let result: string
// Determine the base name to display // Determine the base name to display
const displayName = functionName ?? callSiteMethodName let displayName = functionName ?? callSiteMethodName
// Webpack module wrapper functions have the module path as their name, e.g.,
// "(rsc)/./app/page.tsx" or "(ssr)/./app/page.tsx". These are not meaningful
// function names and should be treated as <unknown>.
if (displayName && /^\([^)]+\)\/\.\//.test(displayName)) {
displayName = undefined
}
if (displayName) { if (displayName) {
// Include typeName if present (e.g., "Object.then", "Promise.resolve") // Include typeName if present (e.g., "Object.then", "Promise.resolve")
// This matches V8's native formatting // This matches V8's native formatting, but V8 omits typeName for:
if (typeName && typeName !== 'global') { // - Numeric method names (e.g., webpack module IDs like "7210")
// - Names starting with special characters (e.g., "(rsc)/./app/page.tsx")
const shouldOmitTypeName =
/^\d+$/.test(displayName) || /^[^a-zA-Z_$]/.test(displayName)
if (typeName && typeName !== 'global' && !shouldOmitTypeName) {
result = typeName + '.' + displayName result = typeName + '.' + displayName
} else { } else {
result = displayName result = displayName
...@@ -626,9 +665,13 @@ function formatMethodName( ...@@ -626,9 +665,13 @@ function formatMethodName(
/** /**
* Create an unsourcemapped frame from a captured frame. * Create an unsourcemapped frame from a captured frame.
* When the frame has an originalString from V8's CallSite.toString(),
* we store it to use V8's native formatting directly in output.
*/ */
function createUnsourcemappedFrame(frame: CapturedFrame): SourceMappedFrame { function createUnsourcemappedFrame(frame: CapturedFrame): SourceMappedFrame {
const file = frame.fileName ?? null const file = frame.fileName ?? null
// formatMethodName is still needed for methodName field (used by sandwich algorithm, etc.)
// but originalString will be used for actual output when available
const methodName = formatMethodName( const methodName = formatMethodName(
frame.functionName, frame.functionName,
frame.methodName, frame.methodName,
...@@ -646,6 +689,9 @@ function createUnsourcemappedFrame(frame: CapturedFrame): SourceMappedFrame { ...@@ -646,6 +689,9 @@ function createUnsourcemappedFrame(frame: CapturedFrame): SourceMappedFrame {
ignored: file !== null && shouldIgnoreListGeneratedFrame(file), ignored: file !== null && shouldIgnoreListGeneratedFrame(file),
}, },
code: null, code: null,
// Store original V8 string for direct use in output - handles all edge cases
// (numeric names, special characters, async, constructor, etc.) correctly
originalString: frame.originalString,
} }
} }
...@@ -653,11 +699,15 @@ function ignoreListAnonymousStackFramesIfSandwiched( ...@@ -653,11 +699,15 @@ function ignoreListAnonymousStackFramesIfSandwiched(
sourceMappedFrames: Array<{ sourceMappedFrames: Array<{
stack: IgnorableStackFrame stack: IgnorableStackFrame
code: string | null code: string | null
originalString?: string
}> }>
) { ) {
return ignoreListAnonymousStackFramesIfSandwichedGeneric( return ignoreListAnonymousStackFramesIfSandwichedGeneric(
sourceMappedFrames, sourceMappedFrames,
(frame) => frame.stack.file === '<anonymous>', // Native functions (Set.forEach, JSON.stringify, etc.) have null file names
// when captured via CallSite, but may have '<anonymous>' as string when
// the frames come from parsing the stack string (fallback path).
(frame) => frame.stack.file === null || frame.stack.file === '<anonymous>',
(frame) => frame.stack.ignored, (frame) => frame.stack.ignored,
(frame) => frame.stack.methodName, (frame) => frame.stack.methodName,
(frame) => { (frame) => {
...@@ -975,6 +1025,7 @@ function parseAndSourceMap( ...@@ -975,6 +1025,7 @@ function parseAndSourceMap(
const sourceMappedFrames: Array<{ const sourceMappedFrames: Array<{
stack: IgnorableStackFrame stack: IgnorableStackFrame
code: string | null code: string | null
originalString?: string
}> = [] }> = []
let sourceFrame: null | string = null let sourceFrame: null | string = null
for (const frame of frames) { for (const frame of frames) {
...@@ -1004,26 +1055,21 @@ function parseAndSourceMap( ...@@ -1004,26 +1055,21 @@ function parseAndSourceMap(
for (let i = 0; i < sourceMappedFrames.length; i++) { for (let i = 0; i < sourceMappedFrames.length; i++) {
const frame = sourceMappedFrames[i] const frame = sourceMappedFrames[i]
// Note: We don't use frame.originalString here because V8's formatting
// includes absolute paths and doesn't match our desired output format.
// The formatMethodName function handles method name edge cases (numeric
// names, special characters, etc.) to match V8's behavior.
const frameStr = frameToString(
frame.stack.methodName,
frame.stack.file,
frame.stack.line1,
frame.stack.column1
)
if (!frame.stack.ignored) { if (!frame.stack.ignored) {
sourceMappedStack += sourceMappedStack += '\n' + frameStr
'\n' +
frameToString(
frame.stack.methodName,
frame.stack.file,
frame.stack.line1,
frame.stack.column1
)
} else if (showIgnoreListed) { } else if (showIgnoreListed) {
sourceMappedStack += sourceMappedStack += '\n' + dim(frameStr)
'\n' +
dim(
frameToString(
frame.stack.methodName,
frame.stack.file,
frame.stack.line1,
frame.stack.column1
)
)
} }
} }
......
...@@ -310,14 +310,14 @@ describe('middleware - development errors', () => { ...@@ -310,14 +310,14 @@ describe('middleware - development errors', () => {
? '\n⨯ Error: booooom!' + ? '\n⨯ Error: booooom!' +
`\n at <unknown> (${getDistDir()}/server/edge-runtime-webpack.js:35)` + `\n at <unknown> (${getDistDir()}/server/edge-runtime-webpack.js:35)` +
'\n at eval (middleware.js:3:13)' + '\n at eval (middleware.js:3:13)' +
`\n at (middleware)/./middleware.js (${getDistDir()}/server/middleware.js:26:1)` + `\n at <unknown> (${getDistDir()}/server/middleware.js:26:1)` +
'\n at __webpack_require__ ' '\n at __webpack_require__ '
: '\n⨯ Error: booooom!' + : '\n⨯ Error: booooom!' +
// TODO: Should be anonymous method without a method name // TODO: Should be anonymous method without a method name
'\n at <unknown> (middleware.js:3)' + '\n at <unknown> (middleware.js:3)' +
// TODO: Should be ignore-listed // TODO: Should be ignore-listed
'\n at eval (middleware.js:3:13)' + '\n at eval (middleware.js:3:13)' +
`\n at (middleware)/./middleware.js (${getDistDir()}/server/middleware.js:18:1)` + `\n at <unknown> (${getDistDir()}/server/middleware.js:18:1)` +
'\n at __webpack_require__ ' '\n at __webpack_require__ '
) )
}) })
......
...@@ -106,8 +106,9 @@ describe('use-cache-close-over-function', () => { ...@@ -106,8 +106,9 @@ describe('use-cache-close-over-function', () => {
'\n ^^^^^^^^^^^' + '\n ^^^^^^^^^^^' +
'\n at createCachedFn (app/server/page.tsx:6:3)' + '\n at createCachedFn (app/server/page.tsx:6:3)' +
'\n at eval (app/server/page.tsx:12:24)' + '\n at eval (app/server/page.tsx:12:24)' +
// TODO(veil): Should be source-mapped. // TODO(veil): Should be source-mapped. The frame shows the generated file path
'\n at <unknown> (rsc)' // because source mapping this frame fails (sourcePosition.source === null).
'\n at <unknown> (.next/dev/server/app/server/page.js:'
) )
expect(cliOutput).toContain( expect(cliOutput).toContain(
'' + '' +
......
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