Skip to content

Sandbox test TMPDIR so vitest runs stop leaking temp directories - #3638

Merged
ymichael merged 1 commit into
mainfrom
bb/investigate-leftover-test-folders-thr_xv3fcd5ra5
Sep 14, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/investigate-leftover-test-folders-thr_xv3fcd5ra5

Conversation

@ymichael

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

The repo has 783 mkdtemp call sites and most of the helpers behind them never delete what they create. createFakePluginHost only removes its storage root in dispose() (241 create calls vs 217 disposes, and some paths skip deletion entirely), the agent-runtime harness at packages/agent-runtime/src/test/runtime-test-harness.ts:211,243 hands back paths with no teardown at all, and many test files simply never rm. A single full turbo run test leaked 938 directories, which had accumulated to roughly 53,000 stale entries and pushed /tmp to 35,204 entries. The leftovers were split across two roots because Turbo runs tasks in strict env mode, which strips TMPDIR and makes os.tmpdir() fall back to /tmp; runs that kept TMPDIR landed in the per-user temp root instead.

What changed

Rather than patch 783 call sites, this sandboxes the temp root itself. defineWorkspaceTestConfig in vitest.shared.ts now registers a global setup (vitest.global-tmpdir.ts) that points TMPDIR at a run-scoped directory and deletes it when the run ends, so every mkdtemp call — existing and future — lands inside something guaranteed to be removed.

One non-obvious detail drove the shape of the module: Vitest invokes global setup once per project, not once per run (three times for @get-bb/plugin-sdk). Without reference counting, the first project to finish would delete the sandbox out from under the projects still running. The sandbox is therefore refcounted on a Symbol.for key and only torn down after the last project finishes.

No wire, CLI, or plugin-API surfaces are touched; this is test infrastructure only.

How you verified

Added packages/scripts/test/vitest-global-tmpdir.test.mjs, covering the refcount contract (shared root across projects, no teardown until the last release, TMPDIR restored). It is mutation-tested: deleting the if (active.references > 0) return; guard makes it fail with expected '/tmp/bb-vt-OLB013' to be '/tmp/bb-vt-OLB013/bb-vt-vm6NWs'. It is placed in packages/scripts/test as .mjs to match the existing pattern for testing root-level tooling (build-utils.test.mjs, bump-plugin-sdk.test.mjs), which also sidesteps that package's rootDir constraint.

Full pnpm exec turbo run test --force --continue (91 tasks, strict env mode), before vs after:

leaked dirs failing tests failing packages
baseline 938 32 5
this branch 1 22 4

The single remaining directory is attributable to other processes sharing /tmp on the test machine; no stray sandbox roots were left behind. Targeted runs: @bb/agent-runtime 413 → 0 leftovers, @get-bb/plugin-sdk 112 → 0, @bb/process-utils 6 → 0, all green.

Failing tests went down, not up. The one failure unique to the fixed run (apps/server/test/services/plugins/plugin-authoring-docs.test.ts) passes 2/2 in isolation and appeared on the baseline side of an earlier comparison run, so it is load flakiness on a busy machine rather than a regression. Two measurement artifacts were ruled out along the way: bb-app's 6 failures are pre-existing (identical list on both sides), and packages/scripts/test/run-cli.test.ts fails only under the --env-mode=loose harness used to thread a probe TMPDIR through Turbo, passing under the real strict invocation.

Also ran turbo run typecheck lint on @bb/scripts, @bb/process-utils, @get-bb/plugin-sdk, and @bb/server, plus oxfmt --check on the changed files: all clean.

AGENT GENERATED

🤖 Generated with Claude Code

Root cause: 783 `mkdtemp` call sites across the repo create directories
under `os.tmpdir()`, and most helpers never delete them. `createFakePluginHost`
only removes its storage root in `dispose()`, the agent-runtime harness hands
back paths with no teardown, and many test files simply never `rm`. A single
full `turbo run test` leaked 938 directories, which had accumulated to ~51,600
stale entries across /tmp and the per-user temp root.

Change: `defineWorkspaceTestConfig` now registers a global setup that points
`TMPDIR` at a run-scoped sandbox and deletes it when the run ends, so every
`mkdtemp` call lands inside a directory that is guaranteed to be removed.
Vitest invokes global setup once per project, so the sandbox is reference
counted and only torn down after the last project finishes.

Verification: full `turbo run test` leaked 938 directories before and 1 after
(that one is attributable to other bb threads sharing /tmp), with no stray
sandbox roots. Failing tests dropped from 32 to 22; the single failure unique
to the fixed run passes 2/2 in isolation and is load flakiness on a busy
machine. Targeted runs confirm @bb/agent-runtime 413 -> 0, @get-bb/plugin-sdk
112 -> 0, and @bb/process-utils 6 -> 0 leftovers, all green.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@ymichael
ymichael merged commit cca4bd9 into main Sep 14, 2026
16 checks passed
@ymichael
ymichael deleted the bb/investigate-leftover-test-folders-thr_xv3fcd5ra5 branch September 14, 2026 17:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant