You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] anchored gitignore pattern (/dir/) matches basename anywhere in tree, not just at root #1087
The gitignore spec says a leading / anchors a pattern to the directory containing the .gitignore file. For example, /inbox/ in the repo root .gitignore should only match inbox/ at the repo root — but graphify's _is_ignored() matches it against any directory named inbox anywhere in the tree.
This causes detect() to silently skip source directories whose names happen to collide with runtime data directories listed in .gitignore.
Reproduction
Have .gitignore contain /inbox/ (ignores inbox/ runtime data dir at repo root)
Have source code in src/inbox/ (unrelated source module)
Run detect() — src/inbox/*.rs files are missing from results
fromgraphify.detectimportdetectfrompathlibimportPathdetected=detect(Path('.'))
# src/inbox/*.rs files NOT in detected['files']['code']
Root Cause
graphify/detect.py L678-679 in _matches():
iffnmatch.fnmatch(target.name, p):
returnTrue
This basename-only check means /inbox/ matches src/inbox because target.name == "inbox" and p == "inbox" — even though src/inbox is not at the repo root.
Suggested Fix
For anchored patterns (those with leading /), the basename shortcut should be restricted: either omit it entirely for anchored patterns, or only apply it when target is directly under anchor:
Fixed in cca13aa. Good catch — the bug was actually two leaks, not one.
Root cause:_matches() inside _is_ignored() (and its twin in _is_included()) had no awareness of the anchored flag. By the time it ran, the leading / had already been stripped from the pattern (p = raw.strip("/")), so /inbox/ and inbox/ looked identical to it. Two shortcuts then fired unconditionally:
fnmatch.fnmatch(target.name, p) — matched src/inbox because target.name == "inbox"
Per-segment loop — would also match inbox at position i=1 in ["src", "inbox"]
Your proposed fix addressed leak 1. Leak 2 (per-segment loop) was also present and would have kept the bug alive.
Fix: added anchored: bool parameter to both _matches() functions. For anchored patterns, the function now does only fnmatch.fnmatch(rel, p) — an exact anchor-relative path match. The basename/segment shortcuts are gated on not anchored. The outer ancestor-walk loop already handles directory semantics (inbox/ matching descendants), so the anchored case doesn't need them.
Same fix applied to _is_included()'s _matches() which had the identical bug.
5 regression tests added covering: anchored dir not matched at depth, anchored dir matched at root (positive), anchored file not matched at depth, unanchored dir still matches anywhere, anchored multi-segment pattern.
Activity
safishamsi commentedon May 30, 2026
Fixed in cca13aa. Good catch — the bug was actually two leaks, not one.
Root cause:
_matches()inside_is_ignored()(and its twin in_is_included()) had no awareness of theanchoredflag. By the time it ran, the leading/had already been stripped from the pattern (p = raw.strip("/")), so/inbox/andinbox/looked identical to it. Two shortcuts then fired unconditionally:fnmatch.fnmatch(target.name, p)— matchedsrc/inboxbecausetarget.name == "inbox"inboxat position i=1 in["src", "inbox"]Your proposed fix addressed leak 1. Leak 2 (per-segment loop) was also present and would have kept the bug alive.
Fix: added
anchored: boolparameter to both_matches()functions. For anchored patterns, the function now does onlyfnmatch.fnmatch(rel, p)— an exact anchor-relative path match. The basename/segment shortcuts are gated onnot anchored. The outer ancestor-walk loop already handles directory semantics (inbox/matching descendants), so the anchored case doesn't need them.Same fix applied to
_is_included()'s_matches()which had the identical bug.5 regression tests added covering: anchored dir not matched at depth, anchored dir matched at root (positive), anchored file not matched at depth, unanchored dir still matches anywhere, anchored multi-segment pattern.
fix anchored gitignore pattern leaking basename match into subtree (c…
fix anchored gitignore pattern leaking basename match into subtree (c…