Skip to content

Commit

Permalink
fix: avoid clean up while committing deps folder (#12722)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev authored Apr 3, 2023
1 parent bedcd8f commit 3f4d109
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
13 changes: 8 additions & 5 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,6 @@ export interface DepOptimizationResult {
* to be able to discard the result
*/
commit: () => Promise<void>
/**
* @deprecated noop
*/
cancel: () => void
}

Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
49 changes: 23 additions & 26 deletions packages/vite/src/node/optimizer/optimizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,12 @@ async function createDepsOptimizer(
}
| undefined

let optimizingNewDeps: Promise<DepOptimizationResult> | undefined
async function close() {
closed = true
await Promise.allSettled([
discover?.cancel(),
depsOptimizer.scanProcessing,
optimizationResult?.cancel(),
optimizingNewDeps,
])
}

Expand Down Expand Up @@ -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<string, OptimizedDepInfo> = {}
// Clone optimized info objects, fileHash, browserHash may be changed for them
Expand All @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 3f4d109

Please sign in to comment.