Skip to content

Commit 3f4d109

Browse files
authored
fix: avoid clean up while committing deps folder (#12722)
1 parent bedcd8f commit 3f4d109

File tree

2 files changed

+31
-31
lines changed

2 files changed

+31
-31
lines changed

packages/vite/src/node/optimizer/index.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,6 @@ export interface DepOptimizationResult {
165165
* to be able to discard the result
166166
*/
167167
commit: () => Promise<void>
168-
/**
169-
* @deprecated noop
170-
*/
171168
cancel: () => void
172169
}
173170

@@ -510,8 +507,11 @@ export function runOptimizeDeps(
510507

511508
const qualifiedIds = Object.keys(depsInfo)
512509
let cleaned = false
510+
let committed = false
513511
const cleanUp = () => {
514-
if (!cleaned) {
512+
// If commit was already called, ignore the clean up even if a cancel was requested
513+
// This minimizes the chances of leaving the deps cache in a corrupted state
514+
if (!cleaned && !committed) {
515515
cleaned = true
516516
// No need to wait, we can clean up in the background because temp folders
517517
// are unique per run
@@ -525,9 +525,12 @@ export function runOptimizeDeps(
525525
metadata,
526526
cancel: cleanUp,
527527
commit: async () => {
528+
// Ignore clean up requests after this point so the temp folder isn't deleted before
529+
// we finish commiting the new deps cache files to the deps folder
530+
committed = true
531+
528532
// Write metadata file, then commit the processing folder to the global deps cache
529533
// Rewire the file paths from the temporal processing dir to the final deps cache dir
530-
531534
const dataPath = path.join(processingCacheDir, '_metadata.json')
532535
fs.writeFileSync(
533536
dataPath,

packages/vite/src/node/optimizer/optimizer.ts

+23-26
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,12 @@ async function createDepsOptimizer(
175175
}
176176
| undefined
177177

178-
let optimizingNewDeps: Promise<DepOptimizationResult> | undefined
179178
async function close() {
180179
closed = true
181180
await Promise.allSettled([
182181
discover?.cancel(),
183182
depsOptimizer.scanProcessing,
184183
optimizationResult?.cancel(),
185-
optimizingNewDeps,
186184
])
187185
}
188186

@@ -261,27 +259,6 @@ async function createDepsOptimizer(
261259
depOptimizationProcessing = newDepOptimizationProcessing()
262260
}
263261

264-
async function optimizeNewDeps() {
265-
// a successful completion of the optimizeDeps rerun will end up
266-
// creating new bundled version of all current and discovered deps
267-
// in the cache dir and a new metadata info object assigned
268-
// to _metadata. A fullReload is only issued if the previous bundled
269-
// dependencies have changed.
270-
271-
// if the rerun fails, _metadata remains untouched, current discovered
272-
// deps are cleaned, and a fullReload is issued
273-
274-
// All deps, previous known and newly discovered are rebundled,
275-
// respect insertion order to keep the metadata file stable
276-
277-
const knownDeps = prepareKnownDeps()
278-
279-
startNextDiscoveredBatch()
280-
281-
optimizationResult = runOptimizeDeps(config, knownDeps)
282-
return await optimizationResult.result
283-
}
284-
285262
function prepareKnownDeps() {
286263
const knownDeps: Record<string, OptimizedDepInfo> = {}
287264
// Clone optimized info objects, fileHash, browserHash may be changed for them
@@ -297,6 +274,18 @@ async function createDepsOptimizer(
297274
}
298275

299276
async function runOptimizer(preRunResult?: DepOptimizationResult) {
277+
// a successful completion of the optimizeDeps rerun will end up
278+
// creating new bundled version of all current and discovered deps
279+
// in the cache dir and a new metadata info object assigned
280+
// to _metadata. A fullReload is only issued if the previous bundled
281+
// dependencies have changed.
282+
283+
// if the rerun fails, _metadata remains untouched, current discovered
284+
// deps are cleaned, and a fullReload is issued
285+
286+
// All deps, previous known and newly discovered are rebundled,
287+
// respect insertion order to keep the metadata file stable
288+
300289
const isRerun = firstRunCalled
301290
firstRunCalled = true
302291

@@ -314,9 +303,17 @@ async function createDepsOptimizer(
314303
currentlyProcessing = true
315304

316305
try {
317-
const processingResult =
318-
preRunResult ?? (await (optimizingNewDeps = optimizeNewDeps()))
319-
optimizingNewDeps = undefined
306+
let processingResult: DepOptimizationResult
307+
if (preRunResult) {
308+
processingResult = preRunResult
309+
} else {
310+
const knownDeps = prepareKnownDeps()
311+
startNextDiscoveredBatch()
312+
313+
optimizationResult = runOptimizeDeps(config, knownDeps)
314+
processingResult = await optimizationResult.result
315+
optimizationResult = undefined
316+
}
320317

321318
if (closed) {
322319
currentlyProcessing = false

0 commit comments

Comments
 (0)