fix(test): restore env per-test, not once at the end - #2481
Open
tranthanhnhatkhoa wants to merge 1 commit into
Open
fix(test): restore env per-test, not once at the end#2481tranthanhnhatkhoa wants to merge 1 commit into
tranthanhnhatkhoa wants to merge 1 commit into
Conversation
Two describe blocks in test/gstack-memory-helpers.test.ts save env in beforeEach and restore it in afterAll. beforeEach re-captures on every test, so after the first one `savedRealHome` no longer holds the real HOME — it holds the previous test's temp dir. The afterAll then "restores" HOME to that stale fixture. The fixture contains a .gbrain/config.json with engine=postgres, so the next FILE in the run reads it as if it were the user's real config. test/gbrain-detect-install.test.ts expects pglite and fails with: Expected: "pglite" Received: "postgres" It passes in isolation (16/16) and fails in the suite, which is what made this look like flakiness rather than a leak. Pairing the hooks fixes it: beforeEach saves, afterEach restores, so each capture is of a genuinely un-mutated value. Verified: polluter alone 29 pass 0 fail (unchanged) polluter + victim 45 pass 0 fail (was 42 pass, 3 fail) full suite 17 fails -> 12 (3 gbrain-detect + 2 gbrain-sync gone)
|
Merging to
After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here |
This was referenced Aug 8, 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.
The bug
Two
describeblocks intest/gstack-memory-helpers.test.tssave env inbeforeEachand restore it inafterAll:beforeEachre-captures on every test, so after the first onesavedRealHomeno longer holds the realHOME— it holds the previous test's temp dir. TheafterAllthen "restores"HOMEto that stale fixture.That fixture contains a
.gbrain/config.jsonwithengine=postgres, so the next file in the run reads it as if it were the user's real config:That file passes in isolation (16/16) and fails in the suite — which is what makes this read as flakiness rather than a leak. It also takes out
gbrain-sync's two split-engine cases for the same reason.The fix
Pair the hooks.
beforeEachsaves,afterEachrestores, so every capture is of a genuinely un-mutated value. No change to what the tests assert.Verified
Environment: bun 1.3.14, Ubuntu 24.04.4 LTS,
main@960c3a8d(v1.60.2.0).Related
Independent of #2480, which hardens
gstack-config.test.tsagainst a different leak (module-scopeGSTACK_HOMEassignments in five suites). That one makes a victim immune; this one removes a polluter. Both are needed — they're separate bugs with the same symptom class, and the suite has enough cross-file env mutation that it's worth a broader audit than either PR does.Worth noting the two are hard to spot because
bunshares one process across files and the suite's exit code is unreliable (#2435), so a leak like this shows up as "those tests are flaky" rather than as a failure anyone chases.🤖 Generated with Claude Code
https://claude.ai/code/session_01H3KRWmyJnSvzzxeooDxuU3