Skip to content

Status(): a directory matched by X/** is pruned from the walk, so !X/keep never re-includes (regression in 70ab8844) #2369

Open

Description

@KaizenShogun

Worktree.Status() reports a file as ignored when its directory matched X/** and a later rule re-includes the file. The pattern matcher gets it right on every version I tested, so this is the walk, not gitignore.

Bisected to 70ab8844 ("worktree: skip ignored directories during Status walk", #2048, 2026-05-04). Its parent 89091499 agrees with git; 70ab8844 does not; main today (8372f31) still does not.

Repro

mkdir -p t/volumes/functions && cd t && git init -q
printf 'volumes/functions/**\n!volumes/functions/deno.json*\n' > .gitignore
touch volumes/functions/deno.jsonsample volumes/functions/otro.txt

git status --porcelain --ignored
# ?? .gitignore
# ?? volumes/
# !! volumes/functions/otro.txt      <- only otro.txt is ignored

git check-ignore -v agrees, rule by rule:

volumes/functions/deno.jsonsample  ->  .gitignore:2:!volumes/functions/deno.json*   (NOT ignored)
volumes/functions/otro.txt         ->  .gitignore:1:volumes/functions/**            (ignored)

Through go-git, opening that worktree and reading Status(), deno.jsonsample is reported as not untracked, i.e. ignored:

version deno.jsonsample via Status() otro.txt
git (reference) not ignored ignored
v5.19.2 not ignored ✔ ignored ✔
89091499 (parent of the commit) not ignored ✔ ignored ✔
70ab8844 ignored ✘ ignored ✔
main 8372f31 ignored ✘ ignored ✔

gitignore.NewMatcher(ReadPatterns(...)).Match([]string{"volumes","functions","deno.jsonsample"}, false) returns false — correct — on all four, including main. Only the Status() walk disagrees, which is why the pattern tests don't catch it.

Why it happens

The walk prunes a directory when the matcher calls it ignored, and for volumes/functions the matcher says true — because ** happily matches the empty remainder. git check-ignore volumes/functions/ says the same thing (.gitignore:1:volumes/functions/**). But git's own walk does not treat that as a reason to stop descending: X/** excludes the contents of X, and a later !X/keep can still re-include one of them. Asking Match(dir, true) and pruning on the answer conflates "this directory's contents are excluded" with "nothing inside can be re-included".

The commit also fixed the mirror case, so please don't just revert it

Same shape, but with the directory itself excluded (X/ instead of X/**) — here git really does refuse to descend, and the re-inclusion is dead:

printf 'volumes/functions/\n!volumes/functions/deno.json*\n' > .gitignore
git status --porcelain --ignored
# !! volumes/          <- everything below is ignored, the negation cannot reach in
version deno.jsonsample via Status()
git (reference) ignored
v5.19.2 not ignored ✘
89091499 not ignored ✘
70ab8844, main ignored ✔

So 70ab8844 traded one wrong family for another: it made X/ correct and X/** wrong. What separates them isn't whether the matcher says the directory is ignored — it says yes to both — but whether the pattern excluded the directory itself or only its contents.

How much it costs

I run a conformance bench that replays 33 real repositories' whole .gitignore trees against git check-ignore as the oracle: 4,463 matcher queries and 2,224 through Status(). main diverges on 1 of 2,224 through Status(), and this is it — the case above is supabase/supabase's docker/.gitignore, verbatim. One query sounds like a corner, so here's the wider count from the same corpus: 5 of the 33 repositories write "ignore everything, keep this" with a negation reaching inside — elasticsearch, grafana, node, next.js, supabase — 14 rule lines between them. My corpus only materialises paths that hit the exact combination once; the construct itself is the one the gitignore docs recommend for the job.

I chased it past the matcher because Status() is the door a consumer actually reaches for: someone using go-git to find out what's untracked never calls ReadPatterns themselves, so a matcher that's right and a walk that isn't still hands them a wrong answer.

Bench, corpus and the go-git adapter, if you want to reproduce any row: https://github.com/KaizenShogun/gitignore-conformance

Activity

  1. KaizenShogun commented on Sep 15, 2026

    @KaizenShogun
    Author

    A note for whoever writes the fix, because two independent attempts have now landed in the same trap.

    #2386 (Go, here) and treeverse/dvc#11096 (Python, different author, different project, for my report of the same bug there) both make Status()/the matcher stop pruning at a re-included directory — and both break the same four paths in my corpus, fixing one. The shape is one line of .gitignore away:

    $ printf 'f/**\n!f/deno.json*\n' > .gitignore   # in a `git init`ed tree
    $ mkdir -p f/deno.jsonsample && touch f/deno.jsonsample/keep
    $ git check-ignore -q f/deno.jsonsample/keep && echo ignored
    ignored
    

    !f/deno.json* re-includes the directory, so git descends — and then f/** ignores what it finds inside. Re-including a directory is permission to look, not a verdict about its contents; both patches turn it into the verdict. The walk has to resume rule evaluation below the re-included directory, which is the part neither patch does.

    Measured against gitignore-conformance: 8,953 queries for Match, 6,714 for Status(), git check-ignore 2.55.0 as the oracle.

  2. KaizenShogun commented on Sep 22, 2026

    @KaizenShogun
    Author

    The cause is in pattern.go, one layer below both doors — so fixing this in worktree/status, where #2386 goes, leaves anyone who calls gitignore directly with the same wrong answer.

    I measured that instead of asserting it. One line, pattern.go:511:

    - matched = len(path) > 0 || isDir
    + matched = len(path) > 0

    main (8372f31) against main plus that line, through all three doors the library exposes. Corpus is 33 repositories' real .gitignore trees replayed against git check-ignore 2.55.0. Sets, not counts — a 5 → 8 could be one fix and four breakages, and here it is:

    door queries main patched fixes breaks
    gitignore.Matcher.Match 8,953 12 12 0 0 — the same 12
    gitignore.Scope 1,010 directory queries 1 0 1 0
    Worktree.Status() 6,714 5 8 1 4

    The Scope row is the one that locates it. Scope is already the right door — it is go-git's may_prune_directory, and worktree_status.go uses it — and it gets 1,009 of 1,010 right. Its single divergence is docker/volumes/functions/ from supabase/supabase, the directory in this report, and it goes away with a change in globMatch. The prune decision is being answered two layers down.

    I am not asking you to apply that line

    Look at the last column. It fixes the file in this report and breaks four paths, all in the same tree: volumes/functions/deno.jsonsample/keep and volumes/functions/main/index.ts/keep. Once the walk stops pruning it descends, and underneath sits the other half — !volumes/functions/deno.json* re-includes a directory, and go-git then treats that as a verdict about everything below it instead of resuming rule evaluation there. That is #2112, and it is the family I flagged on this thread on 15 Sep, when #2386 and treeverse/dvc#11096 each fixed one row and broke four of exactly this shape — the repro in that comment is one of the four here. Three independent attempts now, two languages, three different places in the code, the same trade. The missing mechanism is "a re-included directory is permission to look, not a verdict about its contents", and no one-line change anywhere gets there. #2112 has to move first, or at the same time.

    Your own tests disagree with git, and the test oracle is why

    go test ./plumbing/format/gitignore/ is green on main and fails on the patched tree in four rows: TestIgnoreDoubleStarSuffixRequiresSegment (node_modules and src/node_modules as directories) and TestWildmatchSlashSemantics (**/bar/** vs deep/foo/bar/, a/**/*/** vs a/b/). All four expect excluded. git does not say that:

    rule directory check-ignore x check-ignore x/
    **/node_modules/** node_modules no yes
    **/node_modules/** src/node_modules no yes
    a/** a no yes
    a/**/*/** a/b no yes
    **/bar/** deep/foo/bar no yes
    a/ (control) a yes yes

    The suite ships its own oracle and still passes, because gitOracle in conformance_test.go does arg += "/" before calling check-ignore -q. A trailing ** matches the empty remainder the slash leaves behind, so with the slash git says "excluded" for every one of these and the test agrees with a question nobody asked. Strip the slash and the same helper reproduces the table above. One line of the test helper, and four expectations stop being green by accident.

    For the record on 3189ee9d (#2279), which wrote line 511: I am not asking for a revert. It fixed the mirror case — **/node_modules/** marking every directory excluded and Status() losing everything below the root — and my corpus confirms that is still fixed; main and the patched tree diverge on the same 12 matcher rows. || isDir is holding up the majority case on its own, which is precisely why it cannot be the prune decision.

    Corpus, adapters and the exact rows, if you want to reproduce any of this: https://github.com/KaizenShogun/gitignore-conformance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions