Skip to content

Commit

Permalink
fix: EPERM error on Windows when processing dependencies (#8235)
Browse files Browse the repository at this point in the history
Co-authored-by: patak-dev <matias.capeletto@gmail.com>
  • Loading branch information
mattnathan and patak-dev committed Jun 6, 2022
1 parent 8d7bac4 commit dfe4307
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
lookupFile,
normalizeId,
normalizePath,
removeDir,
removeDirSync,
renameDir,
writeFile
Expand Down Expand Up @@ -539,7 +540,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
35 changes: 35 additions & 0 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,9 @@ export function removeDirSync(dir: string) {
}
}

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

export function ensureWatchedFile(
Expand Down Expand Up @@ -804,6 +807,38 @@ function gracefulRename(
})
}

const GRACEFUL_REMOVE_DIR_TIMEOUT = 5000
function gracefulRemoveDir(
dir: string,
cb: (error: NodeJS.ErrnoException | null) => void
) {
const rmdir = fs.rm ?? fs.rmdir // TODO: Remove after support for Node 12 is dropped
const start = Date.now()
let backoff = 0
rmdir(dir, { recursive: true }, function CB(er) {
if (er) {
if (
(er.code === 'ENOTEMPTY' ||
er.code === 'EACCES' ||
er.code === 'EPERM') &&
Date.now() - start < GRACEFUL_REMOVE_DIR_TIMEOUT
) {
setTimeout(function () {
rmdir(dir, { recursive: true }, CB)
}, backoff)
if (backoff < 100) backoff += 10
return
}

if (er.code === 'ENOENT') {
er = null
}
}

if (cb) cb(er)
})
}

export function emptyCssComments(raw: string) {
return raw.replace(multilineCommentsRE, (s) => ' '.repeat(s.length))
}

0 comments on commit dfe4307

Please sign in to comment.