From 9d1b572052cb80f4d69e3a2699124877ea61ed63 Mon Sep 17 00:00:00 2001 From: patak Date: Wed, 1 Mar 2023 21:51:04 +0100 Subject: [PATCH 1/2] fix: unique dep optimizer temp folders --- 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 b86c7d41948bfc..d2f8a09641248b 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 { stat as fsStat, readdir } from 'node:fs/promises' import path from 'node:path' import { performance } from 'node:perf_hooks' import _debug from 'debug' @@ -864,7 +865,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()) ) } @@ -1232,3 +1236,26 @@ export async function optimizedDepNeedsInterop( } return depInfo?.needsInterop } + +const MAX_TEMP_FOLDER_AGE_MS = 24 * 60 * 60 * 1000 +export async function cleanupTemporalDepsCache( + config: ResolvedConfig, +): Promise { + try { + const cacheDir = path.resolve(config.cacheDir) + if (fs.existsSync(cacheDir)) { + const dirents = await 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 fsStat(tempDirPath) + if (Date.now() - mtime.getTime() > MAX_TEMP_FOLDER_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..aeb560cc50fa7f 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 { + cleanupTemporalDepsCache, getDepsOptimizer, initDepsOptimizer, initDevSsrDepsOptimizer, @@ -690,6 +691,10 @@ export async function createServer( await initServer() } + // Fire a clean up of old temp cache folders, in case old processes didn't + // terminate correctly. Don't await this promise + cleanupTemporalDepsCache(config) + return server } From 2b15ccf923a906dbcc2f0e086e4fb930a5836f5b Mon Sep 17 00:00:00 2001 From: patak Date: Thu, 2 Mar 2023 13:16:27 +0100 Subject: [PATCH 2/2] chore: apply suggestions --- packages/vite/src/node/optimizer/index.ts | 12 ++++++------ packages/vite/src/node/server/index.ts | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index d2f8a09641248b..930c51f1fd615d 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -1,5 +1,5 @@ import fs from 'node:fs' -import { stat as fsStat, readdir } from 'node:fs/promises' +import fsp from 'node:fs/promises' import path from 'node:path' import { performance } from 'node:perf_hooks' import _debug from 'debug' @@ -1237,19 +1237,19 @@ export async function optimizedDepNeedsInterop( return depInfo?.needsInterop } -const MAX_TEMP_FOLDER_AGE_MS = 24 * 60 * 60 * 1000 -export async function cleanupTemporalDepsCache( +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 readdir(cacheDir, { withFileTypes: true }) + 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 fsStat(tempDirPath) - if (Date.now() - mtime.getTime() > MAX_TEMP_FOLDER_AGE_MS) { + const { mtime } = await fsp.stat(tempDirPath) + if (Date.now() - mtime.getTime() > MAX_TEMP_DIR_AGE_MS) { await removeDir(tempDirPath) } } diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index aeb560cc50fa7f..2dae884d1951a5 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -34,7 +34,7 @@ import { cjsSsrResolveExternals } from '../ssr/ssrExternal' import { ssrFixStacktrace, ssrRewriteStacktrace } from '../ssr/ssrStacktrace' import { ssrTransform } from '../ssr/ssrTransform' import { - cleanupTemporalDepsCache, + cleanupDepsCacheStaleDirs, getDepsOptimizer, initDepsOptimizer, initDevSsrDepsOptimizer, @@ -691,9 +691,9 @@ export async function createServer( await initServer() } - // Fire a clean up of old temp cache folders, in case old processes didn't + // Fire a clean up of stale cache dirs, in case old processes didn't // terminate correctly. Don't await this promise - cleanupTemporalDepsCache(config) + cleanupDepsCacheStaleDirs(config) return server }