python / python/mypy

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

Open Beginner friendly
#21,760 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in mypy/modulefinder.py at matches_gitignore and compare its path handling with the sibling matches_exclude. Run the described --exclude-gitignore reproduction using a nested .gitignore containing sub/ and a real subdirectory. Done when the directory-only pattern excludes sub/ and files beneath it, matching git’s behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli, tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.