Preserve matched branches and versions for type:file branch queries
perfloop-oss/zoekt · Uncatalogued correctness
https://perfloop.ai/t/oss/case_em0q2t2d50
Verdict
VERIFIED · settled 2026-11-24 · pull request opened as sourcegraph/zoekt#1297
What happened: The check fails before this change and passes with it.
Hypothesis
When a search uses filename-only mode (such as type:file or type:filename) together with a branch filter, the search returns the wrong branches and the wrong commit version for every matching file that exists on multiple branches. Instead of reporting only the branches selected by the query and the commit version of a selected branch, the search falls back to every branch that contains the file and picks the version of the repository's first matching branch (typically the default branch). This silently misattributes file results and commit links on multi-branch repositories; production query prevalence is unmeasured.
In query.Parse, a query such as "type:file branch:dev hello" lifts the type:file modifier to wrap the entire query in query.Type{Type: query.TypeFileName, Child: ...}. In index/matchtree.go, indexData.newMatchTree translates TypeFileName into a fileNameMatchTree wrapping the compiled child tree, which includes any branchQueryMatchTree built from query.Branch or query.BranchesRepos. After a document matches in indexData.Search (index/eval.go:302), indexData.matchingBranchMask (index/eval.go:498-513) calls visitMatchAtoms to collect the branch masks of all matched branchQueryMatchTree atoms, falling back to d.fileBranchMasks[docID] when no branch atom is visited.
However, visitMatchAtoms (index/matchtree.go:656-660) delegates directly to visitMatches (index/matchtree.go:664-692), and visitMatches deliberately stops at case *fileNameMatchTree (lines 687-688) so that indexData.gatherMatches (index/eval.go:399) does not collect content match fragments inside TypeFileName. Because visitMatchAtoms reuses that skip, matchingBranchMask never visits any branchQueryMatchTree inside a fileNameMatchTree, gets a zero mask, and falls back to d.fileBranchMasks[docID]. Consequently, gatherBranches populates FileMatch.Branches with all branches containing the document, and branchIndex(branchMask) sets FileMatch.Version (for both main repositories and subrepositories in index/eval.go:312-319) to the version of the lowest-indexed branch containing the file instead of the matched branch. Note that indexData.scoreFile (index/score.go:339) also calls visitMatchAtoms to count matched atoms, so the Case should verify whether descending into fileNameMatchTree in visitMatchAtoms preserves expected scoring behavior or whether atom counting should stay scoped separately.
Checked upstream open pull requests touching index/matchtree.go (#1002, #1168, #1286): none modify visitMatchAtoms, visitMatches, or fileNameMatchTree handling.
Property checked: for any query containing branch filters (query.Branch or query.BranchesRepos, combined with And/Or/Not/Boost or parsed via query.Parse("type:file ...")) that matches a document, wrapping the query or a conjunct in query.Type{Type: query.TypeFileName} must produce the same FileMatch.Branches and FileMatch.Version as the unwrapped query. Running a 29-query property test across 7 branch-filter shapes and 4 TypeFileName wrapper placements plus the parsed query (go test -v ./index -run TestPropTypeFileNamePreservesMatchingBranches) produced 41 counterexamples, including parsed query "(type:filename (and branch:\"dev\" substr:\"hello\"))" returning Branches=[main dev release] Version="sha-main" instead of Branches=[dev] Version="sha-dev". An experimental edit making visitMatchAtoms descend into fileNameMatchTree passed the reduced replay alongside TestSearchTypeFileName, TestGatherBranches, and TestScore tests (go test ./index -run "TestTypeFileNamePreservesMatchingBranches|TestSearchTypeFileName|TestGatherBranches|TestScore"). The attached check is a provisional deterministic replay; Case verification should also check multi-branch subrepositories and full-package tests.
Change to test: Traverse into fileNameMatchTree children in visitMatchAtoms while keeping visitMatches skipping fileNameMatchTree for match-fragment gathering, so branch filters inside type:file queries are preserved when computing FileMatch.Branches and FileMatch.Version without collecting content fragments.
Where it lives
perfloop-oss/zoekt · index/eval.go
Evidence
The check fails before this change and passes with it: `Filename-only wrapping preserves matched branches and the version of the first reported branch for the tested Branch and BranchesRepos queries, including And, Or, Not, Boost, parsed type:file and type:filename queries, and main and subrepositories.`
Checks: 3 of 3 passed. Verification: no defect found.
Timeline
2026-11-24· Case opened2026-11-24· PR opened