Skip to content

Commit 6b1b4e6

Browse files
authored
fix: use relative paths in source map's "sources" field (#3177)
1 parent 4f6c134 commit 6b1b4e6

File tree

5 files changed

+23
-21
lines changed

5 files changed

+23
-21
lines changed

packages/vite-node/src/server.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ export class ViteNodeServer {
119119
return this.fetchPromiseMap.get(id)!
120120
}
121121

122-
async transformRequest(id: string) {
122+
async transformRequest(id: string, filepath = id) {
123123
// reuse transform for concurrent requests
124124
if (!this.transformPromiseMap.has(id)) {
125125
this.transformPromiseMap.set(id,
126-
this._transformRequest(id)
126+
this._transformRequest(id, filepath)
127127
.finally(() => {
128128
this.transformPromiseMap.delete(id)
129129
}),
@@ -177,7 +177,7 @@ export class ViteNodeServer {
177177
}
178178
else {
179179
const start = performance.now()
180-
const r = await this._transformRequest(id, transformMode)
180+
const r = await this._transformRequest(id, filePath, transformMode)
181181
duration = performance.now() - start
182182
result = { code: r?.code, map: r?.map as any }
183183
}
@@ -191,15 +191,15 @@ export class ViteNodeServer {
191191
return result
192192
}
193193

194-
protected async processTransformResult(id: string, result: TransformResult) {
195-
const mod = this.server.moduleGraph.getModuleById(id)
194+
protected async processTransformResult(filepath: string, result: TransformResult) {
195+
const mod = this.server.moduleGraph.getModuleById(filepath)
196196
return withInlineSourcemap(result, {
197-
filepath: mod?.file || id,
197+
filepath: mod?.file || filepath,
198198
root: this.server.config.root,
199199
})
200200
}
201201

202-
private async _transformRequest(id: string, customTransformMode?: 'web' | 'ssr') {
202+
private async _transformRequest(id: string, filepath: string, customTransformMode?: 'web' | 'ssr') {
203203
debugRequest(id)
204204

205205
let result: TransformResult | null = null
@@ -225,7 +225,7 @@ export class ViteNodeServer {
225225

226226
const sourcemap = this.options.sourcemap ?? 'inline'
227227
if (sourcemap === 'inline' && result && !id.includes('node_modules'))
228-
result = await this.processTransformResult(id, result)
228+
result = await this.processTransformResult(filepath, result)
229229

230230
if (this.options.debug?.dumpModules)
231231
await this.debugger?.dumpFile(id, result)

packages/vite-node/src/source-map.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { TransformResult } from 'vite'
2-
import { dirname, isAbsolute, join, resolve } from 'pathe'
2+
import { dirname, isAbsolute, relative, resolve } from 'pathe'
33
import type { EncodedSourceMap } from '@jridgewell/trace-mapping'
44
import { install } from './source-map-handler'
55

@@ -24,17 +24,19 @@ export function withInlineSourcemap(result: TransformResult, options: {
2424
if (!map || code.includes(VITE_NODE_SOURCEMAPPING_SOURCE))
2525
return result
2626

27-
// sources path from `ViteDevServer` may be not a valid filesystem path (eg. /src/main.js),
28-
// so we try to convert them to valid filesystem path
2927
map.sources = map.sources?.map((source) => {
3028
if (!source)
3129
return source
32-
// make source absolute again, it might not be relative to the root, but to the "source root"
33-
// https://github.com/bmeurer/vite/blob/172c3e36226ec4bdf2c9d5f8fa84310bde3fec54/packages/vite/src/node/server/transformRequest.ts#L281
34-
if (!isAbsolute(source))
35-
return resolve(dirname(options.filepath), source)
36-
if (!source.startsWith(options.root))
37-
return join(options.root, source)
30+
// sometimes files here are absolute,
31+
// but they are considered absolute to the server url, not the file system
32+
// this is a bug in Vite
33+
// all files should be either absolute to the file system or relative to the source map file
34+
if (isAbsolute(source)) {
35+
const actualPath = (!source.startsWith(options.root) && source.startsWith('/'))
36+
? resolve(options.root, source.slice(1))
37+
: source
38+
return relative(dirname(options.filepath), actualPath)
39+
}
3840
return source
3941
})
4042

packages/vitest/src/typecheck/collect.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export interface FileInformation {
4040
}
4141

4242
export async function collectTests(ctx: WorkspaceProject, filepath: string): Promise<null | FileInformation> {
43-
const request = await ctx.vitenode.transformRequest(filepath)
43+
const request = await ctx.vitenode.transformRequest(filepath, filepath)
4444
if (!request)
4545
return null
4646
const ast = parseAst(request.code, {

packages/vitest/src/typecheck/typechecker.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { rm } from 'node:fs/promises'
22
import type { ExecaChildProcess } from 'execa'
33
import { execa } from 'execa'
4-
import { extname, resolve } from 'pathe'
4+
import { basename, extname, resolve } from 'pathe'
55
import { SourceMapConsumer } from 'source-map'
66
import { getTasks } from '../utils'
77
import { ensurePackageInstalled } from '../node/pkg'
@@ -132,7 +132,7 @@ export class Typechecker {
132132
const processedPos = mapConsumer?.generatedPositionFor({
133133
line: originalError.line,
134134
column: originalError.column,
135-
source: path,
135+
source: basename(path),
136136
}) || originalError
137137
const line = processedPos.line ?? originalError.line
138138
const column = processedPos.column ?? originalError.column

test/vite-node/test/server.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ describe('server works correctly', async () => {
4242
const sourceMap = extractSourceMap(fetchResult.code!)
4343

4444
// expect got sourcemap source in a valid filesystem path
45-
expect(sourceMap?.sources[0]).toBe(resolve(__dirname, '../src/foo.js'))
45+
expect(sourceMap?.sources[0]).toBe('foo.js')
4646
})
4747
})

0 commit comments

Comments
 (0)