Unverified Commit 81f6cb4b authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

Ensure Issue Overlay sourcemaps externals in Turbopack (#73439)

Closes https://linear.app/vercel/issue/NDX-473/
parent b2219d64
...@@ -14,9 +14,10 @@ import path from 'path' ...@@ -14,9 +14,10 @@ import path from 'path'
import url from 'url' import url from 'url'
import { launchEditor } from '../internal/helpers/launchEditor' import { launchEditor } from '../internal/helpers/launchEditor'
import type { StackFrame } from 'next/dist/compiled/stacktrace-parser' import type { StackFrame } from 'next/dist/compiled/stacktrace-parser'
import { SourceMapConsumer } from 'next/dist/compiled/source-map08'
import type { Project, TurbopackStackFrame } from '../../../../build/swc/types' 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, type SourceMapPayload } from 'node:module'
function shouldIgnorePath(modulePath: string): boolean { function shouldIgnorePath(modulePath: string): boolean {
return ( return (
...@@ -33,7 +34,10 @@ export async function batchedTraceSource( ...@@ -33,7 +34,10 @@ export async function batchedTraceSource(
project: Project, project: Project,
frame: TurbopackStackFrame frame: TurbopackStackFrame
): Promise<{ frame: IgnorableStackFrame; source: string | null } | undefined> { ): Promise<{ frame: IgnorableStackFrame; source: string | null } | undefined> {
const file = frame.file ? decodeURIComponent(frame.file) : undefined const file = frame.file
? // TODO(veil): Why are the frames sent encoded?
decodeURIComponent(frame.file)
: undefined
if (!file) return if (!file) return
const sourceFrame = await project.traceSource(frame) const sourceFrame = await project.traceSource(frame)
...@@ -108,11 +112,155 @@ function createStackFrame(searchParams: URLSearchParams) { ...@@ -108,11 +112,155 @@ function createStackFrame(searchParams: URLSearchParams) {
} satisfies TurbopackStackFrame } satisfies TurbopackStackFrame
} }
/**
* https://tc39.es/source-map/#index-map
*/
interface IndexSourceMapSection {
offset: {
line: number
column: number
}
map: ModernRawSourceMap
}
// TODO(veil): Upstream types
interface IndexSourceMap {
version: number
file: string
sections: IndexSourceMapSection[]
}
interface ModernRawSourceMap extends SourceMapPayload {
ignoreList?: number[]
}
type ModernSourceMapPayload = ModernRawSourceMap | IndexSourceMap
/**
* Finds the sourcemap payload applicable to a given frame.
* Equal to the input unless an Index Source Map is used.
*/
function findApplicableSourceMapPayload(
frame: TurbopackStackFrame,
payload: ModernSourceMapPayload
): ModernRawSourceMap | undefined {
if ('sections' in payload) {
const frameLine = frame.line ?? 0
const frameColumn = frame.column ?? 0
// Sections must not overlap and must be sorted: https://tc39.es/source-map/#section-object
// Therefore the last section that has an offset less than or equal to the frame is the applicable one.
// TODO(veil): Binary search
let section: IndexSourceMapSection | undefined = payload.sections[0]
for (
let i = 0;
i < payload.sections.length &&
payload.sections[i].offset.line <= frameLine &&
payload.sections[i].offset.column <= frameColumn;
i++
) {
section = payload.sections[i]
}
return section === undefined ? undefined : section.map
} else {
return payload
}
}
async function nativeTraceSource(
frame: TurbopackStackFrame
): Promise<{ frame: IgnorableStackFrame; source: string | null } | undefined> {
const sourceMap = findSourceMap(
// TODO(veil): Why are the frames sent encoded?
decodeURIComponent(frame.file)
)
if (sourceMap !== undefined) {
const traced = await SourceMapConsumer.with(
sourceMap.payload,
null,
async (consumer) => {
const originalPosition = consumer.originalPositionFor({
line: frame.line ?? 1,
column: frame.column ?? 1,
})
if (originalPosition.source === null) {
return null
}
const sourceContent: string | null =
consumer.sourceContentFor(
originalPosition.source,
/* returnNullOnMissing */ true
) ?? null
return { originalPosition, sourceContent }
}
)
if (traced !== null) {
const { originalPosition, sourceContent } = traced
const applicableSourceMap = findApplicableSourceMapPayload(
frame,
sourceMap.payload
)
// TODO(veil): Upstream a method to sourcemap consumer that immediately says if a frame is ignored or not.
let ignored = false
if (applicableSourceMap === undefined) {
console.error(
'No applicable source map found in sections for frame',
frame
)
} else {
// TODO: O(n^2). Consider moving `ignoreList` into a Set
const sourceIndex = applicableSourceMap.sources.indexOf(
originalPosition.source!
)
ignored = applicableSourceMap.ignoreList?.includes(sourceIndex) ?? false
}
const originalStackFrame: IgnorableStackFrame = {
methodName:
originalPosition.name ||
// default is not a valid identifier in JS so webpack uses a custom variable when it's an unnamed default export
// Resolve it back to `default` for the method name if the source position didn't have the method.
frame.methodName
?.replace('__WEBPACK_DEFAULT_EXPORT__', 'default')
?.replace('__webpack_exports__.', '') ||
'<unknown>',
column: (originalPosition.column ?? 0) + 1,
file: originalPosition.source?.startsWith('file://')
? path.relative(
process.cwd(),
url.fileURLToPath(originalPosition.source)
)
: originalPosition.source,
lineNumber: originalPosition.line ?? 0,
// TODO: c&p from async createOriginalStackFrame but why not frame.arguments?
arguments: [],
ignored,
}
return {
frame: originalStackFrame,
source: sourceContent,
}
}
}
return undefined
}
export async function createOriginalStackFrame( export async function createOriginalStackFrame(
project: Project, project: Project,
frame: TurbopackStackFrame frame: TurbopackStackFrame
): Promise<OriginalStackFrameResponse | null> { ): Promise<OriginalStackFrameResponse | null> {
const traced = await batchedTraceSource(project, frame) const traced =
(await nativeTraceSource(frame)) ??
// TODO(veil): When would the bundler know more than native?
// If it's faster, try the bundler first and fall back to native later.
(await batchedTraceSource(project, frame))
if (!traced) { if (!traced) {
return null return null
} }
...@@ -193,6 +341,8 @@ export function getSourceMapMiddleware(project: Project) { ...@@ -193,6 +341,8 @@ export function getSourceMapMiddleware(project: Project) {
return badRequest(res) return badRequest(res)
} }
// TODO(veil): Always try the native version first.
// Externals could also be files that aren't bundled via Webpack.
if ( if (
filename.startsWith('webpack://') || filename.startsWith('webpack://') ||
filename.startsWith('webpack-internal:///') filename.startsWith('webpack-internal:///')
......
...@@ -41,16 +41,15 @@ describe('app dir - dynamic error trace', () => { ...@@ -41,16 +41,15 @@ describe('app dir - dynamic error trace', () => {
) )
const codeframe = await getRedboxSource(browser) const codeframe = await getRedboxSource(browser)
// TODO(NDX-115): column for "^"" marker is inconsistent between native, Webpack, and Turbopack
expect(codeframe).toEqual( expect(codeframe).toEqual(
process.env.TURBOPACK process.env.TURBOPACK
? outdent` ? outdent`
app/lib.js (4:12) @ Foo app/lib.js (4:13) @ Foo
2 | 2 |
3 | export function Foo() { 3 | export function Foo() {
> 4 | useHeaders() > 4 | useHeaders()
| ^ | ^
5 | return 'foo' 5 | return 'foo'
6 | } 6 | }
7 | 7 |
......
...@@ -66,12 +66,12 @@ const isOwnerStackEnabled = ...@@ -66,12 +66,12 @@ const isOwnerStackEnabled =
if (process.env.TURBOPACK) { if (process.env.TURBOPACK) {
expect(stackFramesContent).toMatchInlineSnapshot(`""`) expect(stackFramesContent).toMatchInlineSnapshot(`""`)
expect(source).toMatchInlineSnapshot(` expect(source).toMatchInlineSnapshot(`
"app/rsc/page.js (5:10) @ Inner "app/rsc/page.js (5:11) @ Inner
3 | // Intermediate component for testing owner stack 3 | // Intermediate component for testing owner stack
4 | function Inner() { 4 | function Inner() {
> 5 | return <Foo /> > 5 | return <Foo />
| ^ | ^
6 | } 6 | }
7 | 7 |
8 | export default function Page() {" 8 | export default function Page() {"
......
...@@ -70,15 +70,15 @@ const isOwnerStackEnabled = ...@@ -70,15 +70,15 @@ const isOwnerStackEnabled =
if (process.env.TURBOPACK) { if (process.env.TURBOPACK) {
expect(stackFramesContent).toMatchInlineSnapshot( expect(stackFramesContent).toMatchInlineSnapshot(
`"at Page (app/rsc/page.js (11:7))"` `"at Page (app/rsc/page.js (11:8))"`
) )
expect(source).toMatchInlineSnapshot(` expect(source).toMatchInlineSnapshot(`
"app/rsc/page.js (5:10) @ Inner "app/rsc/page.js (5:11) @ Inner
3 | // Intermediate component for testing owner stack 3 | // Intermediate component for testing owner stack
4 | function Inner() { 4 | function Inner() {
> 5 | return <Foo /> > 5 | return <Foo />
| ^ | ^
6 | } 6 | }
7 | 7 |
8 | export default function Page() {" 8 | export default function Page() {"
......
...@@ -29,12 +29,12 @@ const isOwnerStackEnabled = ...@@ -29,12 +29,12 @@ const isOwnerStackEnabled =
`"at Page (app/rsc/page.tsx (6:13))"` `"at Page (app/rsc/page.tsx (6:13))"`
) )
expect(source).toMatchInlineSnapshot(` expect(source).toMatchInlineSnapshot(`
"app/rsc/page.tsx (7:9) @ <anonymous> "app/rsc/page.tsx (7:10) @ <anonymous>
5 | <div> 5 | <div>
6 | {list.map((item, index) => ( 6 | {list.map((item, index) => (
> 7 | <span>{item}</span> > 7 | <span>{item}</span>
| ^ | ^
8 | ))} 8 | ))}
9 | </div> 9 | </div>
10 | )" 10 | )"
......
...@@ -50,24 +50,22 @@ function createContext() { ...@@ -50,24 +50,22 @@ function createContext() {
return ctx return ctx
} }
;(process.env.TURBOPACK_BUILD ? describe.skip : describe)( // TODO(veil): Missing `cause` in Turbopack
'development mode', ;(process.env.TURBOPACK ? describe.skip : describe)('development mode', () => {
() => { const context = createContext()
const context = createContext()
beforeAll(async () => { beforeAll(async () => {
context.appPort = await findPort() context.appPort = await findPort()
context.app = await launchApp(appDir, context.appPort, { context.app = await launchApp(appDir, context.appPort, {
...context.handler, ...context.handler,
env: { __NEXT_TEST_WITH_DEVTOOL: '1' }, env: { __NEXT_TEST_WITH_DEVTOOL: '1' },
})
}) })
})
afterAll(() => killApp(context.app)) afterAll(() => killApp(context.app))
it('logs the error correctly', test(context)) it('logs the error correctly', test(context))
} })
)
;(process.env.TURBOPACK_DEV ? describe.skip : describe)( ;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode', 'production mode',
() => { () => {
......
...@@ -66,9 +66,10 @@ describe('server-side dev errors', () => { ...@@ -66,9 +66,10 @@ describe('server-side dev errors', () => {
const stderrOutput = stripAnsi(stderr.slice(stderrIdx)) const stderrOutput = stripAnsi(stderr.slice(stderrIdx))
if (isTurbopack) { if (isTurbopack) {
// FIXME(veil): Paths include root twice. Bug in generated Turbopack sourcemaps.
expect(stderrOutput).toStartWith( expect(stderrOutput).toStartWith(
' ⨯ test/integration/server-side-dev-errors/pages/gsp.js (6:3) @ getStaticProps' + ' ⨯ ../../test/integration/server-side-dev-errors/test/integration/server-side-dev-errors/pages/gsp.js (6:3) @ getStaticProps' +
'\n ⨯ test/integration/server-side-dev-errors/pages/gsp.js (6:3) @ getStaticProps' '\n ⨯ ../../test/integration/server-side-dev-errors/test/integration/server-side-dev-errors/pages/gsp.js (6:3) @ getStaticProps'
) )
expect(stderrOutput).toContain( expect(stderrOutput).toContain(
' ⨯ ReferenceError: missingVar is not defined' + ' ⨯ ReferenceError: missingVar is not defined' +
...@@ -119,9 +120,10 @@ describe('server-side dev errors', () => { ...@@ -119,9 +120,10 @@ describe('server-side dev errors', () => {
const stderrOutput = stripAnsi(stderr.slice(stderrIdx)) const stderrOutput = stripAnsi(stderr.slice(stderrIdx))
if (isTurbopack) { if (isTurbopack) {
// FIXME(veil): Paths include root twice. Bug in generated Turbopack sourcemaps.
expect(stderrOutput).toStartWith( expect(stderrOutput).toStartWith(
' ⨯ test/integration/server-side-dev-errors/pages/gssp.js (6:3) @ getServerSideProps' + ' ⨯ ../../test/integration/server-side-dev-errors/test/integration/server-side-dev-errors/pages/gssp.js (6:3) @ getServerSideProps' +
'\n ⨯ test/integration/server-side-dev-errors/pages/gssp.js (6:3) @ getServerSideProps' '\n ⨯ ../../test/integration/server-side-dev-errors/test/integration/server-side-dev-errors/pages/gssp.js (6:3) @ getServerSideProps'
) )
expect(stderrOutput).toContain( expect(stderrOutput).toContain(
' ⨯ ReferenceError: missingVar is not defined' + ' ⨯ ReferenceError: missingVar is not defined' +
...@@ -172,9 +174,10 @@ describe('server-side dev errors', () => { ...@@ -172,9 +174,10 @@ describe('server-side dev errors', () => {
const stderrOutput = stripAnsi(stderr.slice(stderrIdx)) const stderrOutput = stripAnsi(stderr.slice(stderrIdx))
if (isTurbopack) { if (isTurbopack) {
// FIXME(veil): Paths include root twice. Bug in generated Turbopack sourcemaps.
expect(stderrOutput).toStartWith( expect(stderrOutput).toStartWith(
' ⨯ test/integration/server-side-dev-errors/pages/blog/[slug].js (6:3) @ getServerSideProps' + ' ⨯ ../../test/integration/server-side-dev-errors/test/integration/server-side-dev-errors/pages/blog/[slug].js (6:3) @ getServerSideProps' +
'\n ⨯ test/integration/server-side-dev-errors/pages/blog/[slug].js (6:3) @ getServerSideProps' '\n ⨯ ../../test/integration/server-side-dev-errors/test/integration/server-side-dev-errors/pages/blog/[slug].js (6:3) @ getServerSideProps'
) )
expect(stderrOutput).toContain( expect(stderrOutput).toContain(
' ⨯ ReferenceError: missingVar is not defined' + ' ⨯ ReferenceError: missingVar is not defined' +
...@@ -225,9 +228,10 @@ describe('server-side dev errors', () => { ...@@ -225,9 +228,10 @@ describe('server-side dev errors', () => {
const stderrOutput = stripAnsi(stderr.slice(stderrIdx)) const stderrOutput = stripAnsi(stderr.slice(stderrIdx))
if (isTurbopack) { if (isTurbopack) {
// FIXME(veil): Paths include root twice. Bug in generated Turbopack sourcemaps.
expect(stderrOutput).toStartWith( expect(stderrOutput).toStartWith(
' ⨯ test/integration/server-side-dev-errors/pages/api/hello.js (2:3) @ handler' + ' ⨯ ../../test/integration/server-side-dev-errors/test/integration/server-side-dev-errors/pages/api/hello.js (2:3) @ handler' +
'\n ⨯ test/integration/server-side-dev-errors/pages/api/hello.js (2:3) @ handler' '\n ⨯ ../../test/integration/server-side-dev-errors/test/integration/server-side-dev-errors/pages/api/hello.js (2:3) @ handler'
) )
expect(stderrOutput).toContain( expect(stderrOutput).toContain(
' ⨯ ReferenceError: missingVar is not defined' + ' ⨯ ReferenceError: missingVar is not defined' +
...@@ -280,9 +284,10 @@ describe('server-side dev errors', () => { ...@@ -280,9 +284,10 @@ describe('server-side dev errors', () => {
// FIXME(veil): error repeated // FIXME(veil): error repeated
// FIXME(veil): codeframe repeated after " ⨯ unhandledRejection: Error: catch this rejection" // FIXME(veil): codeframe repeated after " ⨯ unhandledRejection: Error: catch this rejection"
if (isTurbopack) { if (isTurbopack) {
// FIXME(veil): Paths include root twice. Bug in generated Turbopack sourcemaps.
expect(stderrOutput).toStartWith( expect(stderrOutput).toStartWith(
' ⨯ test/integration/server-side-dev-errors/pages/api/blog/[slug].js (2:3) @ handler' + ' ⨯ ../../test/integration/server-side-dev-errors/test/integration/server-side-dev-errors/pages/api/blog/[slug].js (2:3) @ handler' +
'\n ⨯ test/integration/server-side-dev-errors/pages/api/blog/[slug].js (2:3) @ handler' '\n ⨯ ../../test/integration/server-side-dev-errors/test/integration/server-side-dev-errors/pages/api/blog/[slug].js (2:3) @ handler'
) )
expect(stderrOutput).toContain( expect(stderrOutput).toContain(
'\n ⨯ Error: missingVar is not defined' + '\n ⨯ Error: missingVar is not defined' +
...@@ -328,6 +333,7 @@ describe('server-side dev errors', () => { ...@@ -328,6 +333,7 @@ describe('server-side dev errors', () => {
// FIXME(veil): codeframe repeated after " ⨯ unhandledRejection: Error: catch this rejection" // FIXME(veil): codeframe repeated after " ⨯ unhandledRejection: Error: catch this rejection"
if (isTurbopack) { if (isTurbopack) {
// TODO(veil): digest: undefined should be omitted? // TODO(veil): digest: undefined should be omitted?
// FIXME(veil): Paths include root twice. Bug in generated Turbopack sourcemaps.
expect(stderrOutput).toMatchInlineSnapshot(` expect(stderrOutput).toMatchInlineSnapshot(`
"Error: catch this rejection "Error: catch this rejection
at Timeout._onTimeout (../../test/integration/server-side-dev-errors/test/integration/server-side-dev-errors/pages/uncaught-rejection.js:7:19) at Timeout._onTimeout (../../test/integration/server-side-dev-errors/test/integration/server-side-dev-errors/pages/uncaught-rejection.js:7:19)
...@@ -338,8 +344,8 @@ describe('server-side dev errors', () => { ...@@ -338,8 +344,8 @@ describe('server-side dev errors', () => {
8 | }, 10) 8 | }, 10)
9 | return { 9 | return {
10 | props: {}, 10 | props: {},
⨯ test/integration/server-side-dev-errors/pages/uncaught-rejection.js (7:20) @ Timeout._onTimeout ⨯ ../../test/integration/server-side-dev-errors/test/integration/server-side-dev-errors/pages/uncaught-rejection.js (7:20) @ Timeout._onTimeout
⨯ test/integration/server-side-dev-errors/pages/uncaught-rejection.js (7:20) @ Timeout._onTimeout ⨯ ../../test/integration/server-side-dev-errors/test/integration/server-side-dev-errors/pages/uncaught-rejection.js (7:20) @ Timeout._onTimeout
⨯ unhandledRejection: Error: catch this rejection ⨯ unhandledRejection: Error: catch this rejection
at Timeout._onTimeout (./test/integration/server-side-dev-errors/pages/uncaught-rejection.js:7:20) { at Timeout._onTimeout (./test/integration/server-side-dev-errors/pages/uncaught-rejection.js:7:20) {
digest: undefined digest: undefined
...@@ -438,8 +444,8 @@ describe('server-side dev errors', () => { ...@@ -438,8 +444,8 @@ describe('server-side dev errors', () => {
8 | }, 10) 8 | }, 10)
9 | return { 9 | return {
10 | props: {}, 10 | props: {},
⨯ test/integration/server-side-dev-errors/pages/uncaught-empty-rejection.js (7:20) @ Timeout._onTimeout ⨯ ../../test/integration/server-side-dev-errors/test/integration/server-side-dev-errors/pages/uncaught-empty-rejection.js (7:20) @ Timeout._onTimeout
⨯ test/integration/server-side-dev-errors/pages/uncaught-empty-rejection.js (7:20) @ Timeout._onTimeout ⨯ ../../test/integration/server-side-dev-errors/test/integration/server-side-dev-errors/pages/uncaught-empty-rejection.js (7:20) @ Timeout._onTimeout
⨯ unhandledRejection: Error: ⨯ unhandledRejection: Error:
at Timeout._onTimeout (./test/integration/server-side-dev-errors/pages/uncaught-empty-rejection.js:7:20) { at Timeout._onTimeout (./test/integration/server-side-dev-errors/pages/uncaught-empty-rejection.js:7:20) {
digest: undefined digest: undefined
...@@ -537,8 +543,8 @@ describe('server-side dev errors', () => { ...@@ -537,8 +543,8 @@ describe('server-side dev errors', () => {
8 | }, 10) 8 | }, 10)
9 | return { 9 | return {
10 | props: {}, 10 | props: {},
⨯ test/integration/server-side-dev-errors/pages/uncaught-exception.js (7:11) @ Timeout._onTimeout ⨯ ../../test/integration/server-side-dev-errors/test/integration/server-side-dev-errors/pages/uncaught-exception.js (7:11) @ Timeout._onTimeout
⨯ test/integration/server-side-dev-errors/pages/uncaught-exception.js (7:11) @ Timeout._onTimeout ⨯ ../../test/integration/server-side-dev-errors/test/integration/server-side-dev-errors/pages/uncaught-exception.js (7:11) @ Timeout._onTimeout
⨯ uncaughtException: Error: catch this exception ⨯ uncaughtException: Error: catch this exception
at Timeout._onTimeout (./test/integration/server-side-dev-errors/pages/uncaught-exception.js:7:11) { at Timeout._onTimeout (./test/integration/server-side-dev-errors/pages/uncaught-exception.js:7:11) {
digest: undefined digest: undefined
...@@ -636,8 +642,8 @@ describe('server-side dev errors', () => { ...@@ -636,8 +642,8 @@ describe('server-side dev errors', () => {
8 | }, 10) 8 | }, 10)
9 | return { 9 | return {
10 | props: {}, 10 | props: {},
⨯ test/integration/server-side-dev-errors/pages/uncaught-empty-exception.js (7:11) @ Timeout._onTimeout ⨯ ../../test/integration/server-side-dev-errors/test/integration/server-side-dev-errors/pages/uncaught-empty-exception.js (7:11) @ Timeout._onTimeout
⨯ test/integration/server-side-dev-errors/pages/uncaught-empty-exception.js (7:11) @ Timeout._onTimeout ⨯ ../../test/integration/server-side-dev-errors/test/integration/server-side-dev-errors/pages/uncaught-empty-exception.js (7:11) @ Timeout._onTimeout
⨯ uncaughtException: Error: ⨯ uncaughtException: Error:
at Timeout._onTimeout (./test/integration/server-side-dev-errors/pages/uncaught-empty-exception.js:7:11) { at Timeout._onTimeout (./test/integration/server-side-dev-errors/pages/uncaught-empty-exception.js:7:11) {
digest: undefined digest: undefined
......
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