black icon indicating copy to clipboard operation
black copied to clipboard

.gitignore is ignored by black if more than one source location is specified

Open szandara opened this issue 3 years ago • 3 comments

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

szandara avatar Sep 27 '22 13:09 szandara

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

aaossa avatar Oct 07 '22 20:10 aaossa

Should I submit a PR? @ichard26

aaossa avatar Oct 10 '22 13:10 aaossa

@aaossa Would be greatly appreciated!

felix-hilden avatar Oct 15 '22 08:10 felix-hilden