Repository navigation
Sandbox test TMPDIR so vitest runs stop leaking temp directories - #3638
Merged
ymichael merged 1 commit intoSep 14, 2026
Merged
Conversation
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
deleted the
bb/investigate-leftover-test-folders-thr_xv3fcd5ra5
branch
September 14, 2026 17:23
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Human comments
What was wrong
The repo has 783
mkdtempcall sites and most of the helpers behind them never delete what they create.createFakePluginHostonly removes its storage root indispose()(241 create calls vs 217 disposes, and some paths skip deletion entirely), the agent-runtime harness atpackages/agent-runtime/src/test/runtime-test-harness.ts:211,243hands back paths with no teardown at all, and many test files simply neverrm. A single fullturbo run testleaked 938 directories, which had accumulated to roughly 53,000 stale entries and pushed/tmpto 35,204 entries. The leftovers were split across two roots because Turbo runs tasks in strict env mode, which stripsTMPDIRand makesos.tmpdir()fall back to/tmp; runs that keptTMPDIRlanded in the per-user temp root instead.What changed
Rather than patch 783 call sites, this sandboxes the temp root itself.
defineWorkspaceTestConfiginvitest.shared.tsnow registers a global setup (vitest.global-tmpdir.ts) that pointsTMPDIRat a run-scoped directory and deletes it when the run ends, so everymkdtempcall — 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 aSymbol.forkey 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,TMPDIRrestored). It is mutation-tested: deleting theif (active.references > 0) return;guard makes it fail withexpected '/tmp/bb-vt-OLB013' to be '/tmp/bb-vt-OLB013/bb-vt-vm6NWs'. It is placed inpackages/scripts/testas.mjsto match the existing pattern for testing root-level tooling (build-utils.test.mjs,bump-plugin-sdk.test.mjs), which also sidesteps that package'srootDirconstraint.Full
pnpm exec turbo run test --force --continue(91 tasks, strict env mode), before vs after:The single remaining directory is attributable to other processes sharing
/tmpon the test machine; no stray sandbox roots were left behind. Targeted runs:@bb/agent-runtime413 → 0 leftovers,@get-bb/plugin-sdk112 → 0,@bb/process-utils6 → 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), andpackages/scripts/test/run-cli.test.tsfails only under the--env-mode=looseharness used to thread a probeTMPDIRthrough Turbo, passing under the real strict invocation.Also ran
turbo run typecheck linton@bb/scripts,@bb/process-utils,@get-bb/plugin-sdk, and@bb/server, plusoxfmt --checkon the changed files: all clean.🤖 Generated with Claude Code