Unverified Commit 3f4899f7 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Pass down ignored field to error overlay (#72925)

parent 80bbc755
export function shouldIgnorePath(modulePath: string): boolean {
return (
modulePath.includes('node_modules') ||
// Only relevant for when Next.js is symlinked e.g. in the Next.js monorepo
modulePath.includes('next/dist')
)
}
import { bold, cyan, green, red, yellow } from '../../../../lib/picocolors' import { bold, cyan, green, red, yellow } from '../../../../lib/picocolors'
import { SimpleWebpackError } from './simpleWebpackError' import { SimpleWebpackError } from './simpleWebpackError'
import { createOriginalStackFrame } from '../../../../client/components/react-dev-overlay/server/middleware' import {
createOriginalStackFrame,
getIgnoredSources,
} from '../../../../client/components/react-dev-overlay/server/middleware'
import type { webpack } from 'next/dist/compiled/webpack/webpack' import type { webpack } from 'next/dist/compiled/webpack/webpack'
// Based on https://github.com/webpack/webpack/blob/fcdd04a833943394bbb0a9eeb54a962a24cc7e41/lib/stats/DefaultStatsFactoryPlugin.js#L422-L431 // Based on https://github.com/webpack/webpack/blob/fcdd04a833943394bbb0a9eeb54a962a24cc7e41/lib/stats/DefaultStatsFactoryPlugin.js#L422-L431
...@@ -62,6 +65,7 @@ async function getSourceFrame( ...@@ -62,6 +65,7 @@ async function getSourceFrame(
source: { source: {
type: 'bundle', type: 'bundle',
sourceMap, sourceMap,
ignoredSources: getIgnoredSources(sourceMap),
compilation, compilation,
moduleId, moduleId,
modulePath: fileName, modulePath: fileName,
......
...@@ -37,7 +37,7 @@ export const CallStackFrame: React.FC<{ ...@@ -37,7 +37,7 @@ export const CallStackFrame: React.FC<{
return ( return (
<div data-nextjs-call-stack-frame> <div data-nextjs-call-stack-frame>
<h3 data-nextjs-frame-expanded={Boolean(frame.expanded)}> <h3 data-nextjs-frame-expanded={!frame.ignored}>
<HotlinkedText text={formattedMethod} /> <HotlinkedText text={formattedMethod} />
</h3> </h3>
<div <div
......
...@@ -8,34 +8,29 @@ import { GroupedStackFrames } from './GroupedStackFrames' ...@@ -8,34 +8,29 @@ import { GroupedStackFrames } from './GroupedStackFrames'
export type RuntimeErrorProps = { error: ReadyRuntimeError } export type RuntimeErrorProps = { error: ReadyRuntimeError }
export function RuntimeError({ error }: RuntimeErrorProps) { export function RuntimeError({ error }: RuntimeErrorProps) {
const { frames } = error
const { firstFrame, allLeadingFrames, allCallStackFrames } = const { firstFrame, allLeadingFrames, allCallStackFrames } =
React.useMemo(() => { React.useMemo(() => {
const filteredFrames = error.frames const firstFirstPartyFrameIndex = frames.findIndex(
// Filter out nodejs internal frames since you can't do anything about them.
// e.g. node:internal/timers shows up pretty often due to timers, but not helpful to users.
// Only present the last line before nodejs internal trace.
.filter((f) => !f.sourceStackFrame.file?.startsWith('node:'))
const firstFirstPartyFrameIndex = filteredFrames.findIndex(
(entry) => (entry) =>
entry.expanded && !entry.ignored &&
Boolean(entry.originalCodeFrame) && Boolean(entry.originalCodeFrame) &&
Boolean(entry.originalStackFrame) Boolean(entry.originalStackFrame)
) )
return { return {
firstFrame: filteredFrames[firstFirstPartyFrameIndex] ?? null, firstFrame: frames[firstFirstPartyFrameIndex] ?? null,
allLeadingFrames: allLeadingFrames:
firstFirstPartyFrameIndex < 0 firstFirstPartyFrameIndex < 0
? [] ? []
: filteredFrames.slice(0, firstFirstPartyFrameIndex), : frames.slice(0, firstFirstPartyFrameIndex),
allCallStackFrames: filteredFrames.slice(firstFirstPartyFrameIndex + 1), allCallStackFrames: frames.slice(firstFirstPartyFrameIndex + 1),
} }
}, [error.frames]) }, [frames])
const { leadingFramesGroupedByFramework, stackFramesGroupedByFramework } = const { leadingFramesGroupedByFramework, stackFramesGroupedByFramework } =
React.useMemo(() => { React.useMemo(() => {
const leadingFrames = allLeadingFrames.filter((f) => f.expanded) const leadingFrames = allLeadingFrames.filter((f) => !f.ignored)
return { return {
stackFramesGroupedByFramework: stackFramesGroupedByFramework:
......
...@@ -8,7 +8,7 @@ export interface OriginalStackFrame extends OriginalStackFrameResponse { ...@@ -8,7 +8,7 @@ export interface OriginalStackFrame extends OriginalStackFrameResponse {
error: boolean error: boolean
reason: string | null reason: string | null
external: boolean external: boolean
expanded: boolean ignored: boolean
sourceStackFrame: StackFrame sourceStackFrame: StackFrame
} }
...@@ -49,20 +49,15 @@ function getOriginalStackFrame( ...@@ -49,20 +49,15 @@ function getOriginalStackFrame(
error: false, error: false,
reason: null, reason: null,
external: false, external: false,
expanded: !Boolean(
/* collapsed */
(source.file?.includes('node_modules') ||
body.originalStackFrame?.file?.includes('node_modules') ||
body.originalStackFrame?.file?.startsWith('[turbopack]/')) ??
true
),
sourceStackFrame: source, sourceStackFrame: source,
originalStackFrame: body.originalStackFrame, originalStackFrame: body.originalStackFrame,
originalCodeFrame: body.originalCodeFrame || null, originalCodeFrame: body.originalCodeFrame || null,
sourcePackage: body.sourcePackage, sourcePackage: body.sourcePackage,
ignored: body.originalStackFrame?.ignored || false,
} }
} }
// TODO: merge this section into ignoredList handling
if ( if (
source.file === '<anonymous>' || source.file === '<anonymous>' ||
source.file === 'file://' || source.file === 'file://' ||
...@@ -73,11 +68,11 @@ function getOriginalStackFrame( ...@@ -73,11 +68,11 @@ function getOriginalStackFrame(
error: false, error: false,
reason: null, reason: null,
external: true, external: true,
expanded: false,
sourceStackFrame: source, sourceStackFrame: source,
originalStackFrame: null, originalStackFrame: null,
originalCodeFrame: null, originalCodeFrame: null,
sourcePackage: null, sourcePackage: null,
ignored: true,
}) })
} }
...@@ -85,11 +80,11 @@ function getOriginalStackFrame( ...@@ -85,11 +80,11 @@ function getOriginalStackFrame(
error: true, error: true,
reason: err?.message ?? err?.toString() ?? 'Unknown Error', reason: err?.message ?? err?.toString() ?? 'Unknown Error',
external: false, external: false,
expanded: false,
sourceStackFrame: source, sourceStackFrame: source,
originalStackFrame: null, originalStackFrame: null,
originalCodeFrame: null, originalCodeFrame: null,
sourcePackage: null, sourcePackage: null,
ignored: false,
})) }))
} }
......
...@@ -19,11 +19,13 @@ import type { Project, TurbopackStackFrame } from '../../../../build/swc/types' ...@@ -19,11 +19,13 @@ import type { Project, TurbopackStackFrame } from '../../../../build/swc/types'
import { getSourceMapFromFile } from '../internal/helpers/get-source-map-from-file' import { getSourceMapFromFile } from '../internal/helpers/get-source-map-from-file'
import { findSourceMap } from 'node:module' import { findSourceMap } from 'node:module'
type IgnorableStackFrame = StackFrame & { ignored: boolean }
const currentSourcesByFile: Map<string, Promise<string | null>> = new Map() const currentSourcesByFile: Map<string, Promise<string | null>> = new Map()
export async function batchedTraceSource( export async function batchedTraceSource(
project: Project, project: Project,
frame: TurbopackStackFrame frame: TurbopackStackFrame
): Promise<{ frame: StackFrame; source: string | null } | undefined> { ): Promise<{ frame: IgnorableStackFrame; source: string | null } | undefined> {
const file = frame.file ? decodeURIComponent(frame.file) : undefined const file = frame.file ? decodeURIComponent(frame.file) : undefined
if (!file) return if (!file) return
...@@ -31,10 +33,15 @@ export async function batchedTraceSource( ...@@ -31,10 +33,15 @@ export async function batchedTraceSource(
if (!sourceFrame) return if (!sourceFrame) return
let source = null let source = null
let ignored = true
// Don't look up source for node_modules or internals. These can often be large bundled files. // Don't look up source for node_modules or internals. These can often be large bundled files.
if ( if (
sourceFrame.file && sourceFrame.file &&
!(sourceFrame.file.includes('node_modules') || sourceFrame.isInternal) !(
sourceFrame.file.includes('node_modules') ||
// isInternal means resource starts with turbopack://[turbopack]
sourceFrame.isInternal
)
) { ) {
let sourcePromise = currentSourcesByFile.get(sourceFrame.file) let sourcePromise = currentSourcesByFile.get(sourceFrame.file)
if (!sourcePromise) { if (!sourcePromise) {
...@@ -46,18 +53,22 @@ export async function batchedTraceSource( ...@@ -46,18 +53,22 @@ export async function batchedTraceSource(
currentSourcesByFile.delete(sourceFrame.file!) currentSourcesByFile.delete(sourceFrame.file!)
}, 100) }, 100)
} }
ignored = false
source = await sourcePromise source = await sourcePromise
} }
return { // TODO: get ignoredList from turbopack source map
frame: { const ignorableFrame = {
file: sourceFrame.file, file: sourceFrame.file,
lineNumber: sourceFrame.line ?? 0, lineNumber: sourceFrame.line ?? 0,
column: sourceFrame.column ?? 0, column: sourceFrame.column ?? 0,
methodName: sourceFrame.methodName ?? frame.methodName ?? '<unknown>', methodName: sourceFrame.methodName ?? frame.methodName ?? '<unknown>',
ignored,
arguments: [], arguments: [],
}, }
return {
frame: ignorableFrame,
source, source,
} }
} }
......
...@@ -21,18 +21,43 @@ export { getSourceMapFromFile } ...@@ -21,18 +21,43 @@ export { getSourceMapFromFile }
import type { IncomingMessage, ServerResponse } from 'http' import type { IncomingMessage, ServerResponse } from 'http'
import type webpack from 'webpack' import type webpack from 'webpack'
import type { RawSourceMap } from 'next/dist/compiled/source-map08' import type {
NullableMappedPosition,
RawSourceMap,
} from 'next/dist/compiled/source-map08'
import { formatFrameSourceFile } from '../internal/helpers/webpack-module-path' import { formatFrameSourceFile } from '../internal/helpers/webpack-module-path'
import type { MappedPosition } from 'source-map'
function shouldIgnorePath(modulePath: string): boolean {
return (
modulePath.includes('node_modules') ||
// Only relevant for when Next.js is symlinked e.g. in the Next.js monorepo
modulePath.includes('next/dist')
)
}
type IgnoredSources = Array<{ url: string; ignored: boolean }>
export interface IgnorableStackFrame extends StackFrame {
ignored: boolean
}
type SourceAttributes = {
sourcePosition: NullableMappedPosition
sourceContent: string | null
}
type Source = type Source =
| { | {
type: 'file' type: 'file'
sourceMap: RawSourceMap sourceMap: RawSourceMap
ignoredSources: IgnoredSources
modulePath: string modulePath: string
} }
| { | {
type: 'bundle' type: 'bundle'
sourceMap: RawSourceMap sourceMap: RawSourceMap
ignoredSources: IgnoredSources
compilation: webpack.Compilation compilation: webpack.Compilation
moduleId: string moduleId: string
modulePath: string modulePath: string
...@@ -58,7 +83,7 @@ function getSourcePath(source: string) { ...@@ -58,7 +83,7 @@ function getSourcePath(source: string) {
async function findOriginalSourcePositionAndContent( async function findOriginalSourcePositionAndContent(
sourceMap: RawSourceMap, sourceMap: RawSourceMap,
position: { line: number; column: number | null } position: { line: number; column: number | null }
) { ): Promise<SourceAttributes | null> {
const consumer = await new SourceMapConsumer(sourceMap) const consumer = await new SourceMapConsumer(sourceMap)
try { try {
const sourcePosition = consumer.originalPositionFor({ const sourcePosition = consumer.originalPositionFor({
...@@ -85,9 +110,50 @@ async function findOriginalSourcePositionAndContent( ...@@ -85,9 +110,50 @@ async function findOriginalSourcePositionAndContent(
} }
} }
export function getIgnoredSources(sourceMap: RawSourceMap): IgnoredSources {
const ignoreList = new Set<number>()
const moduleFilenames = sourceMap?.sources ?? []
for (let index = 0; index < moduleFilenames.length; index++) {
// bundlerFilePath case: webpack://./app/page.tsx
const bundlerFilePath = moduleFilenames[index]
// Format the path to the normal file path
const formattedFilePath = formatFrameSourceFile(bundlerFilePath)
if (shouldIgnorePath(formattedFilePath)) {
ignoreList.add(index)
}
}
const ignoredSources = sourceMap.sources.map((source, index) => {
return {
url: source,
ignored: ignoreList.has(sourceMap.sources.indexOf(source)),
content: sourceMap.sourcesContent?.[index] ?? null,
}
})
return ignoredSources
}
function isIgnoredSource(
source: Source,
sourcePosition: MappedPosition | NullableMappedPosition
) {
if (sourcePosition.source == null) {
return true
}
for (const ignoredSource of source.ignoredSources) {
if (ignoredSource.ignored && ignoredSource.url === sourcePosition.source) {
return true
}
}
return false
}
function createStackFrame(searchParams: URLSearchParams) { function createStackFrame(searchParams: URLSearchParams) {
const file = searchParams.get('file') as string
return { return {
file: searchParams.get('file') as string, file,
methodName: searchParams.get('methodName') as string, methodName: searchParams.get('methodName') as string,
lineNumber: parseInt(searchParams.get('lineNumber') ?? '0', 10) || 0, lineNumber: parseInt(searchParams.get('lineNumber') ?? '0', 10) || 0,
column: parseInt(searchParams.get('column') ?? '0', 10) || 0, column: parseInt(searchParams.get('column') ?? '0', 10) || 0,
...@@ -99,7 +165,7 @@ function findOriginalSourcePositionAndContentFromCompilation( ...@@ -99,7 +165,7 @@ function findOriginalSourcePositionAndContentFromCompilation(
moduleId: string | undefined, moduleId: string | undefined,
importedModule: string, importedModule: string,
compilation: webpack.Compilation compilation: webpack.Compilation
) { ): SourceAttributes | null {
const module = getModuleById(moduleId, compilation) const module = getModuleById(moduleId, compilation)
return module?.buildInfo?.importLocByPath?.get(importedModule) ?? null return module?.buildInfo?.importLocByPath?.get(importedModule) ?? null
} }
...@@ -136,27 +202,34 @@ export async function createOriginalStackFrame({ ...@@ -136,27 +202,34 @@ export async function createOriginalStackFrame({
}) })
})() })()
if (!result?.sourcePosition.source) { if (!result) {
return null return null
} }
const { sourcePosition, sourceContent } = result const { sourcePosition, sourceContent } = result
const filePath = path.resolve( if (!sourcePosition.source) {
rootDirectory, return null
getSourcePath( }
const ignored =
isIgnoredSource(source, sourcePosition) ||
// If the source file is externals, should be excluded even it's not ignored source.
// e.g. webpack://next/dist/.. needs to be ignored
shouldIgnorePath(source.modulePath)
const sourcePath = getSourcePath(
// When sourcePosition.source is the loader path the modulePath is generally better. // When sourcePosition.source is the loader path the modulePath is generally better.
(sourcePosition.source.includes('|') (sourcePosition.source!.includes('|')
? source.modulePath ? source.modulePath
: sourcePosition.source) || source.modulePath : sourcePosition.source) || source.modulePath
) )
) const filePath = path.resolve(rootDirectory, sourcePath)
const resolvedFilePath = sourceContent const resolvedFilePath = sourceContent
? path.relative(rootDirectory, filePath) ? path.relative(rootDirectory, filePath)
: sourcePosition.source : sourcePosition.source
const traced = { const traced: IgnorableStackFrame = {
file: resolvedFilePath, file: resolvedFilePath,
lineNumber: sourcePosition.line, lineNumber: sourcePosition.line,
column: (sourcePosition.column ?? 0) + 1, column: (sourcePosition.column ?? 0) + 1,
...@@ -168,7 +241,8 @@ export async function createOriginalStackFrame({ ...@@ -168,7 +241,8 @@ export async function createOriginalStackFrame({
?.replace('__WEBPACK_DEFAULT_EXPORT__', 'default') ?.replace('__WEBPACK_DEFAULT_EXPORT__', 'default')
?.replace('__webpack_exports__.', ''), ?.replace('__webpack_exports__.', ''),
arguments: [], arguments: [],
} satisfies StackFrame ignored,
}
return { return {
originalStackFrame: traced, originalStackFrame: traced,
...@@ -201,7 +275,7 @@ export async function getSourceMapFromCompilation( ...@@ -201,7 +275,7 @@ export async function getSourceMapFromCompilation(
} }
} }
export async function getSource( async function getSource(
filename: string, filename: string,
options: { options: {
getCompilations: () => webpack.Compilation[] getCompilations: () => webpack.Compilation[]
...@@ -215,11 +289,11 @@ export async function getSource( ...@@ -215,11 +289,11 @@ export async function getSource(
if (filename.startsWith('file:')) { if (filename.startsWith('file:')) {
const sourceMap = await getSourceMapFromFile(filename) const sourceMap = await getSourceMapFromFile(filename)
return sourceMap return sourceMap
? { ? {
type: 'file', type: 'file',
sourceMap, sourceMap,
ignoredSources: getIgnoredSources(sourceMap),
modulePath: filename.replace(/^file:\/\//, ''), modulePath: filename.replace(/^file:\/\//, ''),
} }
: undefined : undefined
...@@ -241,9 +315,29 @@ export async function getSource( ...@@ -241,9 +315,29 @@ export async function getSource(
for (const compilation of getCompilations()) { for (const compilation of getCompilations()) {
// TODO: `ignoreList` // TODO: `ignoreList`
const sourceMap = await getSourceMapFromCompilation(moduleId, compilation) const sourceMap = await getSourceMapFromCompilation(moduleId, compilation)
const ignoreList = []
const moduleFilenames = sourceMap?.sources ?? []
for (let index = 0; index < moduleFilenames.length; index++) {
// bundlerFilePath case: webpack://./app/page.tsx
const bundlerFilePath = moduleFilenames[index]
// Format the path to the normal file path
const formattedFilePath = formatFrameSourceFile(bundlerFilePath)
if (shouldIgnorePath(formattedFilePath)) {
ignoreList.push(index)
}
}
if (sourceMap) { if (sourceMap) {
return { type: 'bundle', sourceMap, compilation, moduleId, modulePath } const ignoredSources = getIgnoredSources(sourceMap)
return {
type: 'bundle',
sourceMap,
compilation,
moduleId,
modulePath,
ignoredSources,
}
} }
} }
...@@ -270,24 +364,11 @@ export function getOverlayMiddleware(options: { ...@@ -270,24 +364,11 @@ export function getOverlayMiddleware(options: {
const isEdgeServer = searchParams.get('isEdgeServer') === 'true' const isEdgeServer = searchParams.get('isEdgeServer') === 'true'
const isAppDirectory = searchParams.get('isAppDirectory') === 'true' const isAppDirectory = searchParams.get('isAppDirectory') === 'true'
const frame = createStackFrame(searchParams) const frame = createStackFrame(searchParams)
let sourcePackage = findSourcePackage(frame)
if (
!(
/^(rsc:\/\/React\/[^/]+\/)?(webpack-internal:\/\/\/|(file|webpack):\/\/)/.test(
frame.file
) && frame.lineNumber
)
) {
if (sourcePackage) return json(res, { sourcePackage })
return badRequest(res)
}
const formattedFilePath = formatFrameSourceFile(frame.file) const formattedFilePath = formatFrameSourceFile(frame.file)
const filePath = path.join(rootDirectory, formattedFilePath) const filePath = path.join(rootDirectory, formattedFilePath)
const isNextjsSource = filePath.startsWith(NEXT_PROJECT_ROOT) const isNextjsSource = filePath.startsWith(NEXT_PROJECT_ROOT)
let sourcePackage = findSourcePackage(frame)
let source: Source | undefined let source: Source | undefined
if (isNextjsSource) { if (isNextjsSource) {
......
...@@ -10,7 +10,7 @@ import isInternal, { ...@@ -10,7 +10,7 @@ import isInternal, {
export type SourcePackage = 'react' | 'next' export type SourcePackage = 'react' | 'next'
export interface OriginalStackFrameResponse { export interface OriginalStackFrameResponse {
originalStackFrame?: StackFrame | null originalStackFrame?: (StackFrame & { ignored: boolean }) | null
originalCodeFrame?: string | null originalCodeFrame?: string | null
/** We use this to group frames in the error overlay */ /** We use this to group frames in the error overlay */
sourcePackage?: SourcePackage | null sourcePackage?: SourcePackage | null
......
...@@ -69,6 +69,7 @@ import { ...@@ -69,6 +69,7 @@ import {
getSourceMapFromCompilation, getSourceMapFromCompilation,
getSourceMapFromFile, getSourceMapFromFile,
parseStack, parseStack,
getIgnoredSources,
} from '../../../client/components/react-dev-overlay/server/middleware' } from '../../../client/components/react-dev-overlay/server/middleware'
import { import {
batchedTraceSource, batchedTraceSource,
...@@ -1012,6 +1013,7 @@ async function startWatcher(opts: SetupOpts) { ...@@ -1012,6 +1013,7 @@ async function startWatcher(opts: SetupOpts) {
type: 'bundle', type: 'bundle',
sourceMap, sourceMap,
compilation, compilation,
ignoredSources: getIgnoredSources(sourceMap),
moduleId, moduleId,
modulePath, modulePath,
}, },
......
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