python / python/mypy

matches_gitignore: directory-only patterns in nested .gitignore not excluded under --exclude-gitignore

Offen Anfängerfreundlich
#21,760 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug

matches_gitignore (mypy/modulefinder.py) fails to exclude a directory matched by a directory-only pattern (e.g. build/, node_modules/) when the pattern lives in a nested .gitignore (one in a scanned subdirectory), under --exclude-gitignore.

The trailing-slash decision uses the gitignore-relative path with a cwd-relative stat:

relative_path = os.path.relpath(subpath, gi_path)
if fscache.isdir(relative_path):   # relative_path is relative to gi_path, but isdir resolves from cwd
    relative_path = relative_path + "/"

When gi_path != cwd, relative_path doesn't resolve on disk, so isdir returns False, the / is never appended, and the directory-only pattern doesn't match — mypy then type-checks files the user intended to exclude.

The sibling matches_exclude in the same file does it correctly: it builds the match string with os.path.relpath(subpath) but calls fscache.isdir(subpath) on the original path.

To Reproduce

With --exclude-gitignore, a package dir containing a nested .gitignore whose content is sub/, where sub/ is a real subdirectory: mypy still checks files under sub/.

Expected Behavior

sub/ is excluded, matching git's own behavior for directory-only patterns.

Fix

Stat the original subpath (keep relative_path only for the spec match) — proposed in #21752.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne in mypy/modulefinder.py bei matches_gitignore und vergleiche die Pfadbehandlung mit dem gleichgeordneten matches_exclude. Führe die beschriebene --exclude-gitignore-Reproduktion mit einer verschachtelten .gitignore aus, die sub/ und ein echtes Unterverzeichnis enthält. Fertig ist die Änderung, wenn das Verzeichnismuster sub/ und die darin enthaltenen Dateien ausschließt und damit dem Verhalten von git entspricht.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
cli, tooling
Issue-Typ
Bug
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Ruhig
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
78/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.