Skip to content

Decouple using .gitignore from exclude #3711

Open
@aureliojargas

Description

Is your feature request related to a problem? Please describe.

This is the current behaviour for handling .gitignore:

https://black.readthedocs.io/en/stable/usage_and_configuration/file_collection_and_discovery.html#gitignore

If --exclude is not set, Black will automatically ignore files and directories in .gitignore file(s), if present.

If you want Black to continue using .gitignore while also configuring the exclusion rules, please use --extend-exclude.

However, my case does not fill in those two scenarios.

  • Our project is a build system, and one of its subpackages is named build.

  • The directory named build is listed in DEFAULT_EXCLUDES:

DEFAULT_EXCLUDES = r"/(\.direnv|\.eggs|\.git|\.hg|\.ipynb_checkpoints|\.mypy_cache|\.nox|\.pytest_cache|\.ruff_cache|\.tox|\.svn|\.venv|\.vscode|__pypackages__|_build|buck-out|build|dist|venv)/" # noqa: B950

  • Black does not have an option to remove items from the default list, so my only alternative to workaround this is redefining the exclude option/setting. By doing so, black stops honouring my .gitignore file 😭

Alternatives I've tried

  • exclude has precedence over include, so doing --include '/build/|\.pyi?$' does not work.

  • The only way I could make it work was additionally informing the full path for the Python files inside the build directories: black . foo/cli/build/*.py tests/foo/cli/build/*.py. But this is not an ideal solution since I cannot specify this as black configuration in pyproject.toml. As a result, developers running black . will think they're formatting everything (no warning about the skipped files), and those paths will have to be hardcoded somewhere else (Makefile, tox.ini, ...).

Describe the solution you'd like

I want black . to format all the Python files in the repository, skipping all the paths listed in .gitignore.

I have the same problem with ruff and isort, since they also exclude build by default. But both have specific options to control the reading of the .gitignore file:

Then I can use settings to workaround this issue:

[tool.ruff]
# Files in `foo/cli/build/` are ignored by ruff because the `exclude`
# list contains the `build` word by default. We redefine it with just
# `.git`, since the other paths are already listed in `.gitignore`.
respect-gitignore = true
exclude = [".git"]

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions