Skip to content

fix(test): restore env per-test, not once at the end - #2481

Open
tranthanhnhatkhoa wants to merge 1 commit into
garrytan:mainfrom
tranthanhnhatkhoa:fix/memory-helpers-env-restore-per-test
Open

fix(test): restore env per-test, not once at the end#2481
tranthanhnhatkhoa wants to merge 1 commit into
garrytan:mainfrom
tranthanhnhatkhoa:fix/memory-helpers-env-restore-per-test

Conversation

@tranthanhnhatkhoa

Copy link
Copy Markdown

The bug

Two describe blocks in test/gstack-memory-helpers.test.ts save env in beforeEach and restore it in afterAll:

beforeEach(() => {
  savedRealHome = process.env.HOME;   // re-captured every test
  process.env.HOME = testHome;
});

afterAll(() => {
  else process.env.HOME = savedRealHome;   // runs once, at the end
});

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.

That 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:107
  expect(j.gbrain_engine).toBe('pglite')
  Expected: "pglite"   Received: "postgres"

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. beforeEach saves, afterEach restores, so every capture is of a genuinely un-mutated value. No change to what the tests assert.

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)

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.ts against a different leak (module-scope GSTACK_HOME assignments 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 bun shares 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

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)
@trunk-io

trunk-io Bot commented Aug 8, 2026

Copy link
Copy Markdown

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

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

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