Skip to content

Commit

Permalink
fix: back to realpathSync for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Dec 2, 2023
1 parent d3c8f90 commit cafe433
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,15 +631,11 @@ export function copyDir(srcDir: string, destDir: string): void {
// https://github.com/nodejs/node/issues/37737
export let safeRealpath = isWindows ? windowsSafeRealPath : fsp.realpath

async function realpathFallback(path: string) {
return fs.realpathSync(path)
}

// Based on https://github.com/larrybahr/windows-network-drive
// MIT License, Copyright (c) 2017 Larry Bahr
const windowsNetworkMap = new Map()
async function windowsMappedRealpath(path: string) {
const realPath = await fsp.realpath(path)
function windowsMappedRealpath(path: string) {
const realPath = fs.realpathSync.native(path)
if (realPath.startsWith('\\\\')) {
for (const [network, volume] of windowsNetworkMap) {
if (realPath.startsWith(network)) return realPath.replace(network, volume)
Expand All @@ -650,7 +646,7 @@ async function windowsMappedRealpath(path: string) {
const parseNetUseRE = /^(\w+)? +(\w:) +([^ ]+)\s/
let firstSafeRealPathRun = false

async function windowsSafeRealPath(path: string): Promise<string> {
function windowsSafeRealPath(path: string): string {
if (!firstSafeRealPathRun) {
optimizeSafeRealPath()
firstSafeRealPathRun = true
Expand All @@ -662,7 +658,7 @@ async function optimizeSafeRealPath() {
// Skip if using Node <18.10 due to MAX_PATH issue: https://github.com/vitejs/vite/issues/12931
const nodeVersion = process.versions.node.split('.').map(Number)
if (nodeVersion[0] < 18 || (nodeVersion[0] === 18 && nodeVersion[1] < 10)) {
safeRealpath = realpathFallback
safeRealpath = fs.realpathSync
return
}
// Check the availability `fs.realpathSync.native`
Expand All @@ -672,7 +668,7 @@ async function optimizeSafeRealPath() {
await fsp.realpath(path.resolve('./'))
} catch (error) {
if (error.message.includes('EISDIR: illegal operation on a directory')) {
safeRealpath = realpathFallback
safeRealpath = fs.realpathSync
return
}
}
Expand All @@ -686,7 +682,7 @@ async function optimizeSafeRealPath() {
if (m) windowsNetworkMap.set(m[3], m[2])
}
if (windowsNetworkMap.size === 0) {
safeRealpath = fsp.realpath
safeRealpath = fs.realpathSync.native
} else {
safeRealpath = windowsMappedRealpath
}
Expand Down

0 comments on commit cafe433

Please sign in to comment.