.gitignore is ignored by black if more than one source location is specified
Describe the bug
When adding more than one source entry, black ignores the .gitignore entries.
To Reproduce
content of .gitignore
generated/*
Running
black test src
produces
$ black --check tests src
would reformat generated/jtd/__init__.py
Oh no! 💥 💔 💥
1 file would be reformatted, 29 files would be left unchanged.
Running
black src
produces
$ black --check src
All done! ✨ 🍰 ✨
Expected behavior
$ black --check test src
All done! ✨ 🍰 ✨
Environment
- Black's version: 22.8.0
- OS and Python version: MacOS m1
Additional context
works with Black version: 22.6.0
The bug seems to be caused by this section of get_sources:
https://github.com/psf/black/blob/b60b85b234d6a575f636d0a125478115f993c90c/src/black/init.py#L660-L670
In the first run (the first SRC) exclude is None, but then the value gets overridden by re_compile_maybe_verbose(DEFAULT_EXCLUDES), so in the second iteration the condition is not met and skips to gitignore = None.
This can be fixed by creating a flag before the for loop, something like is_default_exclude = exclude is None, and check if the value is false afterwards:
if is_default_exclude:
exclude = re_compile_maybe_verbose(DEFAULT_EXCLUDES)
...
Should I submit a PR? @ichard26
@aaossa Would be greatly appreciated!