Skip to content

Commit 703aab4

Browse files
authoredJan 21, 2023
fix: correctly resolve paths relative to root, when used outside of root directory (#2687)
Fixes #2686
1 parent 1503e15 commit 703aab4

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed
 

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,19 @@ export class ViteNodeRunner {
207207
}
208208

209209
private async _resolveUrl(id: string, importee?: string): Promise<[url: string, fsPath: string]> {
210-
if (!this.shouldResolveId(id))
211-
return [id, id]
212210
// we don't pass down importee here, because otherwise Vite doesn't resolve it correctly
211+
// should be checked before normalization, because it removes this prefix
213212
if (importee && id.startsWith(VALID_ID_PREFIX))
214213
importee = undefined
215214
id = normalizeRequestId(id, this.options.base)
215+
// should be checked after normalization
216+
// provide importer only for relative and absolute paths
217+
// paths like "src/user" are valid for transformRequest, but they will be resolved incorrectly,
218+
// if importer is provided - because they will be treated as relative to the importer instead of root
219+
if (!id.startsWith('/') && !id.startsWith('./') && !id.startsWith('../'))
220+
importee = undefined
221+
if (!this.shouldResolveId(id))
222+
return [id, id]
216223
const { path, exists } = toFilePath(id, this.root)
217224
if (!this.options.resolveId || exists)
218225
return [id, path]

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class VitestRunner extends ViteNodeRunner {
3939
const environment = getCurrentEnvironment()
4040
// do not try and resolve node builtins in Node
4141
// import('url') returns Node internal even if 'url' package is installed
42-
return environment === 'node' ? !isNodeBuiltin(id) : true
42+
return environment === 'node' ? !isNodeBuiltin(id) : !id.startsWith('node:')
4343
}
4444

4545
async resolveUrl(id: string, importee?: string) {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { pathToFileURL } from 'node:url'
1+
import { fileURLToPath, pathToFileURL } from 'node:url'
22
import { readFile } from 'node:fs/promises'
33
import { hasCJSSyntax, isNodeBuiltin } from 'mlly'
44
import { normalizeModuleId } from 'vite-node/utils'
@@ -64,7 +64,7 @@ export const resolve: Resolver = async (url, context, next) => {
6464
}
6565
else {
6666
const { url: resolvedUrl, format } = await next(url, context, next)
67-
filepath = new URL(resolvedUrl).pathname
67+
filepath = fileURLToPath(resolvedUrl)
6868
result = {
6969
url: resolvedUrl,
7070
format,

‎test/core/test/imports.test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ test('dynamic absolute with extension mport works', async () => {
4646
expect(stringTimeoutMod).toBe(variableTimeoutMod)
4747
})
4848

49+
test('dynamic baseUrl import works', async () => {
50+
const staticMod = await import('./../src/timeout')
51+
// @ts-expect-error there is no tsconfig in test/core to handle baseUrl
52+
const dynamicMod = await import('src/timeout')
53+
expect(staticMod).toBe(dynamicMod)
54+
})
55+
4956
test('data with dynamic import works', async () => {
5057
const dataUri = 'data:text/javascript;charset=utf-8,export default "hi"'
5158
const { default: hi } = await import(dataUri)

0 commit comments

Comments
 (0)
Please sign in to comment.