Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unique dep optimizer temp folders #12252

Merged
merged 2 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
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
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 @@ -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())
)
}

Expand Down Expand Up @@ -1232,3 +1236,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