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

未關閉 適合新手
#21,760 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
78/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
冷清
技術堆疊
python
領域
cli, tooling

研究方向

從 mypy/modulefinder.py 中的 matches_gitignore 開始,將其路徑處理與同層的 matches_exclude 進行比較。使用包含 sub/ 和一個實際子目錄的巢狀 .gitignore,執行所描述的 --exclude-gitignore 重現。完成標準是:僅匹配目錄的模式能夠排除 sub/ 及其下的檔案,並符合 git 的行為。

由索引模型根據 Issue 內容生成。

描述

bug
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.

主要語言
Python
星號
20.6k
分支
3.3k
平均合併
1 天 9 小時
30 天內合併 PR
56

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

python/mypy 的其他 Issue

查看 python/mypy 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。