Skip to content

Commit

Permalink
ignore node: for turbopack
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Dec 27, 2024
1 parent a7b0f4d commit aafa027
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,25 @@ export async function batchedTraceSource(
? // TODO(veil): Why are the frames sent encoded?
decodeURIComponent(frame.file)
: undefined

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 sourceFrame = await project.traceSource(frame, currentDirectoryFileUrl)
Expand All @@ -52,18 +69,19 @@ export async function batchedTraceSource(
lineNumber: frame.line ?? 0,
column: frame.column ?? 0,
methodName: frame.methodName ?? '<unknown>',
ignored: shouldIgnorePath(frame.file),
ignored: shouldIgnorePath(file),
arguments: [],
},
source: null,
}
}

let source = null
const originalFile = sourceFrame.originalFile
const originalFile = sourceFrame.originalFile ?? sourceFrame.file

// Don't look up source for node_modules or internals. These can often be large bundled files.
const ignored =
shouldIgnorePath(originalFile ?? sourceFrame.file) ||
shouldIgnorePath(originalFile) ||
// isInternal means resource starts with turbopack://[turbopack]
!!sourceFrame.isInternal
if (originalFile && !ignored) {
Expand Down Expand Up @@ -227,7 +245,11 @@ async function nativeTraceSource(
const sourceIndex = applicableSourceMap.sources.indexOf(
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 = {
Expand Down
37 changes: 7 additions & 30 deletions test/development/errors/node-internal-stack-frame.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { outdent } from 'outdent'

describe('errors - node-internal-stack-frame', () => {
const { next, isTurbopack } = nextTestSetup({
const { next } = nextTestSetup({
files: {
'pages/index.js': outdent`
export default function Page() {}
Expand All @@ -29,35 +29,12 @@ describe('errors - node-internal-stack-frame', () => {
await assertHasRedbox(browser)

const stack = await getStackFramesContent(browser)
if (isTurbopack) {
// FIXME: ignore the next internal frames from node_modules
expect(stack).toMatchInlineSnapshot(`
"at new URL ()
at getServerSideProps (pages/index.js (8:3))
at NextTracerImpl.trace ()
at async doRender ()
at async responseGenerator ()
at async DevServer.renderToResponseWithComponentsImpl ()
at async DevServer.renderPageComponent ()
at async DevServer.renderToResponseImpl ()
at async DevServer.pipeImpl ()
at async NextNodeServer.handleCatchallRenderRequest ()
at async DevServer.handleRequestImpl ()
at async Span.traceAsyncFn ()
at async DevServer.handleRequest ()
at async invokeRender ()
at async handleRequest ()
at async requestHandlerImpl ()
at async Server.requestListener ()"
`)
} else {
expect(stack).toMatchInlineSnapshot(
`"at getServerSideProps (pages/index.js (8:3))"`
)
expect(stack).toMatchInlineSnapshot(
`"at getServerSideProps (pages/index.js (8:3))"`
)

await toggleCollapseCallStackFrames(browser)
const stackCollapsed = await getStackFramesContent(browser)
expect(stackCollapsed).toContain('at new URL ()')
}
await toggleCollapseCallStackFrames(browser)
const stackCollapsed = await getStackFramesContent(browser)
expect(stackCollapsed).toContain('at new URL ()')
})
})

0 comments on commit aafa027

Please sign in to comment.