diff --git a/packages/cli-v3/src/dev/devSession.ts b/packages/cli-v3/src/dev/devSession.ts index 8b4068ff44..482ebf6fc1 100644 --- a/packages/cli-v3/src/dev/devSession.ts +++ b/packages/cli-v3/src/dev/devSession.ts @@ -1,6 +1,5 @@ import { ResolvedConfig } from "@trigger.dev/core/v3/build"; import * as esbuild from "esbuild"; -import { existsSync, mkdirSync, rmSync } from "node:fs"; import { CliApiClient } from "../apiClient.js"; import { BundleResult, @@ -59,7 +58,7 @@ export async function startDevSession({ clearTmpDirs(rawConfig.workingDir); const destination = getTmpDir(rawConfig.workingDir, "build", keepTmpFiles); // Create shared store directory for deduplicating chunk files across rebuilds - const storeDir = getStoreDir(rawConfig.workingDir); + const storeDir = getStoreDir(rawConfig.workingDir, keepTmpFiles); const runtime = await startWorkerRuntime({ name, diff --git a/packages/cli-v3/src/utilities/tempDirectories.ts b/packages/cli-v3/src/utilities/tempDirectories.ts index 1de3a8d09b..2f3859d209 100644 --- a/packages/cli-v3/src/utilities/tempDirectories.ts +++ b/packages/cli-v3/src/utilities/tempDirectories.ts @@ -63,10 +63,23 @@ export function clearTmpDirs(projectRoot: string | undefined) { * Gets the shared store directory for content-addressable build outputs. * This directory persists across rebuilds and is used to deduplicate * identical chunk files between build versions. + * Automatically cleaned up when the process exits. */ -export function getStoreDir(projectRoot: string | undefined): string { +export function getStoreDir(projectRoot: string | undefined, keep: boolean = false): string { projectRoot ??= process.cwd(); const storeDir = path.join(projectRoot, ".trigger", "tmp", "store"); fs.mkdirSync(storeDir, { recursive: true }); + + // Register exit handler to clean up the store directory + if (!keep && !process.env.KEEP_TMP_DIRS) { + onExit(() => { + try { + fs.rmSync(storeDir, { recursive: true, force: true }); + } catch (e) { + // This sometimes fails on Windows with EBUSY + } + }); + } + return storeDir; }