Repository navigation
test(check-pr-size): pin initTestRepo's initial branch so go test passes under init.defaultBranch=main - #240
Merged
Conversation
…ses under init.defaultBranch=main initTestRepo ran `git init -q` with no `-b`, so the fixture repo's first branch was whatever the developer's `init.defaultBranch` happened to be. TestResolveMergeBaseWithAdvancedBaseBranch then runs `git checkout -q -b main <forkPoint>`, which fails with `fatal: a branch named 'main' already exists` on any machine configured with `init.defaultBranch = main`. CI is green only because the ubuntu-latest runner's git still defaults to `master`. Pin the fixture's initial branch to a sentinel name so it can never collide with a branch the tests create themselves. `-b` needs git >= 2.28, the same release that introduced `init.defaultBranch`, so no supported git regresses.
Contributor
|
Warning Review limit reachedNext included review available in 36 minutes. View limit detailsLimit details: You’ve used the included review currently available. Your 129 included PR review attempts over the past 7 days set your current allowance at 1 review per hour. Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. Review configuration: ⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
Comment |
mattmillerai
marked this pull request as ready for review
August 27, 2026 14:41
There was a problem hiding this comment.
🔍 Cursor Review — Consolidated panel
Triggered by @mattmillerai.
Found 3 finding(s).
| Severity | Count |
|---|---|
| 🟢 Low | 2 |
| ⚪ Nit | 1 |
Panel: 8/8 reviewers contributed findings.
…p `init -b` (BE-9614) Addresses the cursor-review panel findings on #240: - `init -b` needs git >= 2.28, and gitRun escalates any non-zero exit to t.Fatalf, so on older git every repo-backed test died inside initTestRepo before reaching its "git too old for check-attr --source" skip. Pin the branch with `git -c init.defaultBranch=...` instead, which pre-2.28 git ignores harmlessly (it does not honor the key, so it cannot collide). - Pinning the branch closed one collision with the developer's global config but left the class open. Neutralize it wholesale with GIT_CONFIG_GLOBAL / GIT_CONFIG_SYSTEM = os.DevNull, the idiom already used by .github/groom/tests/test_scope.py and groom.yml. Set via t.Setenv, not gitRun's cmd.Env, because the production helpers under test (diffFiles, checkAttrSourceSupported) spawn their own git from the test process. Verified: under a global `diff.renames=false`, TestClassifyRenameIntoAttrGeneratedTreeStillCounts and TestAnnotateDiscountsPairsRenamesSoTheEditStillCounts failed with "rename detection did not fire" before this change and pass after. - Rename the fixture branch groom-test-base -> pr-size-test-base; `groom` is an unrelated subsystem under .github/groom/.
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.
ELI-5
The Go tests for the PR-size checker build a throwaway git repo to test against. They never told git what to call that repo's first branch, so git used whatever the developer's
init.defaultBranchsetting says. One test then tries to create its own branch calledmain— and on any machine whereinit.defaultBranch = main(a very common setting), that blows up withfatal: a branch named 'main' already exists. This PR gives the throwaway repo an explicit, deliberately-boring starting branch name so nothing the tests create can ever collide with it — and, after review, seals off the rest of that same class by ignoring the developer's global git config for the duration of these tests.What changed
initTestRepoinscripts/check-pr-size/main_test.go, in two ways. No production code, no CI config, and no other test was touched.1. The initial branch is pinned —
git -c init.defaultBranch=pr-size-test-base init -qinstead of a baregit init -q.The first revision of this PR used
git init -q -b groom-test-base; review (thread) caught that-bneeds git >= 2.28 whilegitRunescalates any non-zero exit tot.Fatalf— so on older git every repo-backed test in the file would die insideinitTestReporather than reach itst.Skip("git too old for check-attr --source")guard. Those guards exist, so pre-2.28 git is explicitly anticipated here. The-cform (already used by.github/bump-callers/tests/test_preflight.sh) pins the branch identically and is ignored harmlessly by pre-2.28 git, which does not honorinit.defaultBranchand therefore cannot collide in the first place. The name also movedgroom-test-base->pr-size-test-base, sincegroomis an unrelated subsystem under.github/groom/.2. The developer's global/system git config is neutralized for these tests —
t.Setenv("GIT_CONFIG_GLOBAL", os.DevNull)+ the same forGIT_CONFIG_SYSTEM, the idiom already used by.github/groom/tests/test_scope.pyandgroom.yml.Pinning the branch fixed one setting; review (thread) pointed out it left the whole class open, and that turned out to be live rather than theoretical — see Verification. It is
t.Setenvrather thangitRun'scmd.Envbecause the failing assertions come fromdiffFiles, production code the test runs in-process aftert.Chdir, spawning its own git from the test process; acmd.Envon the fixture helper would not have covered them. Safe because everyinitTestRepocall site is serial — the same constraintt.Chdiralready imposes.Why CI never caught it
test-pr-size.ymlruns onubuntu-latest, whose git still defaults tomaster, socheckout -b mainfinds no existing branch and the suite is green. The bug only surfaces locally, and only for developers whose git is configured tomain— which is precisely the audience for the local Go gate command documented inAGENTS.md(line 33:(cd scripts/check-pr-size && [ -z "$(gofmt -l .)" ] && go vet ./... && go test ./...)), added by the docs-only #227.Verification
Reproduced the failure on
origin/main(75685ed) before changing anything:Then ran the full matrix on the fix (
-count=1throughout — Go's test cache does not key on env that only the git subprocess reads, so a plain rerun can return a stale cached result). Local git 2.54.0, Go 1.26.5:init.defaultBranchmain(GIT_CONFIG_COUNT=1 …)ok— 258 tests/subtests passed, 0 failedmaster(GIT_CONFIG_COUNT=1 …)okGIT_CONFIG_COUNT=0)okgofmt -l .clean,go vet ./...clean. All three env variants usedGIT_CONFIG_*rather than writing global git config.Review round 2 — the global-config class was not theoretical. Under a global
diff.renames=false, the pre-fix branch (165efcc) fails:Both pass after the
GIT_CONFIG_GLOBAL/GIT_CONFIG_SYSTEMchange, and the suite isokwith no hostile config as well. Separately confirmedgit -c init.defaultBranch=pr-size-test-base init -qstill yieldspr-size-test-baseon git 2.54.0, and re-checked that noinitTestRepocaller sits under at.Parallel()(which would maket.Setenvpanic) — all 15 are serial.The repo gate itself is green on this PR:
test-pr-size.yml(go test) passed in 25s — run 33083662393. As expected it proves nothing about the bug (its runner's git still defaults tomaster); it confirms only that the rename broke nothing. See Residual.Blast-radius check on the rest of the suite
initTestRepohas 16 call sites inmain_test.goand 0 insize_test.go. Grepping the wholecheck-pr-sizepackage for branch-name literals (master,"main",refs/heads,symbolic-ref,branch --show-current,rev-parse --abbrev-ref) returns exactly one hit — line 70, thecheckout -b mainthe tests create explicitly. Nothing reads the initial branch by name, so renaming it is behaviour-neutral for the other 15 call sites, which is what the green full-suite runs above confirm.The half this PR does not fix
Swept the whole repo for the same unpinned pattern: 13
git initsites across 9 files. This PR pins 1. The other 12 —.github/workflows/{pr-risk,pr-derisk,pr-area-label,linear-ticket}.yml(7, all the ancestry-check scratch repos),.github/public-repo-hygiene/tests/test_check_public_repo_hygiene.py(2),scripts/pr-risk/tests/test_pin_contract.sh,.github/groom/tests/test_scope.py,.github/groom/tests/sandbox-tests.sh— were each checked for a follow-oncheckout -b/switch -c/branch -mand for hardcodedmain/masterliterals near the init. None of them creates a named branch aftergit init, so none can collide the way this one does, and none is fixed here.Judgment calls
pr-size-test-base. The first revision kept the work item's literalgroom-test-baseon the reasoning that deviating for cosmetics would be unrequested drift; a reviewer did prefer the rename, so it moved. Either string is an arbitrary sentinel whose only job is to be nothing a developer would set asinit.defaultBranch.Residual
Not fixed here, and worth its own follow-up:
test-pr-size.ymlruns onego test ./...onubuntu-latest, where git defaults tomaster; if a future edit drops the-bagain, the job stays green and the breakage reappears only on developers' machines. The durable fix is a secondgo teststep (or matrix leg) run underGIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=init.defaultBranch GIT_CONFIG_VALUE_0=main, which costs ~2s. It is deliberately not in this PR because the work item scoped this to one file and said not to touchtest-pr-size.yml.git initsites remain unpinned. They are safe today only because none of them happens to create a named branch afterwards (swept and enumerated above) — that is an incidental property, not an enforced one, and the next test added to any of those suites can reintroduce the same collision. A blanket-bpin across all 13 sites, or a lint that requires it, would make the property enforced.mainand verified this fix under all threeinit.defaultBranchsettings. That tracker is not reachable from where this PR was produced, so its transcript was never read; the reproduction and the three-way verification above were re-done from scratch here rather than taken on trust, but the investigation's own artifacts remain unexercised.Provenance
test-pr-size.yml(go test) green on this PR (21s, run 33087171862); all 4 PR checks pass. Reproduced the original failure on origin/main@75685ed; on this branchgofmt -l .clean,go vet ./...clean,go test -count=1 ./...okunderinit.defaultBranch= main / master / unset (258 tests+subtests each). For the review-round change, reproduced thediff.renames=falsefailure on 165efcc and confirmed it green on 934415d.git -c init.defaultBranch=instead ofinit -b(old-git compatibility), the sentinel branch was renamedgroom-test-base->pr-size-test-base, andGIT_CONFIG_GLOBAL/GIT_CONFIG_SYSTEMisolation was added. All three are inside the sameinitTestRepohelper in the same file; still no CI step added and no other test touched.