Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
szandara opened this issue Sep 27, 2022 · 3 comments · Fixed by #3336
Closed

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

szandara opened this issue Sep 27, 2022 · 3 comments · Fixed by #3336
Assignees
Labels
C: file collection Related to file collection (e.g. gitignore & cache) or file discovery and all of its configuration. T: bug Something isn't working

Comments

@szandara
Copy link

szandara commented Sep 27, 2022

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 szandara added the T: bug Something isn't working label Sep 27, 2022
@ichard26 ichard26 self-assigned this Sep 29, 2022
@ichard26 ichard26 added the C: file collection Related to file collection (e.g. gitignore & cache) or file discovery and all of its configuration. label Sep 29, 2022
@aaossa
Copy link
Contributor

aaossa commented Oct 7, 2022

The bug seems to be caused by this section of get_sources:

black/src/black/__init__.py

Lines 660 to 670 in b60b85b

elif p.is_dir():
if exclude is None:
exclude = re_compile_maybe_verbose(DEFAULT_EXCLUDES)
gitignore = get_gitignore(root)
p_gitignore = get_gitignore(p)
# No need to use p's gitignore if it is identical to root's gitignore
# (i.e. root and p point to the same directory).
if gitignore != p_gitignore:
gitignore += p_gitignore
else:
gitignore = None

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
Copy link
Contributor

aaossa commented Oct 10, 2022

Should I submit a PR? @ichard26

@felix-hilden
Copy link
Collaborator

@aaossa Would be greatly appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: file collection Related to file collection (e.g. gitignore & cache) or file discovery and all of its configuration. T: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants