Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid clean up while committing deps folder #12722

Merged
merged 2 commits into from Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions packages/vite/src/node/optimizer/index.ts
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
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