fix(opencode): use extendEnv: false in snapshot git helper to prevent index corruption from hooks#22478
Open
MAnders333 wants to merge 1 commit intoanomalyco:devfrom
Open
Conversation
…t index corruption
74c3666 to
dc416c0
Compare
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.
Issue for this PR
Closes #22477
Type of change
What does this PR do?
When opencode runs inside a pre-commit hook, git sets
GIT_INDEX_FILEin the hook's environment pointing at the working repo's index. The snapshot service's git subprocess helper usedextendEnv: true, which inherited this variable. BecauseGIT_INDEX_FILEtakes higher priority than the index path implied by--git-dir, all snapshot git commands (add,write-tree) wrote into the working repo's index rather than the shadow repo's index. Blobs went to the shadow's object store but index entries ended up in the working repo, leaving it with references to objects that don't exist locally — breaking the commit.The fix switches to
extendEnv: falseand passes onlyPATHandHOMEexplicitly. That's all a local git plumbing operation needs. This mirrors what git itself does viasanitize_repo_env()when crossing repo boundaries. The same fix is applied to thecat-file --batchcall inload()which bypassed the sharedgit()helper.How did you verify your code works?
Reproduced the corruption manually: set up a pre-commit hook that runs
opencode run --agent reviewer, staged specific files, rangit commit, and confirmed the index was corrupted afterward (previously unstaged files staged,error: invalid objecton commit). Traced the root cause through git source (commit.c:run_commit_hooksetsGIT_INDEX_FILE;repository.c:expand_base_dirgives it priority;cross-spawn-spawner.ts:env()merges it into subprocess env viaextendEnv: true). After the fix, the working repo index is unchanged after the opencode session.Checklist