From 71eb4b8930dc77b7e91e86c13f222bd5a71075c0 Mon Sep 17 00:00:00 2001 From: patak Date: Mon, 3 Apr 2023 14:24:03 +0200 Subject: [PATCH 1/2] fix: avoid clean up while committing deps folder --- packages/vite/src/node/optimizer/index.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index d2079064004c69..f7f543067e1a1b 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -510,8 +510,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 +528,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, From 84bba73c91343063806c767d022ecdd27092cf0d Mon Sep 17 00:00:00 2001 From: patak Date: Mon, 3 Apr 2023 15:09:33 +0200 Subject: [PATCH 2/2] refactor: simplify awaiting for optimization result on close --- packages/vite/src/node/optimizer/index.ts | 3 -- packages/vite/src/node/optimizer/optimizer.ts | 49 +++++++++---------- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index f7f543067e1a1b..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 } 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