Unverified Commit 286c14d0 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

fix: add node internals stack frames to ignored list (#73698)

parent 4c5fa1dc
...@@ -58,11 +58,7 @@ function getOriginalStackFrame( ...@@ -58,11 +58,7 @@ function getOriginalStackFrame(
} }
// TODO: merge this section into ignoredList handling // TODO: merge this section into ignoredList handling
if ( if (source.file === 'file://' || source.file?.match(/https?:\/\//)) {
source.file === 'file://' ||
source.file?.match(/^node:/) ||
source.file?.match(/https?:\/\//)
) {
return Promise.resolve({ return Promise.resolve({
error: false, error: false,
reason: null, reason: null,
......
...@@ -24,7 +24,8 @@ function shouldIgnorePath(modulePath: string): boolean { ...@@ -24,7 +24,8 @@ function shouldIgnorePath(modulePath: string): boolean {
return ( return (
modulePath.includes('node_modules') || modulePath.includes('node_modules') ||
// Only relevant for when Next.js is symlinked e.g. in the Next.js monorepo // Only relevant for when Next.js is symlinked e.g. in the Next.js monorepo
modulePath.includes('next/dist') modulePath.includes('next/dist') ||
modulePath.startsWith('node:')
) )
} }
...@@ -39,8 +40,25 @@ export async function batchedTraceSource( ...@@ -39,8 +40,25 @@ export async function batchedTraceSource(
? // TODO(veil): Why are the frames sent encoded? ? // TODO(veil): Why are the frames sent encoded?
decodeURIComponent(frame.file) decodeURIComponent(frame.file)
: undefined : undefined
if (!file) return if (!file) return
// For node internals they cannot traced the actual source code with project.traceSource,
// we need an early return to indicate it's ignored to avoid the unknown scheme error from `project.traceSource`.
if (file.startsWith('node:')) {
return {
frame: {
file,
lineNumber: frame.line ?? 0,
column: frame.column ?? 0,
methodName: frame.methodName ?? '<unknown>',
ignored: true,
arguments: [],
},
source: null,
}
}
const currentDirectoryFileUrl = pathToFileURL(process.cwd()).href const currentDirectoryFileUrl = pathToFileURL(process.cwd()).href
const sourceFrame = await project.traceSource(frame, currentDirectoryFileUrl) const sourceFrame = await project.traceSource(frame, currentDirectoryFileUrl)
...@@ -51,7 +69,7 @@ export async function batchedTraceSource( ...@@ -51,7 +69,7 @@ export async function batchedTraceSource(
lineNumber: frame.line ?? 0, lineNumber: frame.line ?? 0,
column: frame.column ?? 0, column: frame.column ?? 0,
methodName: frame.methodName ?? '<unknown>', methodName: frame.methodName ?? '<unknown>',
ignored: shouldIgnorePath(frame.file), ignored: shouldIgnorePath(file),
arguments: [], arguments: [],
}, },
source: null, source: null,
...@@ -60,6 +78,7 @@ export async function batchedTraceSource( ...@@ -60,6 +78,7 @@ export async function batchedTraceSource(
let source = null let source = null
const originalFile = sourceFrame.originalFile const originalFile = sourceFrame.originalFile
// 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.
const ignored = const ignored =
shouldIgnorePath(originalFile ?? sourceFrame.file) || shouldIgnorePath(originalFile ?? sourceFrame.file) ||
...@@ -226,7 +245,11 @@ async function nativeTraceSource( ...@@ -226,7 +245,11 @@ async function nativeTraceSource(
const sourceIndex = applicableSourceMap.sources.indexOf( const sourceIndex = applicableSourceMap.sources.indexOf(
originalPosition.source! originalPosition.source!
) )
ignored = applicableSourceMap.ignoreList?.includes(sourceIndex) ?? false ignored =
applicableSourceMap.ignoreList?.includes(sourceIndex) ??
// When sourcemap is not available, fallback to checking `frame.file`.
// e.g. In pages router, nextjs server code is not bundled into the page.
shouldIgnorePath(frame.file)
} }
const originalStackFrame: IgnorableStackFrame = { const originalStackFrame: IgnorableStackFrame = {
......
...@@ -30,7 +30,8 @@ function shouldIgnorePath(modulePath: string): boolean { ...@@ -30,7 +30,8 @@ function shouldIgnorePath(modulePath: string): boolean {
return ( return (
modulePath.includes('node_modules') || modulePath.includes('node_modules') ||
// Only relevant for when Next.js is symlinked e.g. in the Next.js monorepo // Only relevant for when Next.js is symlinked e.g. in the Next.js monorepo
modulePath.includes('next/dist') modulePath.includes('next/dist') ||
modulePath.startsWith('node:')
) )
} }
......
...@@ -3,7 +3,7 @@ import { createSandbox } from 'development-sandbox' ...@@ -3,7 +3,7 @@ import { createSandbox } from 'development-sandbox'
import { FileRef, nextTestSetup } from 'e2e-utils' import { FileRef, nextTestSetup } from 'e2e-utils'
import { import {
describeVariants as describe, describeVariants as describe,
getRedboxCallStack, getStackFramesContent,
toggleCollapseCallStackFrames, toggleCollapseCallStackFrames,
} from 'next-test-utils' } from 'next-test-utils'
import path from 'path' import path from 'path'
...@@ -771,7 +771,8 @@ describe.each(['default', 'turbo'])('ReactRefreshLogBox %s', () => { ...@@ -771,7 +771,8 @@ describe.each(['default', 'turbo'])('ReactRefreshLogBox %s', () => {
expect(callStackFrames.length).toBeGreaterThan(9) expect(callStackFrames.length).toBeGreaterThan(9)
}) })
test('should show anonymous frames in stack trace', async () => { // TODO: hide the anonymous frames between 2 ignored frames
test('should show anonymous frames from stack trace', async () => {
await using sandbox = await createSandbox( await using sandbox = await createSandbox(
next, next,
new Map([ new Map([
...@@ -786,42 +787,49 @@ describe.each(['default', 'turbo'])('ReactRefreshLogBox %s', () => { ...@@ -786,42 +787,49 @@ describe.each(['default', 'turbo'])('ReactRefreshLogBox %s', () => {
], ],
]) ])
) )
const { session, browser } = sandbox const { session, browser } = sandbox
await session.assertHasRedbox() await session.assertHasRedbox()
const texts = await getRedboxCallStack(browser)
expect(texts).toMatchSnapshot() const stack = await getStackFramesContent(browser)
expect(stack).toMatchInlineSnapshot(`
"at Array.map ()
at Page (pages/index.js (2:13))"
`)
}) })
test('should hide unrelated frames in stack trace with node:internal calls', async () => { test('should collapse nodejs internal stack frames from stack trace', async () => {
await using sandbox = await createSandbox( await using sandbox = await createSandbox(
next, next,
new Map([ new Map([
[ [
'pages/index.js', 'pages/index.js',
// Node.js will throw an error about the invalid URL since it happens server-side
outdent` outdent`
export default function Page() {} export default function Page() {}
export function getServerSideProps() { function createURL() {
new URL("/", "invalid"); new URL("/", "invalid")
return { props: {} }; }
}`,
export function getServerSideProps() {
createURL()
return { props: {} }
}`,
], ],
]) ])
) )
const { session, browser } = sandbox const { session, browser } = sandbox
await session.assertHasRedbox() await session.assertHasRedbox()
// Should still show the errored line in source code const stack = await getStackFramesContent(browser)
const source = await session.getRedboxSource() expect(stack).toMatchInlineSnapshot(
expect(source).toContain('pages/index.js') `"at getServerSideProps (pages/index.js (8:3))"`
expect(source).toContain(`new URL("/", "invalid")`)
const callStackFrames = await browser.elementsByCss(
'[data-nextjs-call-stack-frame]'
) )
const texts = await Promise.all(callStackFrames.map((f) => f.innerText()))
expect(texts.filter((t) => t.includes('node:internal'))).toHaveLength(0) await toggleCollapseCallStackFrames(browser)
const stackCollapsed = await getStackFramesContent(browser)
expect(stackCollapsed).toContain('at new URL ()')
}) })
}) })
...@@ -54,13 +54,6 @@ exports[`ReactRefreshLogBox default module init error not shown 1`] = ` ...@@ -54,13 +54,6 @@ exports[`ReactRefreshLogBox default module init error not shown 1`] = `
6 | return <h1>Default Export</h1>;" 6 | return <h1>Default Export</h1>;"
`; `;
exports[`ReactRefreshLogBox default should show anonymous frames in stack trace 1`] = `
"Array.map
<anonymous> (0:0)
Page
pages/index.js (2:13)"
`;
exports[`ReactRefreshLogBox default should strip whitespace correctly with newline 1`] = ` exports[`ReactRefreshLogBox default should strip whitespace correctly with newline 1`] = `
"index.js (8:27) @ onClick "index.js (8:27) @ onClick
...@@ -125,13 +118,6 @@ exports[`ReactRefreshLogBox turbo module init error not shown 1`] = ` ...@@ -125,13 +118,6 @@ exports[`ReactRefreshLogBox turbo module init error not shown 1`] = `
6 | return <h1>Default Export</h1>;" 6 | return <h1>Default Export</h1>;"
`; `;
exports[`ReactRefreshLogBox turbo should show anonymous frames in stack trace 1`] = `
"Array.map
<anonymous> (0:0)
Page
pages/index.js (2:13)"
`;
exports[`ReactRefreshLogBox turbo should strip whitespace correctly with newline 1`] = ` exports[`ReactRefreshLogBox turbo should strip whitespace correctly with newline 1`] = `
"index.js (8:27) @ onClick "index.js (8:27) @ onClick
......
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