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

Configuration example "exclude" doesn't work as documented #1473

Closed
orn688 opened this issue Jun 1, 2020 · 3 comments
Closed

Configuration example "exclude" doesn't work as documented #1473

orn688 opened this issue Jun 1, 2020 · 3 comments
Labels
C: configuration CLI and configuration 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 T: documentation Improvements to the docs (e.g. new topic, correction, etc)

Comments

@orn688
Copy link
Contributor

orn688 commented Jun 1, 2020

Describe the bug A clear and concise description of what the bug is.

The example pyproject.toml shown in the Configuration format section of the README has the following exclude:

[tool.black]
line-length = 88
target-version = ['py37']
include = '\.pyi?$'
exclude = '''

(
  /(
      \.eggs         # exclude a few common directories in the
    | \.git          # root of the project
    | \.hg
    | \.mypy_cache
    | \.tox
    | \.venv
    | _build
    | buck-out
    | build
    | dist
  )/
  | foo.py           # also separately exclude a file named foo.py in
                     # the root of the project
)
'''

However, the comment next to foo.py seems to be incorrect. That foo.py will actually exclude any foo.py found in the project, not just in the root.

To Reproduce Steps to reproduce the behavior:

  1. mkdir temp && cd temp
  2. Create pyproject.toml containing the example configuration from above
  3. touch foo.py
  4. mkdir bar
  5. touch bar/foo.py
  6. black --verbose . prints the following:
    Using configuration from /path/to/temp/pyproject.toml.
    foo.py ignored: matches the --exclude regular expression
    bar/foo.py ignored: matches the --exclude regular expression
    No Python files are present to be formatted. Nothing to do 😴
    

Expected behavior A clear and concise description of what you expected to happen.

I would expect Black to not exclude bar/foo.py. It would be nice if there were a way to actually accomplish what the example says (i.e., only exclude foo.py in the project root). Short of that, I think the documentation could use rewording to avoid suggesting that the example pyproject.toml only excludes foo.py in the project root.

Environment (please complete the following information):

  • Version: master
  • OS and Python version: Linux/Python 3.7.7

Does this bug also happen on master? yes

@orn688 orn688 added the T: bug Something isn't working label Jun 1, 2020
@sittingbool
Copy link

Same on MaxOSX and Python 3.8

@orn688
Copy link
Contributor Author

orn688 commented Dec 7, 2020

Update: you can exclude a file only if it's in the root of the project by prepending ^/ to the path. / represents the root of the directory, but you additionally need the ^ because Black uses re.search() to check whether the exclude regex matches each path. Unlike re.match() which only checks for matches at the beginning of the string, re.search() checks for matches anywhere in the string, which is why you need the ^ to anchor matches to the beginning of the exclude string.

So I think the README example should be updated to this:

exclude = '''
^/(
  (
      \.eggs         # exclude a few common directories in the
    | \.git          # root of the project
    | \.hg
    | \.mypy_cache
    | \.tox
    | \.venv
    | _build
    | buck-out
    | build
    | dist
  )/
  | foo.py           # also separately exclude a file named foo.py in
                     # the root of the project
)
'''

(Note that the forward slash before the second opening paren has been moved to precede the first opening paren so it applies to foo.py as well, and is now also preceded by a ^.)

Black's default exclude might also need to be updated – the default exclude it caused issues for my team when we started using Black because we have a library called build in our project that's nested under a subdirectory of the root. But Black still skipped formatting it because it excludes build directories at any nesting level.

orn688 added a commit to orn688/black that referenced this issue Dec 7, 2020
The `exclude` section of the example `pyproject.toml` file didn't work
as expected. It claimed to exclude matched files only in the project
root, but it actually excluded matched files at any directory level
within the project. We can address this by prepending `^/` to the regex
to ensure that it only matches files in the project root.

See psf#1473 (comment) for
explanation.
orn688 added a commit to orn688/black that referenced this issue Dec 7, 2020
The `exclude` section of the example `pyproject.toml` file didn't work
as expected. It claimed to exclude matched files only in the project
root, but it actually excluded matched files at any directory level
within the project. We can address this by prepending `^/` to the regex
to ensure that it only matches files in the project root.

See psf#1473 (comment) for
explanation.
orn688 added a commit to orn688/black that referenced this issue Dec 8, 2020
The `exclude` section of the example `pyproject.toml` file didn't work
as expected. It claimed to exclude matched files only in the project
root, but it actually excluded matched files at any directory level
within the project. We can address this by prepending `^/` to the regex
to ensure that it only matches files in the project root.

See psf#1473 (comment) for
explanation.
JelleZijlstra pushed a commit that referenced this issue Jan 27, 2021
* Update example exclude to match only files in root

The `exclude` section of the example `pyproject.toml` file didn't work
as expected. It claimed to exclude matched files only in the project
root, but it actually excluded matched files at any directory level
within the project. We can address this by prepending `^/` to the regex
to ensure that it only matches files in the project root.

See #1473 (comment) for
explanation.

* Mention excluding directories as well
@ichard26 ichard26 added C: configuration CLI and configuration C: file collection Related to file collection (e.g. gitignore & cache) or file discovery and all of its configuration. T: documentation Improvements to the docs (e.g. new topic, correction, etc) labels May 29, 2021
@ichard26
Copy link
Collaborator

Closing as this was fixed by #1861, thank you @orn688 for both reporting and fixing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: configuration CLI and configuration 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 T: documentation Improvements to the docs (e.g. new topic, correction, etc)
Projects
None yet
Development

No branches or pull requests

3 participants