Skip to content

test: clear inherited GIT_* env once per test file so real-Git suites cannot write into an outer repo - #35859

Merged
nwparker merged 2 commits into
mainfrom
nwparker/vitest-clear-inherited-git-env
Oct 29, 2026
Merged

nwparker merged 2 commits into
mainfrom
nwparker/vitest-clear-inherited-git-env

Conversation

@nwparker

@nwparker nwparker commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 0 0 0 0
Prod 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​16 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​16

ELI5

Many unit tests create a throwaway Git repository and run real git in it, and Orca's own Git code that they exercise runs git too. When the test runner is started from inside a Git hook, Git puts variables like GIT_DIR and GIT_INDEX_FILE in the environment. Every one of those commands then quietly works on the outer repository instead of the throwaway one, so commits, branches and worktrees land in the developer's real repo. This clears those inherited variables once for every test file.

What Changed

  • New setup file config/scripts/vitest-inherited-git-env-setup.ts, registered in UNIT_SETUP_FILES and config/tsconfig.node.json. When each test file loads, it deletes the inherited variables that tell git which repository to work on: GIT_DIR, GIT_WORK_TREE, GIT_INDEX_FILE, GIT_COMMON_DIR, GIT_OBJECT_DIRECTORY, GIT_ALTERNATE_OBJECT_DIRECTORIES, GIT_NAMESPACE, GIT_PREFIX and GIT_QUARANTINE_PATH.
  • GIT_EXEC_PATH and the GIT_CONFIG_* variables are deliberately kept. The Git compatibility CI lane sets GIT_EXEC_PATH to point the bundled git 2.25.5 binary at its helper programs, and that lane uses this vitest config.
  • It follows the same pattern as the existing vitest-caller-identity-env-setup.ts, which clears inherited agent identity.
  • Tests that set GIT_* on purpose are unaffected. The clearing happens before the test file's own module code, beforeAll, beforeEach and test bodies run, so their values are set afterwards and kept.
  • Test infrastructure only. No product code changed.

Why

#35853 fixed this in one suite after it was reproduced: running runtime-failed-local-worktree-removal.test.ts with GIT_DIR pointing at another repo passed, yet wrote the test's commit, branch and worktree into that repo. Other suites that run real Git have the same exposure, so doing it once in setup covers all of them.

Clearing at file load, rather than in a global beforeEach, matters: a beforeEach would also wipe GIT_* values that a suite sets in beforeAll or at module scope.

Linked Issue

N/A (maintainer PR; no existing issue).

Visual Proof

N/A: test infrastructure only.

Testing

  • With GIT_DIR, GIT_WORK_TREE, GIT_INDEX_FILE and GIT_PREFIX set to point at an outer repo, I ran 8 real-Git suites: remove-worktree, worktree-removal-real-git, merge-tree-object-quarantine-real-git, local-pty-provider-spawn-env, runtime-failed-local-worktree-removal, worktree-shared-directories, worktree-safety-real-git and git-binary-compatibility. 113 passed, 39 skipped, and the outer repo stayed empty (0 commits, 0 worktrees). An earlier revision of this PR, which cleared every GIT_* variable, passed 144 tests in 9 suites the same way.
  • config/scripts/ci-unit-selection.test.mjs passes (27). pnpm tc and pnpm run check:code-quality:changed pass. CI runs the full suite.

Review

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Applies to the unit projects (node, bun, node-runtime) that use UNIT_SETUP_FILES. Cross-platform: an environment-variable delete only.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 9 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 10 included reviews currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2e33febb-40a7-4776-8897-9729d023f8f2
Commits

Reviewing files that changed from the base of the PR and between 7f4c8f6 and 9895313.

Files selected for processing: 3
  • config/scripts/ci-unit-files.mjs
  • config/scripts/vitest-inherited-git-env-setup.ts
  • config/tsconfig.node.json
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@greptile-apps

greptile-apps Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 4/5

[Low impact] The change is limited to the shared unit-test setup.

The PR appears safe to merge, with a non-blocking request for a direct regression test.

Findings

  1. P2 Cleanup lacks regression coverage ▶

Summary

Clears nine inherited Git environment variables before unit test files load.

  • Unit tests clear inherited Git repository settings before running.

Reviews (1) · Last reviewed commit: "test: clear only repo-locating Git env s..." · Reviewed by Greptile

Comment on lines +2 to +13
for (const name of [
'GIT_DIR',
'GIT_WORK_TREE',
'GIT_INDEX_FILE',
'GIT_COMMON_DIR',
'GIT_OBJECT_DIRECTORY',
'GIT_ALTERNATE_OBJECT_DIRECTORIES',
'GIT_NAMESPACE',
'GIT_PREFIX',
'GIT_QUARANTINE_PATH'
]) {
delete process.env[name]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Cleanup lacks regression coverage

The new shared cleanup has no direct automated test. Existing Git tests can pass in a clean environment even if this setup stops running, leaving the protection against changing a developer’s repository untested.

Add a test that starts the runner with inherited repository pointers, checks they are gone before the test module loads, and confirms that GIT_EXEC_PATH and values set in beforeAll survive.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@nwparker
nwparker merged commit a50b996 into main Oct 29, 2026
42 checks passed
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