From 3f4d109ea4ca03c6506b7561c0520e45d8eacf42 Mon Sep 17 00:00:00 2001 From: patak Date: Mon, 3 Apr 2023 17:09:53 +0200 Subject: [PATCH] fix: avoid clean up while committing deps folder (#12722) --- packages/vite/src/node/optimizer/index.ts | 13 +++-- packages/vite/src/node/optimizer/optimizer.ts | 49 +++++++++---------- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index d2079064004c69..a290e7a81eca5b 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -165,9 +165,6 @@ export interface DepOptimizationResult { * to be able to discard the result */ commit: () => Promise - /** - * @deprecated noop - */ cancel: () => void } @@ -510,8 +507,11 @@ export function runOptimizeDeps( const qualifiedIds = Object.keys(depsInfo) let cleaned = false + let committed = false const cleanUp = () => { - if (!cleaned) { + // If commit was already called, ignore the clean up even if a cancel was requested + // This minimizes the chances of leaving the deps cache in a corrupted state + if (!cleaned && !committed) { cleaned = true // No need to wait, we can clean up in the background because temp folders // are unique per run @@ -525,9 +525,12 @@ export function runOptimizeDeps( metadata, cancel: cleanUp, commit: async () => { + // Ignore clean up requests after this point so the temp folder isn't deleted before + // we finish commiting the new deps cache files to the deps folder + committed = true + // Write metadata file, then commit the processing folder to the global deps cache // Rewire the file paths from the temporal processing dir to the final deps cache dir - const dataPath = path.join(processingCacheDir, '_metadata.json') fs.writeFileSync( dataPath, diff --git a/packages/vite/src/node/optimizer/optimizer.ts b/packages/vite/src/node/optimizer/optimizer.ts index 56c960da34fdc0..f4f60f291eeeef 100644 --- a/packages/vite/src/node/optimizer/optimizer.ts +++ b/packages/vite/src/node/optimizer/optimizer.ts @@ -175,14 +175,12 @@ async function createDepsOptimizer( } | undefined - let optimizingNewDeps: Promise | undefined async function close() { closed = true await Promise.allSettled([ discover?.cancel(), depsOptimizer.scanProcessing, optimizationResult?.cancel(), - optimizingNewDeps, ]) } @@ -261,27 +259,6 @@ async function createDepsOptimizer( depOptimizationProcessing = newDepOptimizationProcessing() } - async function optimizeNewDeps() { - // a successful completion of the optimizeDeps rerun will end up - // creating new bundled version of all current and discovered deps - // in the cache dir and a new metadata info object assigned - // to _metadata. A fullReload is only issued if the previous bundled - // dependencies have changed. - - // if the rerun fails, _metadata remains untouched, current discovered - // deps are cleaned, and a fullReload is issued - - // All deps, previous known and newly discovered are rebundled, - // respect insertion order to keep the metadata file stable - - const knownDeps = prepareKnownDeps() - - startNextDiscoveredBatch() - - optimizationResult = runOptimizeDeps(config, knownDeps) - return await optimizationResult.result - } - function prepareKnownDeps() { const knownDeps: Record = {} // Clone optimized info objects, fileHash, browserHash may be changed for them @@ -297,6 +274,18 @@ async function createDepsOptimizer( } async function runOptimizer(preRunResult?: DepOptimizationResult) { + // a successful completion of the optimizeDeps rerun will end up + // creating new bundled version of all current and discovered deps + // in the cache dir and a new metadata info object assigned + // to _metadata. A fullReload is only issued if the previous bundled + // dependencies have changed. + + // if the rerun fails, _metadata remains untouched, current discovered + // deps are cleaned, and a fullReload is issued + + // All deps, previous known and newly discovered are rebundled, + // respect insertion order to keep the metadata file stable + const isRerun = firstRunCalled firstRunCalled = true @@ -314,9 +303,17 @@ async function createDepsOptimizer( currentlyProcessing = true try { - const processingResult = - preRunResult ?? (await (optimizingNewDeps = optimizeNewDeps())) - optimizingNewDeps = undefined + let processingResult: DepOptimizationResult + if (preRunResult) { + processingResult = preRunResult + } else { + const knownDeps = prepareKnownDeps() + startNextDiscoveredBatch() + + optimizationResult = runOptimizeDeps(config, knownDeps) + processingResult = await optimizationResult.result + optimizationResult = undefined + } if (closed) { currentlyProcessing = false