Commit 8bc31b72 authored by Luke Sandberg's avatar Luke Sandberg

Fix eval frame source mapping for webpack eval-source-map mode

- Capture methodName separately from functionName for proper V8 format
- Handle eval frames: use evalOrigin as fileName, keep line/col for source mapping
- Add [as methodName] format support (e.g., "Timeout.eval [as _onTimeout]")
- Parse [as methodName] format when falling back to stack string parsing
parent 44b653f4
...@@ -48,8 +48,10 @@ interface CallSite { ...@@ -48,8 +48,10 @@ interface CallSite {
* This preserves the structured data so we don't need to parse stack strings later. * This preserves the structured data so we don't need to parse stack strings later.
*/ */
interface CapturedFrame { interface CapturedFrame {
/** Function name from getFunctionName() or getMethodName() */ /** Function name from getFunctionName() */
functionName: string | undefined functionName: string | undefined
/** Method name from getMethodName() (for [as methodName] format) */
methodName: string | undefined
/** Type name from getTypeName() for qualified names like "Foo.bar" */ /** Type name from getTypeName() for qualified names like "Foo.bar" */
typeName: string | undefined typeName: string | undefined
/** File name from getFileName() */ /** File name from getFileName() */
...@@ -140,17 +142,32 @@ export function parseFrames(stackString: string): CapturedFrame[] { ...@@ -140,17 +142,32 @@ export function parseFrames(stackString: string): CapturedFrame[] {
const parsedFrames = parseStack(stackString) const parsedFrames = parseStack(stackString)
return parsedFrames.map((frame) => { return parsedFrames.map((frame) => {
// Extract "async " and "new " prefixes from method name // Extract "async " and "new " prefixes from method name
let methodName = frame.methodName ?? '' let name = frame.methodName ?? ''
const isAsync = methodName.startsWith('async ') const isAsync = name.startsWith('async ')
if (isAsync) { if (isAsync) {
methodName = methodName.slice(6) name = name.slice(6)
} }
const isConstructor = methodName.startsWith('new ') const isConstructor = name.startsWith('new ')
if (isConstructor) { if (isConstructor) {
methodName = methodName.slice(4) name = name.slice(4)
} }
// Parse "FunctionName [as methodName]" format (e.g., "eval [as _onTimeout]")
let functionName: string | undefined
let methodName: string | undefined
const asMatch = name.match(/^(.+?) [as (.+)]$/)
if (asMatch) {
functionName = asMatch[1] || undefined
methodName = asMatch[2] || undefined
} else {
// Could be just functionName or just methodName - we can't distinguish from parsed stack
functionName = name || undefined
methodName = undefined
}
return { return {
functionName: methodName || undefined, functionName,
methodName,
typeName: undefined, typeName: undefined,
fileName: frame.file ?? undefined, fileName: frame.file ?? undefined,
lineNumber: frame.line1 ?? undefined, lineNumber: frame.line1 ?? undefined,
...@@ -230,23 +247,32 @@ function parseEvalOrigin( ...@@ -230,23 +247,32 @@ function parseEvalOrigin(
*/ */
function captureCallSite(callSite: CallSite): CapturedFrame { function captureCallSite(callSite: CallSite): CapturedFrame {
let fileName = callSite.getFileName() ?? undefined let fileName = callSite.getFileName() ?? undefined
let lineNumber = callSite.getLineNumber() ?? undefined const lineNumber = callSite.getLineNumber() ?? undefined
let columnNumber = callSite.getColumnNumber() ?? undefined const 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.
// This is common in edge runtime with eval-source-map. // When using eval-source-map (common in webpack dev mode), the sourceURL comment in the eval'd code
// causes V8 to set evalOrigin to just the sourceURL (e.g., "webpack-internal:///(rsc)/./app/page.tsx").
// The lineNumber/columnNumber are positions within the eval'd code which will be source-mapped.
// When NOT using sourceURL, evalOrigin has format "eval at <name> (file:line:col)" and we extract the file.
if (fileName === undefined && callSite.isEval()) { if (fileName === undefined && callSite.isEval()) {
const evalOrigin = parseEvalOrigin(callSite.getEvalOrigin()) const evalOrigin = callSite.getEvalOrigin()
if (evalOrigin) { if (evalOrigin) {
fileName = evalOrigin.file // Check if this is a traditional eval origin with (file:line:col) format
lineNumber = evalOrigin.line const parsed = parseEvalOrigin(evalOrigin)
columnNumber = evalOrigin.column if (parsed) {
fileName = parsed.file
// Don't override lineNumber/columnNumber - they're positions in eval code that need source mapping
} else {
// evalOrigin is just the sourceURL from eval-source-map
fileName = evalOrigin
}
} }
} }
return { return {
functionName: functionName: callSite.getFunctionName() ?? undefined,
callSite.getFunctionName() ?? callSite.getMethodName() ?? undefined, methodName: callSite.getMethodName() ?? undefined,
typeName: callSite.getTypeName() ?? undefined, typeName: callSite.getTypeName() ?? undefined,
fileName, fileName,
lineNumber, lineNumber,
...@@ -515,6 +541,7 @@ function resolveFunctionName( ...@@ -515,6 +541,7 @@ function resolveFunctionName(
// Format it with typeName and async/constructor prefixes to match the mangledName format // Format it with typeName and async/constructor prefixes to match the mangledName format
return formatMethodName( return formatMethodName(
foundName, foundName,
capturedFrame.methodName,
capturedFrame.typeName, capturedFrame.typeName,
capturedFrame.isAsync, capturedFrame.isAsync,
capturedFrame.isConstructor capturedFrame.isConstructor
...@@ -549,37 +576,52 @@ interface SourceMappedFrame { ...@@ -549,37 +576,52 @@ interface SourceMappedFrame {
*/ */
function formatMethodName( function formatMethodName(
functionName: string | undefined, functionName: string | undefined,
callSiteMethodName: string | undefined,
typeName: string | undefined, typeName: string | undefined,
isAsync: boolean, isAsync: boolean,
isConstructor: boolean = false isConstructor: boolean = false
): string { ): string {
let methodName: string let result: string
if (functionName) {
// Determine the base name to display
const displayName = functionName ?? callSiteMethodName
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
if (typeName && typeName !== 'global') { if (typeName && typeName !== 'global') {
methodName = typeName + '.' + functionName result = typeName + '.' + displayName
} else { } else {
methodName = functionName result = displayName
}
// Add "[as methodName]" suffix when both functionName and methodName exist
// This matches V8's format for cases like "Timeout.eval [as _onTimeout]"
if (
functionName &&
callSiteMethodName &&
functionName !== callSiteMethodName
) {
result += ' [as ' + callSiteMethodName + ']'
} }
} else { } else {
// Use '<unknown>' for compatibility with stacktrace-parser which uses this // Use '<unknown>' for compatibility with stacktrace-parser which uses this
// string for frames without a method name (e.g., "at (file.js:1:1)") // string for frames without a method name (e.g., "at (file.js:1:1)")
// in the future this should probably be `<anonymous>` // in the future this should probably be `<anonymous>`
methodName = '<unknown>' result = '<unknown>'
} }
// Add "new " prefix for constructor calls (before async) // Add "new " prefix for constructor calls (before async)
if (isConstructor) { if (isConstructor) {
methodName = 'new ' + methodName result = 'new ' + result
} }
// Add "async " prefix for async functions // Add "async " prefix for async functions
if (isAsync) { if (isAsync) {
methodName = 'async ' + methodName result = 'async ' + result
} }
return methodName return result
} }
/** /**
...@@ -589,6 +631,7 @@ function createUnsourcemappedFrame(frame: CapturedFrame): SourceMappedFrame { ...@@ -589,6 +631,7 @@ function createUnsourcemappedFrame(frame: CapturedFrame): SourceMappedFrame {
const file = frame.fileName ?? null const file = frame.fileName ?? null
const methodName = formatMethodName( const methodName = formatMethodName(
frame.functionName, frame.functionName,
frame.methodName,
frame.typeName, frame.typeName,
frame.isAsync, frame.isAsync,
frame.isConstructor frame.isConstructor
...@@ -753,6 +796,7 @@ function getSourcemappedFrameIfPossible( ...@@ -753,6 +796,7 @@ function getSourcemappedFrameIfPossible(
// Compute the full mangled name including typeName and constructor prefix for proper formatting // Compute the full mangled name including typeName and constructor prefix for proper formatting
const mangledName = formatMethodName( const mangledName = formatMethodName(
capturedFrame.functionName, capturedFrame.functionName,
capturedFrame.methodName,
capturedFrame.typeName, capturedFrame.typeName,
capturedFrame.isAsync, capturedFrame.isAsync,
capturedFrame.isConstructor capturedFrame.isConstructor
......
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