From bdd827e101f8ee4178864424b29b332a40ada013 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 --- packages/next/src/server/patch-error-inspect.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/next/src/server/patch-error-inspect.ts b/packages/next/src/server/patch-error-inspect.ts index f96e91dfae8ec..40aef77bff947 100644 --- a/packages/next/src/server/patch-error-inspect.ts +++ b/packages/next/src/server/patch-error-inspect.ts @@ -87,7 +87,14 @@ function getSourcemappedFrameIfPossible( let sourceMap: SyncSourceMapConsumer let rawSourceMap: ModernRawSourceMap if (sourceMapCacheEntry === undefined) { - const moduleSourceMap = findSourceMap(frame.file) + let sourceURL = frame.file + // e.g. "/APP/.next/server/chunks/ssr/[root of the server]__2934a0._.js" + // will be keyed by Node.js as "file:///APP/.next/server/chunks/ssr/[root%20of%20the%20server]__2934a0._.js". + // This is likely caused by `callsite.toString()` in `Error.prepareStackTrace converting file URLs to paths. + if (sourceURL.startsWith('/')) { + sourceURL = url.pathToFileURL(frame.file).toString() + } + const moduleSourceMap = findSourceMap(sourceURL) if (moduleSourceMap === undefined) { return null }