Unverified Commit d8bfc3c9 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

Don't parse error message lines as stack frames in the server error inspector (#99888)

## What

Fix formatting of `Errors` containing multiline `message` properties

Multi-line error messages printed in the terminal could end with bogus
stack frames such as:

```
    at <unknown> (Turbopack is configured to run webpack loaders and PostCSS transforms in ...)
    at <unknown> (https://nextjs.org/docs/app/api-reference/config/next-config-js/turbopackPluginRuntimeStrategy)
```

`parseAndSourceMap` in `patch-error-inspect.ts` passed the whole of
`error.stack` to the stack parser, and `error.stack` starts with the
message. `stacktrace-parser`'s Gecko pattern accepts any line that
*starts with* a scheme-like word (`http`, `file`, `webpack`,
`turbopack`, `about`, `rsc`, ...), case-insensitively and without
requiring a colon. So message lines such as a docs URL, or prose that
happens to start with "Turbopack", were treated as frames.

The fix is to only pass the 'stack frame' part of th parses the part of
the stack after the message:
- `prepareUnsourcemappedStackTrace`, which formats every stack, records
the offset of the first frame on the error under a non-enumerable
`Symbol.for` key, together with the string it produced. If `error.stack`
is still that string, the frames are found exactly, even if the message
or name changed afterwards.
- For stacks we didn't format, or that were replaced afterwards, it
strips the `name: message` header if `error.stack` starts with it, and
otherwise parses the whole stack as before.


<!-- NEXT_JS_LLM -->
parent ef247bc8
......@@ -164,6 +164,15 @@ function computeErrorName(error: Error): string {
return error.name || 'Error'
}
// Records where the frames start in a stack produced by
// `prepareUnsourcemappedStackTrace`, so they can be parsed without having to
// guess where the message ends. Uses `Symbol.for` since this module is bundled
// into several runtimes, and any copy may have formatted a given stack.
const STACK_FRAMES_START = Symbol.for('next.dev.stackFramesStart')
type ErrorWithStackFramesStart = Error & {
[STACK_FRAMES_START]?: { stack: string; framesStart: number }
}
function prepareUnsourcemappedStackTrace(
error: Error,
structuredStackTrace: any[]
......@@ -171,9 +180,47 @@ function prepareUnsourcemappedStackTrace(
const name = computeErrorName(error)
const message = error.message || ''
let stack = name + ': ' + message
const framesStart = stack.length
for (let i = 0; i < structuredStackTrace.length; i++) {
stack += '\n at ' + structuredStackTrace[i].toString()
}
// Record where the real stack frames start
try {
// Non-enumerable so that it doesn't show up when inspecting the error.
Object.defineProperty(error, STACK_FRAMES_START, {
value: { stack, framesStart },
configurable: true,
writable: true,
})
} catch {
// The error may be frozen. We'll fall back to finding the frames by
// matching the message.
}
return stack
}
/**
* Returns the part of `error.stack` after the message, so the stack parser
* can't mistake message lines for frames (e.g. a line starting with a URL or
* with "webpack"/"turbopack").
*/
function getUnparsedStackFrames(error: Error, errorName: string): string {
let stack = String(error.stack)
const recorded = (error as ErrorWithStackFramesStart)[STACK_FRAMES_START]
// `error.stack` may have been replaced after we formatted it.
if (recorded !== undefined && recorded.stack === stack) {
return stack.slice(recorded.framesStart)
}
// Errors printing by `prepareUnsourcemappedStackTrace` are `Error: <message>` so attempt to strip that prefix
if (stack.startsWith(errorName)) {
stack = stack.slice(errorName.length)
}
if (stack.startsWith(': ')) {
stack = stack.slice(': '.length)
}
if (stack.startsWith(error.message)) {
stack = stack.slice(error.message.length)
}
return stack
}
......@@ -435,12 +482,12 @@ function parseAndSourceMap(
inspectOptions: util.InspectOptions
): string {
const showIgnoreListed = process.env.__NEXT_SHOW_IGNORE_LISTED === 'true'
// We overwrote Error.prepareStackTrace earlier so error.stack is not sourcemapped.
let unparsedStack = String(error.stack)
// We could just read it from `error.stack`.
// This works around cases where a 3rd party `Error.prepareStackTrace` implementation
// doesn't implement the name computation correctly.
const errorName = computeErrorName(error)
// We overwrote Error.prepareStackTrace earlier so error.stack is not sourcemapped.
let unparsedStack = getUnparsedStackFrames(error, errorName)
let idx = unparsedStack.indexOf('react_stack_bottom_frame')
if (idx !== -1) {
......
......@@ -37,7 +37,7 @@ describe('proxy-runtime', () => {
4 |
The exported configuration object in a source file needs to have a very specific format from which some properties can be statically parsed at compiled-time.
https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config
at <unknown> (https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config)"
at ignore-listed frames"
`)
} else {
expect(cliOutput).toContain(
......
......@@ -59,5 +59,7 @@ describe('turbopack css node process connect failure', () => {
// This is not a Turbopack bug, so don't present it as one.
expect(cliOutput).not.toContain('TurbopackInternalError')
expect(cliOutput).not.toContain('https://bugs.nextjs.org/')
// Lines of the message must not be mistaken for stack frames.
expect(cliOutput).not.toContain('at <unknown>')
})
})
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