Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ export function runOptimizeDeps(
const depsCacheDir = getDepsCacheDir(resolvedConfig, ssr)
const processingCacheDir = getProcessingDepsCacheDir(resolvedConfig, ssr)

// Create a temporal directory so we don't need to delete optimized deps
// Create a temporary directory so we don't need to delete optimized deps
// until they have been processed. This also avoids leaving the deps cache
// directory in a corrupted state if there is an error
fs.mkdirSync(processingCacheDir, { recursive: true })
Expand Down Expand Up @@ -517,7 +517,7 @@ export function runOptimizeDeps(
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
// Rewire the file paths from the temporary processing dir to the final deps cache dir
const dataPath = path.join(processingCacheDir, '_metadata.json')
debug?.(colors.green(`creating _metadata.json in ${processingCacheDir}`))
fs.writeFileSync(
Expand All @@ -526,39 +526,39 @@ export function runOptimizeDeps(
)

// In order to minimize the time where the deps folder isn't in a consistent state,
// we first rename the old depsCacheDir to a temporal path, then we rename the
// we first rename the old depsCacheDir to a temporary path, then we rename the
// new processing cache dir to the depsCacheDir. In systems where doing so in sync
// is safe, we do an atomic operation (at least for this thread). For Windows, we
// found there are cases where the rename operation may finish before it's done
// so we do a graceful rename checking that the folder has been properly renamed.
// We found that the rename-rename (then delete the old folder in the background)
// is safer than a delete-rename operation.
const temporalPath = depsCacheDir + getTempSuffix()
const temporaryPath = depsCacheDir + getTempSuffix()
const depsCacheDirPresent = fs.existsSync(depsCacheDir)
if (isWindows) {
if (depsCacheDirPresent) {
debug?.(colors.green(`renaming ${depsCacheDir} to ${temporalPath}`))
await safeRename(depsCacheDir, temporalPath)
debug?.(colors.green(`renaming ${depsCacheDir} to ${temporaryPath}`))
await safeRename(depsCacheDir, temporaryPath)
}
debug?.(
colors.green(`renaming ${processingCacheDir} to ${depsCacheDir}`),
)
await safeRename(processingCacheDir, depsCacheDir)
} else {
if (depsCacheDirPresent) {
debug?.(colors.green(`renaming ${depsCacheDir} to ${temporalPath}`))
fs.renameSync(depsCacheDir, temporalPath)
debug?.(colors.green(`renaming ${depsCacheDir} to ${temporaryPath}`))
fs.renameSync(depsCacheDir, temporaryPath)
}
debug?.(
colors.green(`renaming ${processingCacheDir} to ${depsCacheDir}`),
)
fs.renameSync(processingCacheDir, depsCacheDir)
}

// Delete temporal path in the background
// Delete temporary path in the background
if (depsCacheDirPresent) {
debug?.(colors.green(`removing cache temp dir ${temporalPath}`))
fsp.rm(temporalPath, { recursive: true, force: true })
debug?.(colors.green(`removing cache temp dir ${temporaryPath}`))
fsp.rm(temporaryPath, { recursive: true, force: true })
}
},
}
Expand Down