Cotton code
33 lines · 1.5 KB · TypeScriptRaw
// One private temp directory per `pnpm test:scripts` run, removed when the run// ends — `globalSetup` in `vitest.scripts.config.ts`.//// WHY. The scripts suite makes its scratch space with `mkdtemp(tmpdir(), …)` in// dozens of files and helpers, and most of them never remove it. On the CI host// that came to about 200,000 directories in /tmp by 29 Sep 2026 — 62,921 of// them `cotton-fake-*` from `helpers/fakeMcpServer.ts` alone, about 3,000 more// every day — and every Mac that runs the suite leaks the same way. Fixing each// file one by one leaves the next new test free to leak again; pointing the// whole run at a directory that is deleted afterwards covers every test,// helper and child script (a shell `mktemp` reads the same `TMPDIR`).//// It runs in the main process BEFORE the fork pool starts, so every worker and// everything a worker spawns inherits the new `TMPDIR`. The name is short on// purpose: a Mac's own `TMPDIR` is already long, and a unix socket path under it// has a 104-byte ceiling.import { mkdtempSync, rmSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";/** The prefix of the run's directory, so a guard can tell the run is inside one. */export const RUN_TMP_PREFIX = "cst-";export default function setup(): () => void {  const dir = mkdtempSync(join(tmpdir(), RUN_TMP_PREFIX));  process.env.TMPDIR = dir;  process.env.TMP = dir;  process.env.TEMP = dir;  return () => {    rmSync(dir, { recursive: true, force: true, maxRetries: 3 });  };}