Commit f0fbc02f authored by Luke Sandberg's avatar Luke Sandberg

revert changes to server-source-maps.test.ts

parent cc9c655c
......@@ -78,9 +78,9 @@ type NameMappingsByLine = Map<number, Array<{ column: number; name: string }>>
interface SourceMapCacheEntry {
map: SyncSourceMapConsumer
payload: ModernSourceMapPayload
/** Cached name mappings for efficient lookup */
/** Cached name mappings for efficient lookup, the SourceMapConsumer doesn't allow random access */
nameMappings?: NameMappingsByLine
/** Cached generated source file content for name validation */
/** Cached generated source file content for name validation, if `null` then we failed to read the file. */
generatedSource?: string | null
/** File name (URL) for reading generated source */
fileName: string
......@@ -374,7 +374,9 @@ function getSourcemappedFrameIfPossible(
}
let maybeSourceMapPayload: ModernSourceMapPayload | undefined
try {
console.error('looking up source map for ', sourceURL)
const sourceMap = nativeFindSourceMap(sourceURL)
console.error('\tfound source map')
maybeSourceMapPayload = sourceMap?.payload
} catch (cause) {
// We should not log an actual error instance here because that will re-enter
......@@ -393,10 +395,12 @@ function getSourcemappedFrameIfPossible(
// source map.
return createUnsourcemappedFrame(frame)
}
console.error('1: maybeSourceMapPayload=', maybeSourceMapPayload)
if (maybeSourceMapPayload === undefined) {
maybeSourceMapPayload = bundlerFindSourceMapPayload(sourceURL)
}
console.error('2: maybeSourceMapPayload=', maybeSourceMapPayload)
if (maybeSourceMapPayload === undefined) {
return createUnsourcemappedFrame(frame)
}
......@@ -408,11 +412,13 @@ function getSourcemappedFrameIfPossible(
// 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'
console.error('about to parse sourcemap for ', sourceURL)
sourceMapConsumer = new SyncSourceMapConsumer(
sourceMapPayload,
// @ts-expect-error: our typings don't include this parameter but it is here.
sourceMapURL
)
console.error('\tparsed source map')
} catch (cause) {
// We should not log an actual error instance here because that will re-enter
// this codepath during error inspection and could lead to infinite recursion.
......@@ -492,43 +498,33 @@ function getSourcemappedFrameIfPossible(
?.replace('__webpack_exports__.', '')
if (enclosingPosition && sourceMapCacheEntry && methodName) {
// Parse the methodName to extract components for reconstruction
// Format: [async] [new] [TypeName.]functionName[ [as methodName]]
let prefix = ''
let remaining = methodName
// Extract "async " prefix
if (remaining.startsWith('async ')) {
prefix += 'async '
remaining = remaining.slice(6)
// Parse methodName format: [async] [new] [TypeName.]functionName[ [as methodName]]
// We need to extract just the functionName part for deobfuscation
let funcStart = 0
let funcEnd = methodName.length
// Skip "async " prefix
if (methodName.startsWith('async ')) {
funcStart = 6
}
// Extract "new " prefix
if (remaining.startsWith('new ')) {
prefix += 'new '
remaining = remaining.slice(4)
// Skip "new " prefix
if (methodName.startsWith('new ', funcStart)) {
funcStart += 4
}
// Extract " [as methodName]" suffix
// Note: We keep the [as methodName] suffix unchanged since we can't
// deobfuscate property names - the enclosing position only tells us
// where the function is defined, not where it's accessed
let asSuffix = ''
const asIndex = remaining.indexOf(' [as ')
// Find " [as methodName]" suffix
const asIndex = methodName.indexOf(' [as ', funcStart)
if (asIndex !== -1) {
asSuffix = remaining.slice(asIndex)
remaining = remaining.slice(0, asIndex)
funcEnd = asIndex
}
// Extract TypeName. prefix (e.g., "Object.foo" -> typeName="Object.", functionName="foo")
let typePrefix = ''
const dotIndex = remaining.lastIndexOf('.') // Use lastIndexOf for nested namespaces
if (dotIndex !== -1) {
typePrefix = remaining.slice(0, dotIndex + 1)
remaining = remaining.slice(dotIndex + 1)
// Find TypeName. prefix (after async/new, before [as])
const dotIndex = methodName.lastIndexOf('.', funcEnd)
if (dotIndex !== -1 && dotIndex >= funcStart) {
funcStart = dotIndex + 1
}
// `remaining` is now the raw function name
const rawFunctionName = remaining
const rawFunctionName = methodName.slice(funcStart, funcEnd)
const deobfuscatedName = findNameAtPosition(
getNameMappings(sourceMapCacheEntry),
......@@ -539,8 +535,11 @@ function getSourcemappedFrameIfPossible(
)
if (deobfuscatedName) {
// Reconstruct the method name with the deobfuscated function name
methodName = prefix + typePrefix + deobfuscatedName + asSuffix
// Replace the function name portion, keeping prefix and suffix
methodName =
methodName.slice(0, funcStart) +
deobfuscatedName +
methodName.slice(funcEnd)
}
}
......
......@@ -397,15 +397,25 @@ describe('app-dir - server source maps', () => {
'\n'
)
} else {
// Node.js is not fine with invalid URLs in vanilla source maps.
// The specific paths in the output vary based on how the source map consumer
// resolves the invalid source URL. Just check for the key elements.
// Note: The "Invalid source map" warning may or may not appear depending on
// whether Node's findSourceMap throws or returns a successful-but-invalid result.
const cliOutput = normalizeCliOutput(next.cliOutput.slice(outputIndex))
expect(cliOutput).toContain('Error: bad-sourcemap')
expect(cliOutput).toContain('at logError')
expect(cliOutput).toContain('at Page')
expect(normalizeCliOutput(next.cliOutput.slice(outputIndex))).toContain(
// Node.js is not fine with invalid URLs in vanilla source maps.
// Feel free to adjust these locations. They're just here to showcase
// sourcemapping is broken on invalid sources.
'' +
`\nwebpack-internal:///(rsc)/./app/bad-sourcemap/page.js: Invalid source map. Only conformant source maps can be used to find the original code. Cause: TypeError [ERR_INVALID_ARG_TYPE]: The "payload" argument must be of type object. Received null` +
'\nError: bad-sourcemap' +
'\n at logError (webpack-internal:///(rsc)/./app/bad-sourcemap/page.js:12:19)' +
'\n at Page (webpack-internal:///(rsc)/./app/bad-sourcemap/page.js:15:5)'
)
// Expect the invalid sourcemap warning only once per render.
// Dynamic I/O renders three times.
// One from filterStackFrameDEV.
// One from findSourceMapURLDEV.
expect(
normalizeCliOutput(next.cliOutput.slice(outputIndex)).split(
'Invalid source map.'
).length - 1
).toEqual(3)
}
} else {
// Bundlers silently drop invalid sourcemaps.
......
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