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 }>> ...@@ -78,9 +78,9 @@ type NameMappingsByLine = Map<number, Array<{ column: number; name: string }>>
interface SourceMapCacheEntry { interface SourceMapCacheEntry {
map: SyncSourceMapConsumer map: SyncSourceMapConsumer
payload: ModernSourceMapPayload payload: ModernSourceMapPayload
/** Cached name mappings for efficient lookup */ /** Cached name mappings for efficient lookup, the SourceMapConsumer doesn't allow random access */
nameMappings?: NameMappingsByLine 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 generatedSource?: string | null
/** File name (URL) for reading generated source */ /** File name (URL) for reading generated source */
fileName: string fileName: string
...@@ -374,7 +374,9 @@ function getSourcemappedFrameIfPossible( ...@@ -374,7 +374,9 @@ function getSourcemappedFrameIfPossible(
} }
let maybeSourceMapPayload: ModernSourceMapPayload | undefined let maybeSourceMapPayload: ModernSourceMapPayload | undefined
try { try {
console.error('looking up source map for ', sourceURL)
const sourceMap = nativeFindSourceMap(sourceURL) const sourceMap = nativeFindSourceMap(sourceURL)
console.error('\tfound source map')
maybeSourceMapPayload = sourceMap?.payload maybeSourceMapPayload = sourceMap?.payload
} catch (cause) { } catch (cause) {
// We should not log an actual error instance here because that will re-enter // We should not log an actual error instance here because that will re-enter
...@@ -393,10 +395,12 @@ function getSourcemappedFrameIfPossible( ...@@ -393,10 +395,12 @@ function getSourcemappedFrameIfPossible(
// source map. // source map.
return createUnsourcemappedFrame(frame) return createUnsourcemappedFrame(frame)
} }
console.error('1: maybeSourceMapPayload=', maybeSourceMapPayload)
if (maybeSourceMapPayload === undefined) { if (maybeSourceMapPayload === undefined) {
maybeSourceMapPayload = bundlerFindSourceMapPayload(sourceURL) maybeSourceMapPayload = bundlerFindSourceMapPayload(sourceURL)
} }
console.error('2: maybeSourceMapPayload=', maybeSourceMapPayload)
if (maybeSourceMapPayload === undefined) { if (maybeSourceMapPayload === undefined) {
return createUnsourcemappedFrame(frame) return createUnsourcemappedFrame(frame)
} }
...@@ -408,11 +412,13 @@ function getSourcemappedFrameIfPossible( ...@@ -408,11 +412,13 @@ function getSourcemappedFrameIfPossible(
// relative paths but is actually wrong (the chunk and sourcemap have different content hashes). // 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. // We are using the node API to read the sourcemap and it doesn't give us access to the URI.
const sourceMapURL = sourceURL + '.map' const sourceMapURL = sourceURL + '.map'
console.error('about to parse sourcemap for ', sourceURL)
sourceMapConsumer = new SyncSourceMapConsumer( sourceMapConsumer = new SyncSourceMapConsumer(
sourceMapPayload, sourceMapPayload,
// @ts-expect-error: our typings don't include this parameter but it is here. // @ts-expect-error: our typings don't include this parameter but it is here.
sourceMapURL sourceMapURL
) )
console.error('\tparsed source map')
} catch (cause) { } catch (cause) {
// We should not log an actual error instance here because that will re-enter // 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. // this codepath during error inspection and could lead to infinite recursion.
...@@ -492,43 +498,33 @@ function getSourcemappedFrameIfPossible( ...@@ -492,43 +498,33 @@ function getSourcemappedFrameIfPossible(
?.replace('__webpack_exports__.', '') ?.replace('__webpack_exports__.', '')
if (enclosingPosition && sourceMapCacheEntry && methodName) { if (enclosingPosition && sourceMapCacheEntry && methodName) {
// Parse the methodName to extract components for reconstruction // Parse methodName format: [async] [new] [TypeName.]functionName[ [as methodName]]
// Format: [async] [new] [TypeName.]functionName[ [as methodName]] // We need to extract just the functionName part for deobfuscation
let prefix = '' let funcStart = 0
let remaining = methodName let funcEnd = methodName.length
// Extract "async " prefix // Skip "async " prefix
if (remaining.startsWith('async ')) { if (methodName.startsWith('async ')) {
prefix += 'async ' funcStart = 6
remaining = remaining.slice(6) }
} // Skip "new " prefix
// Extract "new " prefix if (methodName.startsWith('new ', funcStart)) {
if (remaining.startsWith('new ')) { funcStart += 4
prefix += 'new ' }
remaining = remaining.slice(4)
} // Find " [as methodName]" suffix
const asIndex = methodName.indexOf(' [as ', funcStart)
// 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 ')
if (asIndex !== -1) { if (asIndex !== -1) {
asSuffix = remaining.slice(asIndex) funcEnd = asIndex
remaining = remaining.slice(0, asIndex)
} }
// Extract TypeName. prefix (e.g., "Object.foo" -> typeName="Object.", functionName="foo") // Find TypeName. prefix (after async/new, before [as])
let typePrefix = '' const dotIndex = methodName.lastIndexOf('.', funcEnd)
const dotIndex = remaining.lastIndexOf('.') // Use lastIndexOf for nested namespaces if (dotIndex !== -1 && dotIndex >= funcStart) {
if (dotIndex !== -1) { funcStart = dotIndex + 1
typePrefix = remaining.slice(0, dotIndex + 1)
remaining = remaining.slice(dotIndex + 1)
} }
// `remaining` is now the raw function name const rawFunctionName = methodName.slice(funcStart, funcEnd)
const rawFunctionName = remaining
const deobfuscatedName = findNameAtPosition( const deobfuscatedName = findNameAtPosition(
getNameMappings(sourceMapCacheEntry), getNameMappings(sourceMapCacheEntry),
...@@ -539,8 +535,11 @@ function getSourcemappedFrameIfPossible( ...@@ -539,8 +535,11 @@ function getSourcemappedFrameIfPossible(
) )
if (deobfuscatedName) { if (deobfuscatedName) {
// Reconstruct the method name with the deobfuscated function name // Replace the function name portion, keeping prefix and suffix
methodName = prefix + typePrefix + deobfuscatedName + asSuffix methodName =
methodName.slice(0, funcStart) +
deobfuscatedName +
methodName.slice(funcEnd)
} }
} }
......
...@@ -397,15 +397,25 @@ describe('app-dir - server source maps', () => { ...@@ -397,15 +397,25 @@ describe('app-dir - server source maps', () => {
'\n' '\n'
) )
} else { } else {
expect(normalizeCliOutput(next.cliOutput.slice(outputIndex))).toContain(
// Node.js is not fine with invalid URLs in vanilla source maps. // 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 // Feel free to adjust these locations. They're just here to showcase
// resolves the invalid source URL. Just check for the key elements. // sourcemapping is broken on invalid sources.
// 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. `\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` +
const cliOutput = normalizeCliOutput(next.cliOutput.slice(outputIndex)) '\nError: bad-sourcemap' +
expect(cliOutput).toContain('Error: bad-sourcemap') '\n at logError (webpack-internal:///(rsc)/./app/bad-sourcemap/page.js:12:19)' +
expect(cliOutput).toContain('at logError') '\n at Page (webpack-internal:///(rsc)/./app/bad-sourcemap/page.js:15:5)'
expect(cliOutput).toContain('at Page') )
// 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 { } else {
// Bundlers silently drop invalid sourcemaps. // 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