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: EPERM error on Windows when processing dependencies #8235

Merged
merged 4 commits into from May 22, 2022
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion packages/vite/src/node/optimizer/index.ts
Expand Up @@ -15,6 +15,7 @@ import {
lookupFile,
normalizeId,
normalizePath,
removeDir,
removeDirSync,
renameDir,
writeFile
Expand Down Expand Up @@ -534,7 +535,7 @@ export async function runOptimizeDeps(
async function commitProcessingDepsCacheSync() {
// Processing is done, we can now replace the depsCacheDir with processingCacheDir
// Rewire the file paths from the temporal processing dir to the final deps cache dir
removeDirSync(depsCacheDir)
await removeDir(depsCacheDir)
await renameDir(processingCacheDir, depsCacheDir)
}

Expand Down
28 changes: 28 additions & 0 deletions packages/vite/src/node/utils.ts
Expand Up @@ -534,6 +534,34 @@ export function removeDirSync(dir: string) {
}
}

const REMOVE_DIR_TIMEOUT = 5000
const rmdir = promisify(fs.rm ?? fs.rmdir) // TODO: Remove after support for Node 12 is dropped
patak-dev marked this conversation as resolved.
Show resolved Hide resolved
export async function removeDir(dir: string) {
mattnathan marked this conversation as resolved.
Show resolved Hide resolved
const t0 = Date.now()
const attempt = async () => {
try {
await rmdir(dir, { recursive: true })
} catch (e) {
if (e.code === 'ENOENT') {
return
}
if (e.code === 'ENOTEMPTY' || e.code === 'EPERM') {
if (Date.now() - t0 < REMOVE_DIR_TIMEOUT) {
// There's no delay here, testing showed it wasn't required.
// If the remove doesn't succeed the first time, it almost always succeed immediately afterwards.
// In rare cases two or three attempts are needed before the dir is successfully removed,
// typically taking <1s total.
await attempt()
return
mattnathan marked this conversation as resolved.
Show resolved Hide resolved
}
}

throw e
}
}
await attempt()
}

export const renameDir = isWindows ? promisify(gracefulRename) : fs.renameSync

export function ensureWatchedFile(
Expand Down