Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: "EISDIR: illegal operation on a directory, realpath" error on RA… #13655

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 16 additions & 17 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,32 +571,22 @@ export function copyDir(srcDir: string, destDir: string): void {
}
}

/**
* Wrapper to prevent `fs.realpathSync.native` errors in Windows virtual and RAM disks
* that bypass the Volume Mount Manager, in programs such as imDisk
* @param path - a path to a file
* @returns {string}
*/
function wrapRealpathSyncNative(path: string): string {
try {
return fs.realpathSync.native(path)
} catch {
return fs.realpathSync(path)
}
}

// `fs.realpathSync.native` resolves differently in Windows network drive,
// causing file read errors. skip for now.
// https://github.com/nodejs/node/issues/37737
export let safeRealpathSync = isWindows
? windowsSafeRealPathSync
: fs.realpathSync.native

// when using `fs.realpathSync.native` get errors in Windows virtual and RAM disks
// that bypass the Volume Mount Manager, in programs such as imDisk
let supportedRealpathSync = fs.realpathSync.native

// Based on https://github.com/larrybahr/windows-network-drive
// MIT License, Copyright (c) 2017 Larry Bahr
const windowsNetworkMap = new Map()
function windowsMappedRealpathSync(path: string) {
const realPath = wrapRealpathSyncNative(path)
const realPath = supportedRealpathSync(path)
if (realPath.startsWith('\\\\')) {
for (const [network, volume] of windowsNetworkMap) {
if (realPath.startsWith(network)) return realPath.replace(network, volume)
Expand All @@ -622,7 +612,16 @@ function optimizeSafeRealPathSync() {
safeRealpathSync = fs.realpathSync
return
}

// Check the availability `fs.realpathSync.native`
// in Windows virtual and RAM disks that bypass the Volume Mount Manager
// get the error EISDIR: illegal operation on a directory
try {
fs.realpathSync.native(path.resolve('./'))
} catch (error) {
if (~error.message.indexOf('EISDIR: illegal operation on a directory')) {
patak-dev marked this conversation as resolved.
Show resolved Hide resolved
supportedRealpathSync = fs.realpathSync
datacutter marked this conversation as resolved.
Show resolved Hide resolved
}
}
exec('net use', (error, stdout) => {
if (error) return
const lines = stdout.split('\n')
Expand All @@ -633,7 +632,7 @@ function optimizeSafeRealPathSync() {
if (m) windowsNetworkMap.set(m[3], m[2])
}
if (windowsNetworkMap.size === 0) {
safeRealpathSync = wrapRealpathSyncNative
safeRealpathSync = supportedRealpathSync
} else {
safeRealpathSync = windowsMappedRealpathSync
}
Expand Down