Skip to content

Commit 4bbd463

Browse files
committed
[sourcemaps] Ignore-list sources whose sourcemaps ignore-list everything
1 parent c5f3478 commit 4bbd463

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

packages/next/src/build/webpack/plugins/wellknown-errors-plugin/parseNotFoundError.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
getIgnoredSources,
66
} from '../../../../server/dev/middleware-webpack'
77
import type { webpack } from 'next/dist/compiled/webpack/webpack'
8+
import type { RawSourceMap } from 'next/dist/compiled/source-map08'
89

910
// Based on https://github.com/webpack/webpack/blob/fcdd04a833943394bbb0a9eeb54a962a24cc7e41/lib/stats/DefaultStatsFactoryPlugin.js#L422-L431
1011
/*
@@ -46,6 +47,12 @@ function getModuleTrace(input: any, compilation: any) {
4647
return moduleTrace
4748
}
4849

50+
function sourceMapIgnoreListsEverything(
51+
sourceMap: RawSourceMap & { ignoreList?: number[] }
52+
): boolean {
53+
return sourceMap.sources.length === sourceMap.ignoreList?.length
54+
}
55+
4956
async function getSourceFrame(
5057
input: any,
5158
fileName: any,
@@ -62,6 +69,8 @@ async function getSourceFrame(
6269
const moduleId = compilation.chunkGraph.getModuleId(module)
6370

6471
const result = await createOriginalStackFrame({
72+
// TODO
73+
ignoredByDefault: sourceMapIgnoreListsEverything(sourceMap),
6574
source: {
6675
type: 'bundle',
6776
sourceMap,

packages/next/src/server/dev/middleware-webpack.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,13 @@ async function findOriginalSourcePositionAndContent(
9494
try {
9595
consumer = await new SourceMapConsumer(sourceMap)
9696
} catch (cause) {
97-
throw new Error(
98-
`${sourceMap.file}: Invalid source map. Only conformant source maps can be used to find the original code.`,
99-
{ cause }
97+
console.error(
98+
new Error(
99+
`${sourceMap.file}: Invalid source map. Only conformant source maps can be used to find the original code.`,
100+
{ cause }
101+
)
100102
)
103+
return null
101104
}
102105

103106
try {
@@ -178,11 +181,14 @@ function findOriginalSourcePositionAndContentFromCompilation(
178181
}
179182

180183
export async function createOriginalStackFrame({
184+
ignoredByDefault,
181185
source,
182186
rootDirectory,
183187
frame,
184188
errorMessage,
185189
}: {
190+
/** setting this to true will not consult ignoreList */
191+
ignoredByDefault: boolean
186192
source: Source
187193
rootDirectory: string
188194
frame: StackFrame
@@ -214,6 +220,7 @@ export async function createOriginalStackFrame({
214220
}
215221

216222
const ignored =
223+
ignoredByDefault ||
217224
isIgnoredSource(source, sourcePosition) ||
218225
// If the source file is externals, should be excluded even it's not ignored source.
219226
// e.g. webpack://next/dist/.. needs to be ignored
@@ -404,6 +411,12 @@ function getOriginalStackFrames({
404411
)
405412
}
406413

414+
function sourceMapIgnoreListsEverything(
415+
sourceMap: RawSourceMap & { ignoreList?: number[] }
416+
): boolean {
417+
return sourceMap.sources.length === sourceMap.ignoreList?.length
418+
}
419+
407420
async function getOriginalStackFrame({
408421
isServer,
409422
isEdgeServer,
@@ -494,8 +507,10 @@ async function getOriginalStackFrame({
494507
originalCodeFrame: null,
495508
}
496509
}
510+
defaultStackFrame.ignored = sourceMapIgnoreListsEverything(source.sourceMap)
497511

498512
const originalStackFrameResponse = await createOriginalStackFrame({
513+
ignoredByDefault: defaultStackFrame.ignored,
499514
frame,
500515
source,
501516
rootDirectory,

packages/next/src/server/patch-error-inspect.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,19 @@ function createUnsourcemappedFrame(
173173
}
174174
}
175175

176+
function sourceMapIgnoreListsEverything(
177+
sourceMap: ModernSourceMapPayload
178+
): boolean {
179+
if ('sections' in sourceMap) {
180+
// If sections are present, the ignoreList is not used.
181+
// This is because sections are used to ignore-list everything.
182+
return sourceMap.sections.every((section) => {
183+
return sourceMapIgnoreListsEverything(section.map)
184+
})
185+
}
186+
return sourceMap.sources.length === sourceMap.ignoreList?.length
187+
}
188+
176189
/**
177190
* @param frame
178191
* @param sourceMapCache
@@ -261,6 +274,7 @@ function getSourcemappedFrameIfPossible(
261274
line: frame.lineNumber ?? 1,
262275
})
263276

277+
let ignored = sourceMapIgnoreListsEverything(sourceMapPayload)
264278
if (sourcePosition.source === null) {
265279
return {
266280
stack: {
@@ -269,7 +283,7 @@ function getSourcemappedFrameIfPossible(
269283
file: frame.file,
270284
lineNumber: frame.lineNumber,
271285
methodName: frame.methodName,
272-
ignored: shouldIgnoreListGeneratedFrame(frame.file),
286+
ignored: ignored || shouldIgnoreListGeneratedFrame(frame.file),
273287
},
274288
code: null,
275289
}
@@ -280,18 +294,17 @@ function getSourcemappedFrameIfPossible(
280294
sourceMapPayload
281295
)
282296
// TODO(veil): Upstream a method to sourcemap consumer that immediately says if a frame is ignored or not.
283-
let ignored = false
284297
if (applicableSourceMap === undefined) {
285298
console.error('No applicable source map found in sections for frame', frame)
286-
} else if (shouldIgnoreListOriginalFrame(sourcePosition.source)) {
299+
} else if (!ignored && shouldIgnoreListOriginalFrame(sourcePosition.source)) {
287300
// Externals may be libraries that don't ship ignoreLists.
288301
// This is really taking control away from libraries.
289302
// They should still ship `ignoreList` so that attached debuggers ignore-list their frames.
290303
// TODO: Maybe only ignore library sourcemaps if `ignoreList` is absent?
291304
// Though keep in mind that Turbopack omits empty `ignoreList`.
292305
// So if we establish this convention, we should communicate it to the ecosystem.
293306
ignored = true
294-
} else {
307+
} else if (!ignored) {
295308
// TODO: O(n^2). Consider moving `ignoreList` into a Set
296309
const sourceIndex = applicableSourceMap.sources.indexOf(
297310
sourcePosition.source

test/e2e/app-dir/server-source-maps/server-source-maps.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,10 +506,8 @@ describe('app-dir - server source maps', () => {
506506
"stack": [
507507
"eval app/module-evaluation/module.js (1:22)",
508508
"<FIXME-file-protocol>",
509-
"<FIXME-file-protocol>",
510509
"eval rsc:/Prerender/webpack-internal:///(rsc)/app/module-evaluation/page.js (5:65)",
511510
"<FIXME-file-protocol>",
512-
"<FIXME-file-protocol>",
513511
"Function.all <anonymous>",
514512
"Function.all <anonymous>",
515513
"Page <anonymous>",

0 commit comments

Comments
 (0)