Skip to content

fix: sandbox test homedir to prevent stray files in ~#2417

Merged
louisgv merged 1 commit intomainfrom
fix/mock-homedir-in-preload
Mar 10, 2026
Merged

fix: sandbox test homedir to prevent stray files in ~#2417
louisgv merged 1 commit intomainfrom
fix/mock-homedir-in-preload

Conversation

@la14-1
Copy link
Copy Markdown
Member

@la14-1 la14-1 commented Mar 10, 2026

Summary

  • Bun's os.homedir() ignores process.env.HOME — it reads from getpwuid() and caches the result. Named imports (import { homedir } from "node:os") capture a binding to the native function that cannot be patched by test preloads.
  • This caused 11 test files using homedir() to write .spawn-test-* directories to the real home directory instead of the preload sandbox, leaving stray files behind (especially when Bun segfaults on exit and cleanup callbacks don't fire).
  • Add getUserHome() helper to shared/ui.ts that prefers process.env.HOME over homedir(), ensuring the test preload's sandbox is respected.
  • Replace all homedir() and process.env.HOME || homedir() patterns across production and test code with the new helper / process.env.HOME.

Test plan

  • bunx @biomejs/biome check src/ — 0 errors
  • bun test — 1464 pass, 0 fail
  • Verified .spawn-test-* dirs now write inside /tmp/spawn-test-home-*/ sandbox
  • Verified zero stray files in ~/ after test run

🤖 Generated with Claude Code

Bun's os.homedir() reads from getpwuid() and ignores runtime changes to
process.env.HOME. Named imports capture the native function binding, so
patching os.homedir on the default export doesn't propagate. This caused
all test files using homedir() to write .spawn-test-* dirs to the real
home directory instead of the preload sandbox.

Add getUserHome() helper to shared/ui.ts that prefers process.env.HOME,
replace all direct homedir() calls in production and test code.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@louisgv louisgv merged commit 486aba4 into main Mar 10, 2026
5 checks passed
@louisgv louisgv deleted the fix/mock-homedir-in-preload branch March 10, 2026 07:20
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.

2 participants