ruff: exclude pattern with `*` in file name incorrectly matches entire file path
ruff --exclude foo*.py
this incorrectly excludes the file foo/bar/baz.py.
since excludes takes globs (as mentioned here), i would expect it to only match python files with names starting with foo.
i’m on windows (which uses \ as path separators instead of /), that may be related
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 6
- Comments: 15 (11 by maintainers)
We have kind of a weird thing going on, which I think I followed from Flake8 or Black. When given a pattern like
foo*.py, we match against both the basename and the absolute path (when testing to exclude a given file or directory).So it may make sense to turn on
literal_separator, since we’d still match*.pyon the cases described above. Or we may want to reconsider our behavior here more holistically.Came here because I was having an issue with the globs unexpectedly including subdirectories.
In my case, I am trying to exclude
tests/*.py(which contains an old codebase which will be deprecated) while includingtests/v3/*.py. Following the documentation:I added:
I was surprised to find out that this excluded all Python files in
tests/and subdirectories thereof. As pointed out above, it brings into question the purpose of having both*and**. Being a glob pattern, Ruff’s behaviour also doesn’t match a standard shell glob (e.g.,ls tests/*.pydoes not list files intests/v3/)On mobile so apologies for short reply.
Globbing is pretty tricky because it depends on the context in which it is run. For example, at a shell, you might expect
*.pyto only match Python files in the current directory, but in a.gitignorefile you would expect (and is what happens)*.pyto exclude bothfoo.pyandbar/foo.py. In the gitignore world, you would restrict your glob to the current directory (relative to the.gitignorefile) by prefixing the glob with a/. So,/*.pywould matchfoo.pybut notbar/foo.py.So whether
*.pyis supposed to match a path separator or not depends a lot on the context in which a glob is applied. But generally speaking, for exclude rules I would try to follow what gitignore does. (With that said, I haven’t looked at in detail the context here, so take what I’m saying with a grain of salt.)Our plan for red knot is to have either an
includeandexclude(or both?) that support git patterns. Whether we bring this behavior to ruff depends on how it proves in practice and the timeline for using Red Knot’s infrastructure for Ruff.I think @konstin and @MichaReiser have also been thinking about this recently
For visibility, I think my comment to my (closed) PR addressing this is quite relevant here:
Implementing the
.gitignorelogic as to the meaning of a leading forward slash,/, would be good as well to distinguish/*.pyand*.py.Is it currently possible to exclude python files in the root directory without excluding python files in every subdirectory?
The current approach seems different from both gitignore and standard globs, instead a third thing:
…in no case would we expect
/*.pyto match all python files — which IIUC is what ruff currently does?On Android, the long press variations for
'do not include`, but you can instead do:?123->=\<->`Thanks! I’m impressed that you can manage code formatting on mobile.