Skip to content

Commit

Permalink
Shorten unsourcemapped absolute locations in terminal stacktraces
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Nov 14, 2024
1 parent 52f4099 commit 40b0503
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions packages/next/src/server/patch-error-inspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,26 @@ function frameToString(frame: StackFrame): string {
sourceLocation += `:${frame.column}`
}

const filePath =
let fileLocation: string | null
if (
frame.file !== null &&
frame.file.startsWith('file://') &&
URL.canParse(frame.file)
? // If not relative to CWD, the path is ambiguous to IDEs and clicking will prompt to select the file first.
// In a multi-app repo, this leads to potentially larger file names but will make clicking snappy.
// There's no tradeoff for the cases where `dir` in `next dev [dir]` is omitted
// since relative to cwd is both the shortest and snappiest.
path.relative(process.cwd(), url.fileURLToPath(frame.file))
: frame.file
) {
// If not relative to CWD, the path is ambiguous to IDEs and clicking will prompt to select the file first.
// In a multi-app repo, this leads to potentially larger file names but will make clicking snappy.
// There's no tradeoff for the cases where `dir` in `next dev [dir]` is omitted
// since relative to cwd is both the shortest and snappiest.
fileLocation = path.relative(process.cwd(), url.fileURLToPath(frame.file))
} else if (frame.file !== null && frame.file.startsWith('/')) {
fileLocation = path.relative(process.cwd(), frame.file)
} else {
fileLocation = frame.file
}

return frame.methodName
? ` at ${frame.methodName} (${filePath}${sourceLocation})`
: ` at ${filePath}${frame.lineNumber}:${frame.column}`
? ` at ${frame.methodName} (${fileLocation}${sourceLocation})`
: ` at ${fileLocation}${sourceLocation}`
}

function computeErrorName(error: Error): string {
Expand Down

0 comments on commit 40b0503

Please sign in to comment.