Skip to content

Commit

Permalink
fix: unique dep optimizer temp folders (#12252)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev authored Mar 2, 2023
1 parent c35e100 commit 38ce81c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
29 changes: 28 additions & 1 deletion packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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())
)
}

Expand Down Expand Up @@ -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<void> {
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)
}
}
5 changes: 5 additions & 0 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 38ce81c

Please sign in to comment.