Skip to content

Commit 807418f

Browse files
authoredAug 31, 2023
fix(vite-node): check more precisely for root/base paths (#4049)
1 parent 3525e37 commit 807418f

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed
 

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import createDebug from 'debug'
66
import type { EncodedSourceMap } from '@jridgewell/trace-mapping'
77
import type { DebuggerOptions, FetchResult, ViteNodeResolveId, ViteNodeServerOptions } from './types'
88
import { shouldExternalize } from './externalize'
9-
import { normalizeModuleId, toArray, toFilePath } from './utils'
9+
import { normalizeModuleId, toArray, toFilePath, withTrailingSlash } from './utils'
1010
import { Debugger } from './debug'
1111
import { withInlineSourcemap } from './source-map'
1212

@@ -106,7 +106,7 @@ export class ViteNodeServer {
106106
}
107107

108108
async resolveId(id: string, importer?: string, transformMode?: 'web' | 'ssr'): Promise<ViteNodeResolveId | null> {
109-
if (importer && !importer.startsWith(this.server.config.root))
109+
if (importer && !importer.startsWith(withTrailingSlash(this.server.config.root)))
110110
importer = resolve(this.server.config.root, importer)
111111
const mode = transformMode ?? ((importer && this.getTransformMode(importer)) || 'ssr')
112112
return this.server.pluginContainer.resolveId(id, importer, { ssr: mode === 'ssr' })
@@ -182,7 +182,7 @@ export class ViteNodeServer {
182182
const cacheDir = this.options.deps?.cacheDir
183183

184184
if (cacheDir && id.includes(cacheDir)) {
185-
if (!id.startsWith(this.server.config.root))
185+
if (!id.startsWith(withTrailingSlash(this.server.config.root)))
186186
id = join(this.server.config.root, id)
187187
const timeout = setTimeout(() => {
188188
throw new Error(`ViteNodeServer: ${id} not found. This is a bug, please report it.`)

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { TransformResult } from 'vite'
22
import { dirname, isAbsolute, relative, resolve } from 'pathe'
33
import type { EncodedSourceMap } from '@jridgewell/trace-mapping'
4+
import { withTrailingSlash } from './utils'
45
import { install } from './source-map-handler'
56

67
interface InstallSourceMapSupportOptions {
@@ -32,7 +33,7 @@ export function withInlineSourcemap(result: TransformResult, options: {
3233
// this is a bug in Vite
3334
// all files should be either absolute to the file system or relative to the source map file
3435
if (isAbsolute(source)) {
35-
const actualPath = (!source.startsWith(options.root) && source.startsWith('/'))
36+
const actualPath = (!source.startsWith(withTrailingSlash(options.root)) && source.startsWith('/'))
3637
? resolve(options.root, source.slice(1))
3738
: source
3839
return relative(dirname(options.filepath), actualPath)

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

+10-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function slash(str: string) {
2222
export const VALID_ID_PREFIX = '/@id/'
2323

2424
export function normalizeRequestId(id: string, base?: string): string {
25-
if (base && id.startsWith(base))
25+
if (base && id.startsWith(withTrailingSlash(base)))
2626
id = `/${id.slice(base.length)}`
2727

2828
// keep drive the same as in process cwd
@@ -105,12 +105,12 @@ export function toFilePath(id: string, root: string): { path: string; exists: bo
105105
if (id.startsWith('/@fs/'))
106106
return { absolute: id.slice(4), exists: true }
107107
// check if /src/module.js -> <root>/src/module.js
108-
if (!id.startsWith(root) && id.startsWith('/')) {
108+
if (!id.startsWith(withTrailingSlash(root)) && id.startsWith('/')) {
109109
const resolved = resolve(root, id.slice(1))
110110
if (existsSync(cleanUrl(resolved)))
111111
return { absolute: resolved, exists: true }
112112
}
113-
else if (id.startsWith(root) && existsSync(cleanUrl(id))) {
113+
else if (id.startsWith(withTrailingSlash(root)) && existsSync(cleanUrl(id))) {
114114
return { absolute: id, exists: true }
115115
}
116116
return { absolute: id, exists: false }
@@ -200,6 +200,13 @@ function traverseBetweenDirs(
200200
}
201201
}
202202

203+
export function withTrailingSlash(path: string): string {
204+
if (path[path.length - 1] !== '/')
205+
return `${path}/`
206+
207+
return path
208+
}
209+
203210
export function createImportMetaEnvProxy() {
204211
// packages/vitest/src/node/plugins/index.ts:146
205212
const booleanKeys = [

0 commit comments

Comments
 (0)
Please sign in to comment.