From 4588c593f5eed67a7b6919c9d357d0d3412c254e Mon Sep 17 00:00:00 2001 From: eps1lon Date: Tue, 12 Nov 2024 22:55:50 +0100 Subject: [PATCH] Shorten unsourcemapped absolute locations in terminal stacktraces --- .../next/src/server/patch-error-inspect.ts | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/packages/next/src/server/patch-error-inspect.ts b/packages/next/src/server/patch-error-inspect.ts index f96e91dfae8ec..cc7e1a9907f38 100644 --- a/packages/next/src/server/patch-error-inspect.ts +++ b/packages/next/src/server/patch-error-inspect.ts @@ -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 {