Skip to content

Commit 38ce81c

Browse files
authoredMar 2, 2023
fix: unique dep optimizer temp folders (#12252)
1 parent c35e100 commit 38ce81c

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed
 

‎packages/vite/src/node/optimizer/index.ts

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'node:fs'
2+
import fsp from 'node:fs/promises'
23
import path from 'node:path'
34
import { performance } from 'node:perf_hooks'
45
import _debug from 'debug'
@@ -879,7 +880,10 @@ export function getDepsCacheDir(config: ResolvedConfig, ssr: boolean): string {
879880

880881
function getProcessingDepsCacheDir(config: ResolvedConfig, ssr: boolean) {
881882
return (
882-
getDepsCacheDirPrefix(config) + getDepsCacheSuffix(config, ssr) + '_temp'
883+
getDepsCacheDirPrefix(config) +
884+
getDepsCacheSuffix(config, ssr) +
885+
'_temp_' +
886+
getHash(Date.now().toString())
883887
)
884888
}
885889

@@ -1247,3 +1251,26 @@ export async function optimizedDepNeedsInterop(
12471251
}
12481252
return depInfo?.needsInterop
12491253
}
1254+
1255+
const MAX_TEMP_DIR_AGE_MS = 24 * 60 * 60 * 1000
1256+
export async function cleanupDepsCacheStaleDirs(
1257+
config: ResolvedConfig,
1258+
): Promise<void> {
1259+
try {
1260+
const cacheDir = path.resolve(config.cacheDir)
1261+
if (fs.existsSync(cacheDir)) {
1262+
const dirents = await fsp.readdir(cacheDir, { withFileTypes: true })
1263+
for (const dirent of dirents) {
1264+
if (dirent.isDirectory() && dirent.name.includes('_temp_')) {
1265+
const tempDirPath = path.resolve(config.cacheDir, dirent.name)
1266+
const { mtime } = await fsp.stat(tempDirPath)
1267+
if (Date.now() - mtime.getTime() > MAX_TEMP_DIR_AGE_MS) {
1268+
await removeDir(tempDirPath)
1269+
}
1270+
}
1271+
}
1272+
}
1273+
} catch (err) {
1274+
config.logger.error(err)
1275+
}
1276+
}

‎packages/vite/src/node/server/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { cjsSsrResolveExternals } from '../ssr/ssrExternal'
3434
import { ssrFixStacktrace, ssrRewriteStacktrace } from '../ssr/ssrStacktrace'
3535
import { ssrTransform } from '../ssr/ssrTransform'
3636
import {
37+
cleanupDepsCacheStaleDirs,
3738
getDepsOptimizer,
3839
initDepsOptimizer,
3940
initDevSsrDepsOptimizer,
@@ -690,6 +691,10 @@ export async function createServer(
690691
await initServer()
691692
}
692693

694+
// Fire a clean up of stale cache dirs, in case old processes didn't
695+
// terminate correctly. Don't await this promise
696+
cleanupDepsCacheStaleDirs(config)
697+
693698
return server
694699
}
695700

0 commit comments

Comments
 (0)
Please sign in to comment.