Skip to content

Commit c4f6039

Browse files
authored
fix(module-runner): resolve resolvedSources correctly (#20959)
1 parent 8b69c9e commit c4f6039

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

packages/vite/src/module-runner/sourcemap/decoder.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { OriginalMapping } from '@jridgewell/trace-mapping'
22
import { originalPositionFor } from '@jridgewell/trace-mapping'
3-
import { posixResolve } from '../utils'
3+
import { posixDirname, posixResolve } from '../utils'
44

55
interface SourceMapLike {
66
version: number
@@ -20,6 +20,7 @@ export class DecodedMap {
2020
_decoded: undefined | number[][][]
2121
_decodedMemo: Stats
2222
url: string
23+
file: string
2324
version: number
2425
names: string[] = []
2526
resolvedSources: string[]
@@ -34,8 +35,10 @@ export class DecodedMap {
3435
this._encoded = mappings || ''
3536
this._decodedMemo = memoizedState()
3637
this.url = from
38+
this.file = from
39+
const originDir = posixDirname(from)
3740
this.resolvedSources = (sources || []).map((s) =>
38-
posixResolve(s || '', from),
41+
posixResolve(originDir, s || ''),
3942
)
4043
}
4144
}

packages/vite/src/node/ssr/runtime/__tests__/fixtures/pre-source-mapped-file.js

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/vite/src/node/ssr/runtime/__tests__/server-source-maps.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,23 @@ describe('module runner initialization', async () => {
109109
' at Module.throwError (<root>/fixtures/string-literal-sourcemap.ts:11:9)',
110110
])
111111
})
112+
113+
it('should correctly pickup the url from sources', async ({
114+
server,
115+
runner,
116+
}) => {
117+
const mod = await runner.import('/fixtures/pre-source-mapped-file.js')
118+
const error = await getError(() => mod.default())
119+
// The error stack shows "transpiled-inline.ts" because it is specified in the source map's "sources" field.
120+
// The file itself does not exist on the file system, but we should still respect "sources".
121+
// If source maps handling breaks, the stack trace will point to "transpiled-inline.js" instead, which would be a bug.
122+
expect(serializeStackDeep(server, error).slice(0, 3))
123+
.toMatchInlineSnapshot(`
124+
[
125+
"Error: __TEST_STACK_TRANSPILED_INLINE__",
126+
" at innerTestStack (<root>/fixtures/transpiled-inline.ts:22:9)",
127+
" at Module.testStack (<root>/fixtures/transpiled-inline.ts:12:3)",
128+
]
129+
`)
130+
})
112131
})

0 commit comments

Comments
 (0)