From 5beaf20a0faa692686bfd29aba8b0a037bf2a7bb Mon Sep 17 00:00:00 2001 From: fi3ework Date: Tue, 18 Apr 2023 03:51:06 +0800 Subject: [PATCH 1/9] fix(ssr): stacktrace uses abs path with or without sourcemap --- .../vite/src/node/server/transformRequest.ts | 8 ++++ packages/vite/src/node/ssr/ssrTransform.ts | 7 ++- .../__tests__/css-sourcemap.spec.ts | 1 + .../__tests__/js-sourcemap.spec.ts | 1 + .../ssr-html/__tests__/ssr-html.spec.ts | 45 ++++++++++++------- playground/ssr-html/src/error.ts | 3 ++ playground/ssr-html/test-stacktrace.js | 29 +++++++----- playground/test-utils.ts | 3 ++ 8 files changed, 69 insertions(+), 28 deletions(-) create mode 100644 playground/ssr-html/src/error.ts diff --git a/packages/vite/src/node/server/transformRequest.ts b/packages/vite/src/node/server/transformRequest.ts index b08a01e5d01b9e..accd84aff2a53d 100644 --- a/packages/vite/src/node/server/transformRequest.ts +++ b/packages/vite/src/node/server/transformRequest.ts @@ -293,6 +293,7 @@ async function loadAndTransform( // to resolve and display them in a meaningful way (rather than // with absolute paths). if (path.isAbsolute(sourcePath)) { + map.sourceRoot = path.dirname(mod.file) + path.sep map.sources[sourcesIndex] = path.relative( path.dirname(mod.file), sourcePath, @@ -303,6 +304,13 @@ async function loadAndTransform( } } + // no sourcemap for raw js source file + if (!map && mod.file) { + map = { + sources: [mod.file], + } as SourceMap + } + const result = ssr && !server.config.experimental.skipSsrTransform ? await server.ssrTransform(code, map as SourceMap, url, originalCode) diff --git a/packages/vite/src/node/ssr/ssrTransform.ts b/packages/vite/src/node/ssr/ssrTransform.ts index 8a62094dcb0789..9caac70268f99b 100644 --- a/packages/vite/src/node/ssr/ssrTransform.ts +++ b/packages/vite/src/node/ssr/ssrTransform.ts @@ -279,13 +279,18 @@ async function ssrTransformScript( ...map, sources: inMap.sources, sourcesContent: inMap.sourcesContent, + // sourceRoot: inMap.sourceRoot, } as RawSourceMap, inMap as RawSourceMap, ], false, ) as SourceMap } else { - map.sources = [url] + if (inMap?.sources) { + map.sources = inMap.sources + } else { + map.sources = [url] + } // needs to use originalCode instead of code // because code might be already transformed even if map is null map.sourcesContent = [originalCode] diff --git a/playground/css-sourcemap/__tests__/css-sourcemap.spec.ts b/playground/css-sourcemap/__tests__/css-sourcemap.spec.ts index 696864b12ffd64..5be3c0a23b3e24 100644 --- a/playground/css-sourcemap/__tests__/css-sourcemap.spec.ts +++ b/playground/css-sourcemap/__tests__/css-sourcemap.spec.ts @@ -69,6 +69,7 @@ describe.runIf(isServe)('serve', () => { expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(` { "mappings": "AAAA;EACE,UAAU;AACZ;;ACAA;EACE,UAAU;AACZ", + "sourceRoot": "/root/", "sources": [ "be-imported.css", "linked-with-import.css", diff --git a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts index f3c78604287a7c..06ad21b2e84b9a 100644 --- a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts +++ b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts @@ -24,6 +24,7 @@ if (!isBuild) { expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(` { "mappings": "AAAO,aAAM,MAAM;", + "sourceRoot": "/root/", "sources": [ "bar.ts", ], diff --git a/playground/ssr-html/__tests__/ssr-html.spec.ts b/playground/ssr-html/__tests__/ssr-html.spec.ts index 2b9da5cadd01c0..2bb082aa7407c2 100644 --- a/playground/ssr-html/__tests__/ssr-html.spec.ts +++ b/playground/ssr-html/__tests__/ssr-html.spec.ts @@ -62,23 +62,34 @@ describe.runIf(isServe)('hmr', () => { describe.runIf(isServe)('stacktrace', () => { const execFileAsync = promisify(execFile) - for (const sourcemapsEnabled of [false, true]) { - test(`stacktrace is correct when sourcemaps is${ - sourcemapsEnabled ? '' : ' not' - } enabled in Node.js`, async () => { - const testStacktraceFile = path.resolve( - __dirname, - '../test-stacktrace.js', - ) + for (const ext of ['js', 'ts']) { + for (const sourcemapsEnabled of [false, true]) { + test(`stacktrace of ${ext} is correct when sourcemaps is${ + sourcemapsEnabled ? '' : ' not' + } enabled in Node.js`, async () => { + const testStacktraceFile = path.resolve( + __dirname, + '../test-stacktrace.js', + ) - const p = await execFileAsync('node', [ - testStacktraceFile, - '' + sourcemapsEnabled, - ]) - const line = p.stdout - .split('\n') - .find((line) => line.includes('Module.error')) - expect(line.trim()).toMatch(/[\\/]src[\\/]error\.js:2:9/) - }) + const p = await execFileAsync('node', [ + testStacktraceFile, + '' + sourcemapsEnabled, + ext, + ]) + const lines = p.stdout + .split('\n') + .filter((line) => line.includes('Module.error')) + + const reg = new RegExp( + // TODO: ts without sourcemaps will resolve column to 8 which should be 9 + path.resolve(__dirname, '../src') + '/error\\.' + ext + ':2:[89]', + ) + + lines.forEach((line) => { + expect(line.trim()).toMatch(reg) + }) + }) + } } }) diff --git a/playground/ssr-html/src/error.ts b/playground/ssr-html/src/error.ts new file mode 100644 index 00000000000000..fe8eeb20af8f8a --- /dev/null +++ b/playground/ssr-html/src/error.ts @@ -0,0 +1,3 @@ +export function error() { + throw new Error('e') +} diff --git a/playground/ssr-html/test-stacktrace.js b/playground/ssr-html/test-stacktrace.js index c3ce5e56736799..327a8b4c423dae 100644 --- a/playground/ssr-html/test-stacktrace.js +++ b/playground/ssr-html/test-stacktrace.js @@ -3,8 +3,10 @@ import { fileURLToPath } from 'node:url' import { createServer } from 'vite' const isSourceMapEnabled = process.argv[2] === 'true' +const ext = process.argv[3] process.setSourceMapsEnabled(isSourceMapEnabled) console.log('# sourcemaps enabled:', isSourceMapEnabled) +console.log('# source file extension:', ext) const version = (() => { const m = process.version.match(/^v(\d+)\.(\d+)\.\d+$/) @@ -31,17 +33,24 @@ const vite = await createServer({ appType: 'custom', }) -const mod = await vite.ssrLoadModule('/src/error.js') -try { - mod.error() -} catch (e) { - // this should not be called - // when sourcemap support for `new Function` is supported and sourcemap is enabled - // because the stacktrace is already rewritten by Node.js - if (!(isSourceMapEnabled && isFunctionSourceMapSupported)) { - vite.ssrFixStacktrace(e) +const dir = path.dirname(fileURLToPath(import.meta.url)) + +const abs1 = await vite.ssrLoadModule(`/src/error.${ext}`) +const abs2 = await vite.ssrLoadModule(path.resolve(dir, `./src/error.${ext}`)) +const relative = await vite.ssrLoadModule(`./src/error.${ext}`) + +for (const mod of [abs1, abs2, relative]) { + try { + mod.error() + } catch (e) { + // this should not be called + // when sourcemap support for `new Function` is supported and sourcemap is enabled + // because the stacktrace is already rewritten by Node.js + if (!(isSourceMapEnabled && isFunctionSourceMapSupported)) { + vite.ssrFixStacktrace(e) + } + console.log(e) } - console.log(e) } await vite.close() diff --git a/playground/test-utils.ts b/playground/test-utils.ts index e7fcb93a4d5daf..e21aa9ae6c1f09 100644 --- a/playground/test-utils.ts +++ b/playground/test-utils.ts @@ -308,6 +308,9 @@ export const formatSourcemapForSnapshot = (map: any): any => { delete m.file delete m.names m.sources = m.sources.map((source) => source.replace(root, '/root')) + if (m.sourceRoot) { + m.sourceRoot = m.sourceRoot.replace(root + path.sep, '/root/') + } return m } From 6521e96f68c62adcaf2838ef57b4ec24829959e1 Mon Sep 17 00:00:00 2001 From: fi3ework Date: Wed, 19 Apr 2023 23:33:13 +0800 Subject: [PATCH 2/9] fix: Windows path --- packages/vite/src/node/server/transformRequest.ts | 2 +- playground/css-sourcemap/__tests__/css-sourcemap.spec.ts | 2 +- playground/js-sourcemap/__tests__/js-sourcemap.spec.ts | 2 +- playground/ssr-html/__tests__/ssr-html.spec.ts | 6 +++++- playground/test-utils.ts | 2 +- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/node/server/transformRequest.ts b/packages/vite/src/node/server/transformRequest.ts index accd84aff2a53d..138220e01cbf36 100644 --- a/packages/vite/src/node/server/transformRequest.ts +++ b/packages/vite/src/node/server/transformRequest.ts @@ -293,7 +293,7 @@ async function loadAndTransform( // to resolve and display them in a meaningful way (rather than // with absolute paths). if (path.isAbsolute(sourcePath)) { - map.sourceRoot = path.dirname(mod.file) + path.sep + map.sourceRoot = path.dirname(mod.file) map.sources[sourcesIndex] = path.relative( path.dirname(mod.file), sourcePath, diff --git a/playground/css-sourcemap/__tests__/css-sourcemap.spec.ts b/playground/css-sourcemap/__tests__/css-sourcemap.spec.ts index 5be3c0a23b3e24..98dad19c1b9683 100644 --- a/playground/css-sourcemap/__tests__/css-sourcemap.spec.ts +++ b/playground/css-sourcemap/__tests__/css-sourcemap.spec.ts @@ -69,7 +69,7 @@ describe.runIf(isServe)('serve', () => { expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(` { "mappings": "AAAA;EACE,UAAU;AACZ;;ACAA;EACE,UAAU;AACZ", - "sourceRoot": "/root/", + "sourceRoot": "/root", "sources": [ "be-imported.css", "linked-with-import.css", diff --git a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts index 06ad21b2e84b9a..60f05ebf409527 100644 --- a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts +++ b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts @@ -24,7 +24,7 @@ if (!isBuild) { expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(` { "mappings": "AAAO,aAAM,MAAM;", - "sourceRoot": "/root/", + "sourceRoot": "/root", "sources": [ "bar.ts", ], diff --git a/playground/ssr-html/__tests__/ssr-html.spec.ts b/playground/ssr-html/__tests__/ssr-html.spec.ts index 2bb082aa7407c2..331aa549a90732 100644 --- a/playground/ssr-html/__tests__/ssr-html.spec.ts +++ b/playground/ssr-html/__tests__/ssr-html.spec.ts @@ -3,6 +3,7 @@ import { promisify } from 'node:util' import path from 'node:path' import fetch from 'node-fetch' import { describe, expect, test } from 'vitest' +import { normalizePath } from 'vite' import { port } from './serve' import { editFile, isServe, page, untilUpdated } from '~utils' @@ -83,7 +84,10 @@ describe.runIf(isServe)('stacktrace', () => { const reg = new RegExp( // TODO: ts without sourcemaps will resolve column to 8 which should be 9 - path.resolve(__dirname, '../src') + '/error\\.' + ext + ':2:[89]', + normalizePath( + path.resolve(__dirname, '../src', `error.${ext}`), + ).replace('.', '\\.') + ':2:[89]', + 'i', ) lines.forEach((line) => { diff --git a/playground/test-utils.ts b/playground/test-utils.ts index e21aa9ae6c1f09..26eaf5c1323982 100644 --- a/playground/test-utils.ts +++ b/playground/test-utils.ts @@ -309,7 +309,7 @@ export const formatSourcemapForSnapshot = (map: any): any => { delete m.names m.sources = m.sources.map((source) => source.replace(root, '/root')) if (m.sourceRoot) { - m.sourceRoot = m.sourceRoot.replace(root + path.sep, '/root/') + m.sourceRoot = m.sourceRoot.replace(root, '/root') } return m } From 8d2075b65d133bb249a769bafbce767144e55020 Mon Sep 17 00:00:00 2001 From: fi3ework Date: Wed, 10 May 2023 23:55:38 +0800 Subject: [PATCH 3/9] prefer mod.file --- packages/vite/src/node/ssr/ssrModuleLoader.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/ssr/ssrModuleLoader.ts b/packages/vite/src/node/ssr/ssrModuleLoader.ts index 09a9b8817cc380..5b6eb703e851e0 100644 --- a/packages/vite/src/node/ssr/ssrModuleLoader.ts +++ b/packages/vite/src/node/ssr/ssrModuleLoader.ts @@ -222,7 +222,7 @@ async function instantiateModule( ssrExportAllKey, '"use strict";' + result.code + - `\n//# sourceURL=${mod.url}${sourceMapSuffix}`, + `\n//# sourceURL=${mod.file}${sourceMapSuffix}`, ) await initModule( context.global, From fccb8f9d62021bd8a85b2fff0f6387a2975c66a0 Mon Sep 17 00:00:00 2001 From: fi3ework Date: Thu, 11 May 2023 00:12:20 +0800 Subject: [PATCH 4/9] test windows path --- playground/ssr-html/__tests__/ssr-html.spec.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/playground/ssr-html/__tests__/ssr-html.spec.ts b/playground/ssr-html/__tests__/ssr-html.spec.ts index 331aa549a90732..4a9999c3cb5ace 100644 --- a/playground/ssr-html/__tests__/ssr-html.spec.ts +++ b/playground/ssr-html/__tests__/ssr-html.spec.ts @@ -91,6 +91,7 @@ describe.runIf(isServe)('stacktrace', () => { ) lines.forEach((line) => { + console.log('🧐', line.trim()) expect(line.trim()).toMatch(reg) }) }) From 9a3c3c4de0aa37091721d505a76ecc1749464fe8 Mon Sep 17 00:00:00 2001 From: fi3ework Date: Thu, 11 May 2023 00:30:24 +0800 Subject: [PATCH 5/9] Revert "prefer mod.file" This reverts commit 8d2075b65d133bb249a769bafbce767144e55020. --- packages/vite/src/node/ssr/ssrModuleLoader.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/ssr/ssrModuleLoader.ts b/packages/vite/src/node/ssr/ssrModuleLoader.ts index 5b6eb703e851e0..09a9b8817cc380 100644 --- a/packages/vite/src/node/ssr/ssrModuleLoader.ts +++ b/packages/vite/src/node/ssr/ssrModuleLoader.ts @@ -222,7 +222,7 @@ async function instantiateModule( ssrExportAllKey, '"use strict";' + result.code + - `\n//# sourceURL=${mod.file}${sourceMapSuffix}`, + `\n//# sourceURL=${mod.url}${sourceMapSuffix}`, ) await initModule( context.global, From 06301b7d0917d5991733ab461a46a23a94a45bf5 Mon Sep 17 00:00:00 2001 From: fi3ework Date: Thu, 11 May 2023 00:30:24 +0800 Subject: [PATCH 6/9] Revert "test windows path" This reverts commit fccb8f9d62021bd8a85b2fff0f6387a2975c66a0. --- playground/ssr-html/__tests__/ssr-html.spec.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/playground/ssr-html/__tests__/ssr-html.spec.ts b/playground/ssr-html/__tests__/ssr-html.spec.ts index 4a9999c3cb5ace..331aa549a90732 100644 --- a/playground/ssr-html/__tests__/ssr-html.spec.ts +++ b/playground/ssr-html/__tests__/ssr-html.spec.ts @@ -91,7 +91,6 @@ describe.runIf(isServe)('stacktrace', () => { ) lines.forEach((line) => { - console.log('🧐', line.trim()) expect(line.trim()).toMatch(reg) }) }) From a1bebbc310c2d4db494d76f69eb34102048f654c Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Sat, 13 May 2023 21:44:54 +0900 Subject: [PATCH 7/9] chore: revert changes --- packages/vite/src/node/server/transformRequest.ts | 8 -------- packages/vite/src/node/ssr/ssrTransform.ts | 7 +------ playground/css-sourcemap/__tests__/css-sourcemap.spec.ts | 1 - playground/js-sourcemap/__tests__/js-sourcemap.spec.ts | 1 - 4 files changed, 1 insertion(+), 16 deletions(-) diff --git a/packages/vite/src/node/server/transformRequest.ts b/packages/vite/src/node/server/transformRequest.ts index 138220e01cbf36..b08a01e5d01b9e 100644 --- a/packages/vite/src/node/server/transformRequest.ts +++ b/packages/vite/src/node/server/transformRequest.ts @@ -293,7 +293,6 @@ async function loadAndTransform( // to resolve and display them in a meaningful way (rather than // with absolute paths). if (path.isAbsolute(sourcePath)) { - map.sourceRoot = path.dirname(mod.file) map.sources[sourcesIndex] = path.relative( path.dirname(mod.file), sourcePath, @@ -304,13 +303,6 @@ async function loadAndTransform( } } - // no sourcemap for raw js source file - if (!map && mod.file) { - map = { - sources: [mod.file], - } as SourceMap - } - const result = ssr && !server.config.experimental.skipSsrTransform ? await server.ssrTransform(code, map as SourceMap, url, originalCode) diff --git a/packages/vite/src/node/ssr/ssrTransform.ts b/packages/vite/src/node/ssr/ssrTransform.ts index 9caac70268f99b..8a62094dcb0789 100644 --- a/packages/vite/src/node/ssr/ssrTransform.ts +++ b/packages/vite/src/node/ssr/ssrTransform.ts @@ -279,18 +279,13 @@ async function ssrTransformScript( ...map, sources: inMap.sources, sourcesContent: inMap.sourcesContent, - // sourceRoot: inMap.sourceRoot, } as RawSourceMap, inMap as RawSourceMap, ], false, ) as SourceMap } else { - if (inMap?.sources) { - map.sources = inMap.sources - } else { - map.sources = [url] - } + map.sources = [url] // needs to use originalCode instead of code // because code might be already transformed even if map is null map.sourcesContent = [originalCode] diff --git a/playground/css-sourcemap/__tests__/css-sourcemap.spec.ts b/playground/css-sourcemap/__tests__/css-sourcemap.spec.ts index 98dad19c1b9683..696864b12ffd64 100644 --- a/playground/css-sourcemap/__tests__/css-sourcemap.spec.ts +++ b/playground/css-sourcemap/__tests__/css-sourcemap.spec.ts @@ -69,7 +69,6 @@ describe.runIf(isServe)('serve', () => { expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(` { "mappings": "AAAA;EACE,UAAU;AACZ;;ACAA;EACE,UAAU;AACZ", - "sourceRoot": "/root", "sources": [ "be-imported.css", "linked-with-import.css", diff --git a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts index 60f05ebf409527..f3c78604287a7c 100644 --- a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts +++ b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts @@ -24,7 +24,6 @@ if (!isBuild) { expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(` { "mappings": "AAAO,aAAM,MAAM;", - "sourceRoot": "/root", "sources": [ "bar.ts", ], From 51dae229042e8ae971a1c2f1e4c79f6de4b79116 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Sat, 13 May 2023 22:14:24 +0900 Subject: [PATCH 8/9] fix: handle relative path sourcemap correctly --- packages/vite/src/node/ssr/ssrModuleLoader.ts | 2 +- packages/vite/src/node/ssr/ssrStacktrace.ts | 10 ++++++---- packages/vite/src/node/ssr/ssrTransform.ts | 3 ++- playground/ssr-html/__tests__/ssr-html.spec.ts | 7 +++---- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/packages/vite/src/node/ssr/ssrModuleLoader.ts b/packages/vite/src/node/ssr/ssrModuleLoader.ts index 09a9b8817cc380..3e498809f32829 100644 --- a/packages/vite/src/node/ssr/ssrModuleLoader.ts +++ b/packages/vite/src/node/ssr/ssrModuleLoader.ts @@ -222,7 +222,7 @@ async function instantiateModule( ssrExportAllKey, '"use strict";' + result.code + - `\n//# sourceURL=${mod.url}${sourceMapSuffix}`, + `\n//# sourceURL=${mod.id}${sourceMapSuffix}`, ) await initModule( context.global, diff --git a/packages/vite/src/node/ssr/ssrStacktrace.ts b/packages/vite/src/node/ssr/ssrStacktrace.ts index fe66b8803530b7..80073f6e05d232 100644 --- a/packages/vite/src/node/ssr/ssrStacktrace.ts +++ b/packages/vite/src/node/ssr/ssrStacktrace.ts @@ -1,3 +1,4 @@ +import path from 'node:path' import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping' import type { ModuleGraph } from '../server/moduleGraph' @@ -21,10 +22,10 @@ export function ssrRewriteStacktrace( .map((line) => { return line.replace( /^ {4}at (?:(\S.*?)\s\()?(.+?):(\d+)(?::(\d+))?\)?/, - (input, varName, url, line, column) => { - if (!url) return input + (input, varName, id, line, column) => { + if (!id) return input - const mod = moduleGraph.urlToModuleMap.get(url) + const mod = moduleGraph.idToModuleMap.get(id) const rawSourceMap = mod?.ssrTransformResult?.map if (!rawSourceMap) { @@ -43,7 +44,8 @@ export function ssrRewriteStacktrace( } const trimmedVarName = varName.trim() - const source = `${pos.source}:${pos.line}:${pos.column}` + const sourceFile = path.resolve(path.dirname(id), pos.source) + const source = `${sourceFile}:${pos.line}:${pos.column}` if (!trimmedVarName || trimmedVarName === 'eval') { return ` at ${source}` } else { diff --git a/packages/vite/src/node/ssr/ssrTransform.ts b/packages/vite/src/node/ssr/ssrTransform.ts index 8a62094dcb0789..5496019504e50d 100644 --- a/packages/vite/src/node/ssr/ssrTransform.ts +++ b/packages/vite/src/node/ssr/ssrTransform.ts @@ -1,3 +1,4 @@ +import path from 'node:path' import MagicString from 'magic-string' import type { SourceMap } from 'rollup' import type { @@ -285,7 +286,7 @@ async function ssrTransformScript( false, ) as SourceMap } else { - map.sources = [url] + map.sources = [path.basename(url)] // needs to use originalCode instead of code // because code might be already transformed even if map is null map.sourcesContent = [originalCode] diff --git a/playground/ssr-html/__tests__/ssr-html.spec.ts b/playground/ssr-html/__tests__/ssr-html.spec.ts index 331aa549a90732..c16742cf41923e 100644 --- a/playground/ssr-html/__tests__/ssr-html.spec.ts +++ b/playground/ssr-html/__tests__/ssr-html.spec.ts @@ -3,7 +3,6 @@ import { promisify } from 'node:util' import path from 'node:path' import fetch from 'node-fetch' import { describe, expect, test } from 'vitest' -import { normalizePath } from 'vite' import { port } from './serve' import { editFile, isServe, page, untilUpdated } from '~utils' @@ -84,9 +83,9 @@ describe.runIf(isServe)('stacktrace', () => { const reg = new RegExp( // TODO: ts without sourcemaps will resolve column to 8 which should be 9 - normalizePath( - path.resolve(__dirname, '../src', `error.${ext}`), - ).replace('.', '\\.') + ':2:[89]', + path + .resolve(__dirname, '../src', `error.${ext}`) + .replace(/\\/g, '\\\\') + ':2:[89]', 'i', ) From 89b865e92a71a24fd14dc7859df147d2d741d8b5 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Sat, 13 May 2023 22:18:01 +0900 Subject: [PATCH 9/9] fix: sourcemap stacktrace difference --- packages/vite/src/node/ssr/ssrStacktrace.ts | 6 ++++-- playground/ssr-html/__tests__/ssr-html.spec.ts | 3 +-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/vite/src/node/ssr/ssrStacktrace.ts b/packages/vite/src/node/ssr/ssrStacktrace.ts index 80073f6e05d232..5a2c55fdcc9830 100644 --- a/packages/vite/src/node/ssr/ssrStacktrace.ts +++ b/packages/vite/src/node/ssr/ssrStacktrace.ts @@ -36,7 +36,8 @@ export function ssrRewriteStacktrace( const pos = originalPositionFor(traced, { line: Number(line) - offset, - column: Number(column), + // stacktrace's column is 1-indexed, but sourcemap's one is 0-indexed + column: Number(column) - 1, }) if (!pos.source || pos.line == null || pos.column == null) { @@ -45,7 +46,8 @@ export function ssrRewriteStacktrace( const trimmedVarName = varName.trim() const sourceFile = path.resolve(path.dirname(id), pos.source) - const source = `${sourceFile}:${pos.line}:${pos.column}` + // stacktrace's column is 1-indexed, but sourcemap's one is 0-indexed + const source = `${sourceFile}:${pos.line}:${pos.column + 1}` if (!trimmedVarName || trimmedVarName === 'eval') { return ` at ${source}` } else { diff --git a/playground/ssr-html/__tests__/ssr-html.spec.ts b/playground/ssr-html/__tests__/ssr-html.spec.ts index c16742cf41923e..f01565679680c2 100644 --- a/playground/ssr-html/__tests__/ssr-html.spec.ts +++ b/playground/ssr-html/__tests__/ssr-html.spec.ts @@ -82,10 +82,9 @@ describe.runIf(isServe)('stacktrace', () => { .filter((line) => line.includes('Module.error')) const reg = new RegExp( - // TODO: ts without sourcemaps will resolve column to 8 which should be 9 path .resolve(__dirname, '../src', `error.${ext}`) - .replace(/\\/g, '\\\\') + ':2:[89]', + .replace(/\\/g, '\\\\') + ':2:9', 'i', )