Skip to content

[BUG] anchored gitignore pattern (/dir/) matches basename anywhere in tree, not just at root #1087

Closed

Description

@cheerc

Context

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

  1. Have .gitignore contain /inbox/ (ignores inbox/ runtime data dir at repo root)
  2. Have source code in src/inbox/ (unrelated source module)
  3. Run detect() — src/inbox/*.rs files are missing from results
from graphify.detect import detect
from pathlib import Path

detected = detect(Path('.'))
# src/inbox/*.rs files NOT in detected['files']['code']

Root Cause

graphify/detect.py L678-679 in _matches():

if fnmatch.fnmatch(target.name, p):
    return True

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:

if not anchored and fnmatch.fnmatch(target.name, p):
    return True

Or, more conservatively, gate it on rel_anchor containing no / (i.e., the target is directly under the anchor directory).

Impact

Silently dropping source files from the graph. Users don't get errors — they just get incomplete graphs with no indication anything is wrong.

Environment

  • graphify version: latest (HEAD as of 2026-05-30)
  • agend-terminal repo (the repo where this was discovered)

Activity

  1. safishamsi commented on May 30, 2026

    @safishamsi
    Member

    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:

    1. fnmatch.fnmatch(target.name, p) — matched src/inbox because target.name == "inbox"
    2. 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.

  2. added a commit that references this issue on May 30, 2026
    cca13aa
  3. added a commit that references this issue on Aug 13, 2026
    4efbfa5
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