Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
createOriginalStackFrame,
getIgnoredSources,
} from '../../../../server/dev/middleware-webpack'
import isInternal from '../../../../shared/lib/is-internal'
import type { webpack } from 'next/dist/compiled/webpack/webpack'
import type { RawSourceMap } from 'next/dist/compiled/source-map08'

Expand Down Expand Up @@ -89,10 +90,22 @@ async function getSourceFrame(
},
})

if (result === null || result.originalStackFrame === null) {
return {
column1: '',
frame: '',
line1: '',
}
}

const originalStackFrame = result.originalStackFrame
return {
frame: result?.originalCodeFrame ?? '',
line1: result?.originalStackFrame?.line1?.toString() ?? '',
column1: result?.originalStackFrame?.column1?.toString() ?? '',
frame:
(!isInternal(originalStackFrame.file)
? result.originalCodeFrame
: null) ?? '',
line1: originalStackFrame.line1?.toString() ?? '',
column1: originalStackFrame.column1?.toString() ?? '',
}
}
} catch {}
Expand Down
3 changes: 1 addition & 2 deletions packages/next/src/next-devtools/server/shared.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { codeFrameColumns } from 'next/dist/compiled/babel/code-frame'
import isInternal from '../../shared/lib/is-internal'
import type { StackFrame } from '../../server/lib/parse-stack'
import { ignoreListAnonymousStackFramesIfSandwiched as ignoreListAnonymousStackFramesIfSandwichedGeneric } from '../../server/lib/source-maps'

Expand Down Expand Up @@ -68,7 +67,7 @@ export function getOriginalCodeFrame(
source: string | null,
colors: boolean = process.stdout.isTTY
): string | null {
if (!source || isInternal(frame.file)) {
if (!source) {
return null
}

Expand Down
34 changes: 27 additions & 7 deletions packages/next/src/server/dev/middleware-turbopack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,16 +307,25 @@ async function createOriginalStackFrame(
)
}

/** undefined = not yet computed */
let originalCodeFrame: string | null | undefined

const tracedFrame = traced.frame
return {
originalStackFrame: {
arguments: traced.frame.arguments,
arguments: tracedFrame.arguments,
file: normalizedStackFrameLocation,
line1: traced.frame.line1,
column1: traced.frame.column1,
ignored: traced.frame.ignored,
methodName: traced.frame.methodName,
line1: tracedFrame.line1,
column1: tracedFrame.column1,
ignored: tracedFrame.ignored,
methodName: tracedFrame.methodName,
},
get originalCodeFrame() {
if (originalCodeFrame === undefined) {
originalCodeFrame = getOriginalCodeFrame(tracedFrame, traced.source)
}
return originalCodeFrame
},
originalCodeFrame: getOriginalCodeFrame(traced.frame, traced.source),
}
}

Expand Down Expand Up @@ -507,7 +516,18 @@ export async function getOriginalStackFrames({
reason: 'Failed to create original stack frame',
}
}
return { status: 'fulfilled', value: stackFrame }
const originalStackFrame = stackFrame.originalStackFrame
return {
status: 'fulfilled',
value: {
originalStackFrame,
originalCodeFrame:
(originalStackFrame?.ignored ?? true)
? null
: // TODO: Don't get all codeframes of non-ignored frames eagerly.
stackFrame.originalCodeFrame,
},
}
} catch (error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't this mean that we try to compute it also when there is no stack frame? probably doesn't mattter but reads a little odd

(originalStackFrame?.ignored ?? true) ? null : stackFrame.originalCodeFrame

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment below for webpack

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor immediately bent over to do it 😄 I wanted to avoid the additional optional chaining + nullish coalescing but also don't feel strongly enough about it to revert 🤷🏻

return {
status: 'rejected',
Expand Down
20 changes: 18 additions & 2 deletions packages/next/src/server/dev/middleware-webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,17 @@ export async function createOriginalStackFrame({
ignored,
}

/** undefined = not yet computed */
let originalCodeFrame: string | null | undefined

return {
originalStackFrame: traced,
originalCodeFrame: getOriginalCodeFrame(traced, sourceContent),
get originalCodeFrame() {
if (originalCodeFrame === undefined) {
originalCodeFrame = getOriginalCodeFrame(traced, sourceContent)
}
return originalCodeFrame
},
}
}

Expand Down Expand Up @@ -530,7 +538,15 @@ async function getOriginalStackFrame({
}
}

return originalStackFrameResponse
const originalStackFrame = originalStackFrameResponse.originalStackFrame
return {
originalStackFrame,
originalCodeFrame:
(originalStackFrame?.ignored ?? true)
? null
: // TODO: Don't get all codeframes of non-ignored frames eagerly.
originalStackFrameResponse.originalCodeFrame,
}
}

export function getOverlayMiddleware(options: {
Expand Down
42 changes: 18 additions & 24 deletions packages/next/src/server/patch-error-inspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,33 +303,27 @@ function getSourcemappedFrameIfPossible(
ignored,
}

/** undefined = not yet computed*/
/** undefined = not yet computed */
let codeFrame: string | null | undefined

return Object.defineProperty(
{
stack: originalFrame,
code: null,
return {
stack: originalFrame,
get code() {
if (codeFrame === undefined) {
const sourceContent: string | null =
sourceMapConsumer.sourceContentFor(
sourcePosition.source,
/* returnNullOnMissing */ true
) ?? null
codeFrame = getOriginalCodeFrame(
originalFrame,
sourceContent,
inspectOptions.colors
)
}
return codeFrame
},
'code',
{
get: () => {
if (codeFrame === undefined) {
const sourceContent: string | null =
sourceMapConsumer.sourceContentFor(
sourcePosition.source,
/* returnNullOnMissing */ true
) ?? null
codeFrame = getOriginalCodeFrame(
originalFrame,
sourceContent,
inspectOptions.colors
)
}
return codeFrame
},
}
)
}
}

function parseAndSourceMap(
Expand Down
Loading