From 38ce81ceb86d42c27ec39eefa8091f47f6f25967 Mon Sep 17 00:00:00 2001 From: patak Date: Thu, 2 Mar 2023 13:32:55 +0100 Subject: [PATCH] fix: unique dep optimizer temp folders (#12252) --- packages/vite/src/node/optimizer/index.ts | 29 ++++++++++++++++++++++- packages/vite/src/node/server/index.ts | 5 ++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 28b14c8da9229c..45768d81a1b794 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -1,4 +1,5 @@ import fs from 'node:fs' +import fsp from 'node:fs/promises' import path from 'node:path' import { performance } from 'node:perf_hooks' import _debug from 'debug' @@ -879,7 +880,10 @@ export function getDepsCacheDir(config: ResolvedConfig, ssr: boolean): string { function getProcessingDepsCacheDir(config: ResolvedConfig, ssr: boolean) { return ( - getDepsCacheDirPrefix(config) + getDepsCacheSuffix(config, ssr) + '_temp' + getDepsCacheDirPrefix(config) + + getDepsCacheSuffix(config, ssr) + + '_temp_' + + getHash(Date.now().toString()) ) } @@ -1247,3 +1251,26 @@ export async function optimizedDepNeedsInterop( } return depInfo?.needsInterop } + +const MAX_TEMP_DIR_AGE_MS = 24 * 60 * 60 * 1000 +export async function cleanupDepsCacheStaleDirs( + config: ResolvedConfig, +): Promise { + try { + const cacheDir = path.resolve(config.cacheDir) + if (fs.existsSync(cacheDir)) { + const dirents = await fsp.readdir(cacheDir, { withFileTypes: true }) + for (const dirent of dirents) { + if (dirent.isDirectory() && dirent.name.includes('_temp_')) { + const tempDirPath = path.resolve(config.cacheDir, dirent.name) + const { mtime } = await fsp.stat(tempDirPath) + if (Date.now() - mtime.getTime() > MAX_TEMP_DIR_AGE_MS) { + await removeDir(tempDirPath) + } + } + } + } + } catch (err) { + config.logger.error(err) + } +} diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 0edf8692835375..2dae884d1951a5 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -34,6 +34,7 @@ import { cjsSsrResolveExternals } from '../ssr/ssrExternal' import { ssrFixStacktrace, ssrRewriteStacktrace } from '../ssr/ssrStacktrace' import { ssrTransform } from '../ssr/ssrTransform' import { + cleanupDepsCacheStaleDirs, getDepsOptimizer, initDepsOptimizer, initDevSsrDepsOptimizer, @@ -690,6 +691,10 @@ export async function createServer( await initServer() } + // Fire a clean up of stale cache dirs, in case old processes didn't + // terminate correctly. Don't await this promise + cleanupDepsCacheStaleDirs(config) + return server }