Skip to content

Commit e9cb413

Browse files
authoredDec 8, 2022
fix: try to resolve id, if relative path is provided (#2461)
* fix: try to resolve id, if relative path is provided * chore: update lockfile
1 parent f02c982 commit e9cb413

File tree

9 files changed

+77
-67
lines changed

9 files changed

+77
-67
lines changed
 

‎packages/coverage-c8/src/provider.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class C8CoverageProvider implements CoverageProvider {
5858
if (!map)
5959
return
6060

61-
const filepath = result.file || file.split('?')[0]
61+
const filepath = file.split('?')[0]
6262

6363
const url = _url.pathToFileURL(filepath).href
6464

‎packages/vite-node/src/client.ts

+18-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import vm from 'vm'
44
import { dirname, extname, isAbsolute, resolve } from 'pathe'
55
import { isNodeBuiltin } from 'mlly'
66
import createDebug from 'debug'
7-
import { isPrimitive, mergeSlashes, normalizeModuleId, normalizeRequestId, slash, toFilePath } from './utils'
7+
import { cleanUrl, isPrimitive, normalizeModuleId, normalizeRequestId, slash, toFilePath } from './utils'
88
import type { HotContext, ModuleCache, ViteNodeRunnerOptions } from './types'
99
import { extractSourceMap } from './source-map'
1010

@@ -221,39 +221,43 @@ export class ViteNodeRunner {
221221

222222
Object.defineProperty(request, 'callstack', { get: () => callstack })
223223

224-
const resolveId = async (dep: string, callstackPosition = 1) => {
224+
const resolveId = async (dep: string, callstackPosition = 1): Promise<[dep: string, id: string | undefined]> => {
225225
if (this.options.resolveId && this.shouldResolveId(dep)) {
226-
let importer = callstack[callstack.length - callstackPosition]
226+
let importer: string | undefined = callstack[callstack.length - callstackPosition]
227+
if (importer && !dep.startsWith('.'))
228+
importer = undefined
227229
if (importer && importer.startsWith('mock:'))
228230
importer = importer.slice(5)
229-
const { id } = await this.options.resolveId(dep, importer) || {}
230-
dep = id && isAbsolute(id) ? mergeSlashes(`/@fs/${id}`) : id || dep
231+
const resolved = await this.options.resolveId(normalizeRequestId(dep), importer)
232+
return [dep, resolved?.id]
231233
}
232234

233-
return dep
235+
return [dep, undefined]
234236
}
235237

238+
const [dep, resolvedId] = await resolveId(id, 2)
239+
236240
const requestStubs = this.options.requestStubs || DEFAULT_REQUEST_STUBS
237241
if (id in requestStubs)
238242
return requestStubs[id]
239243

240244
// eslint-disable-next-line prefer-const
241-
let { code: transformed, externalize, file } = await this.options.fetchModule(id)
245+
let { code: transformed, externalize } = await this.options.fetchModule(resolvedId || dep)
242246

243247
// in case we resolved fsPath incorrectly, Vite will return the correct file path
244248
// in that case we need to update cache, so we don't have the same module as different exports
245249
// but we ignore fsPath that has custom query, because it might need to be different
246-
if (file && !fsPath.includes('?') && fsPath !== file) {
247-
if (this.moduleCache.has(file)) {
248-
mod = this.moduleCache.get(file)
250+
if (resolvedId && !fsPath.includes('?') && fsPath !== resolvedId) {
251+
if (this.moduleCache.has(resolvedId)) {
252+
mod = this.moduleCache.get(resolvedId)
249253
this.moduleCache.set(fsPath, mod)
250254
if (mod.promise)
251255
return mod.promise
252256
if (mod.exports)
253257
return mod.exports
254258
}
255259
else {
256-
this.moduleCache.set(file, mod)
260+
this.moduleCache.set(resolvedId, mod)
257261
}
258262
}
259263

@@ -267,8 +271,10 @@ export class ViteNodeRunner {
267271
if (transformed == null)
268272
throw new Error(`[vite-node] Failed to load ${id}`)
269273

274+
const file = cleanUrl(resolvedId || fsPath)
275+
// console.log('file', file)
270276
// disambiguate the `<UNIT>:/` on windows: see nodejs/node#31710
271-
const url = pathToFileURL(file || fsPath).href
277+
const url = pathToFileURL(file).href
272278
const meta = { url }
273279
const exports = Object.create(null)
274280
Object.defineProperty(exports, Symbol.toStringTag, {

‎packages/vite-node/src/server.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { TransformResult, ViteDevServer } from 'vite'
44
import createDebug from 'debug'
55
import type { DebuggerOptions, FetchResult, RawSourceMap, ViteNodeResolveId, ViteNodeServerOptions } from './types'
66
import { shouldExternalize } from './externalize'
7-
import { cleanUrl, normalizeModuleId, toArray, toFilePath } from './utils'
7+
import { normalizeModuleId, toArray, toFilePath } from './utils'
88
import { Debugger } from './debug'
99
import { withInlineSourcemap } from './source-map'
1010

@@ -130,8 +130,6 @@ export class ViteNodeServer {
130130
const module = this.server.moduleGraph.getModuleById(id)
131131
const timestamp = module ? module.lastHMRTimestamp : null
132132
const cache = this.fetchCache.get(filePath)
133-
if (cache?.result.id)
134-
id = cache.result.id
135133
if (timestamp !== null && cache && cache.timestamp >= timestamp)
136134
return cache.result
137135

@@ -143,16 +141,10 @@ export class ViteNodeServer {
143141
this.debugger?.recordExternalize(id, externalize)
144142
}
145143
else {
146-
let file = module?.file
147-
if (!file) {
148-
const [, resolvedId] = await this.server.moduleGraph.resolveUrl(id, true)
149-
id = resolvedId
150-
file = cleanUrl(resolvedId)
151-
}
152144
const start = performance.now()
153145
const r = await this._transformRequest(id)
154146
duration = performance.now() - start
155-
result = { file, id, code: r?.code, map: r?.map as unknown as RawSourceMap }
147+
result = { code: r?.code, map: r?.map as unknown as RawSourceMap }
156148
}
157149

158150
this.fetchCache.set(filePath, {

‎packages/vite-node/src/types.ts

-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ export interface FetchResult {
3131
code?: string
3232
externalize?: string
3333
map?: RawSourceMap
34-
id?: string
35-
file?: string
3634
}
3735

3836
export type HotContext = Omit<ViteHotContext, 'acceptDeps' | 'decline'>

‎packages/vitest/src/runtime/execute.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export class VitestRunner extends ViteNodeRunner {
3030
prepareContext(context: Record<string, any>) {
3131
const request = context.__vite_ssr_import__
3232
const resolveId = context.__vitest_resolve_id__
33+
const resolveUrl = async (dep: string) => {
34+
const [id, resolvedId] = await resolveId(dep)
35+
return resolvedId || id
36+
}
3337

3438
const mocker = new VitestMocker(this.options, this.moduleCache, request)
3539

@@ -42,8 +46,8 @@ export class VitestRunner extends ViteNodeRunner {
4246
}
4347

4448
return Object.assign(context, {
45-
__vite_ssr_import__: async (dep: string) => mocker.requestWithMock(await resolveId(dep)),
46-
__vite_ssr_dynamic_import__: async (dep: string) => mocker.requestWithMock(await resolveId(dep)),
49+
__vite_ssr_import__: async (dep: string) => mocker.requestWithMock(await resolveUrl(dep)),
50+
__vite_ssr_dynamic_import__: async (dep: string) => mocker.requestWithMock(await resolveUrl(dep)),
4751
__vitest_mocker__: mocker,
4852
})
4953
}

‎pnpm-lock.yaml

+45-38
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/reporters/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sonar-config.xml

‎test/reporters/deps-reporter.vitest.config.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { defineConfig } from 'vitest/config'
33
export default defineConfig({
44
test: {
55
include: ['tests/reporters.spec.ts'],
6-
reporters: ['pkg-reporter'],
6+
reporters: ['pkg-reporter', 'vitest-sonar-reporter'],
7+
outputFile: './sonar-config.xml',
78
},
89
})

‎test/reporters/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"devDependencies": {
88
"execa": "^6.1.0",
99
"pkg-reporter": "./reportPkg/",
10-
"vitest": "workspace:*"
10+
"vitest": "workspace:*",
11+
"vitest-sonar-reporter": "0.3.3"
1112
}
1213
}

0 commit comments

Comments
 (0)
Please sign in to comment.