fix: sandbox test homedir to prevent stray files in ~#2417
Merged
Conversation
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>
7e059e1 to
2a5e13c
Compare
4 tasks
louisgv
approved these changes
Mar 10, 2026
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.
Summary
os.homedir()ignoresprocess.env.HOME— it reads fromgetpwuid()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.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).getUserHome()helper toshared/ui.tsthat prefersprocess.env.HOMEoverhomedir(), ensuring the test preload's sandbox is respected.homedir()andprocess.env.HOME || homedir()patterns across production and test code with the new helper /process.env.HOME.Test plan
bunx @biomejs/biome check src/— 0 errorsbun test— 1464 pass, 0 fail.spawn-test-*dirs now write inside/tmp/spawn-test-home-*/sandbox~/after test run🤖 Generated with Claude Code