Skip to content

Commit

Permalink
fix: don't use resolved paths if they don't actually exist (#1868)
Browse files Browse the repository at this point in the history
Previously `toFilePath` would treat _any_ absolute path that doesn't
start with the `root` (usually the current working directory) as a
relative path, which resulted in some seriously mangled paths. Now we
actually check if that resolved path exists before committing to it.

This fixes #1864.
  • Loading branch information
simon-abbott committed Oct 31, 2022
1 parent 2815372 commit 6c3d708
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 79 deletions.
20 changes: 13 additions & 7 deletions packages/vite-node/src/utils.ts
@@ -1,4 +1,5 @@
import { fileURLToPath, pathToFileURL } from 'url'
import { existsSync } from 'fs'
import { relative, resolve } from 'pathe'
import type { TransformResult } from 'vite'
import { isNodeBuiltin } from 'mlly'
Expand Down Expand Up @@ -75,13 +76,18 @@ export function pathFromRoot(root: string, filename: string) {
}

export function toFilePath(id: string, root: string): string {
let absolute = id.startsWith('/@fs/')
? id.slice(4)
: id.startsWith(root)
? id
: id.startsWith('/')
? resolve(root, id.slice(1))
: id
let absolute = (() => {
if (id.startsWith('/@fs/'))
return id.slice(4)
if (!id.startsWith(root) && id.startsWith('/')) {
const resolved = resolve(root, id.slice(1))
// The resolved path can have query values. Remove them before checking
// the file path.
if (existsSync(resolved.replace(/\?.*$/, '')))
return resolved
}
return id
})()

if (absolute.startsWith('//'))
absolute = absolute.slice(1)
Expand Down
116 changes: 44 additions & 72 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6c3d708

Please sign in to comment.