Skip to content

black does not exclude files in git submodules #3040

@janjachnik-dyson

Description

@janjachnik-dyson

Describe the bug

black does not exclude files in git submodules when full filename is passed

To Reproduce

I created an example project with a git submodule to test. The super-project has a pyproject.toml which excludes the subproject:

[tool.black]
force-exclude = 'subproject'

Commands to clone and reproduce bug:

git clone --recurse-submodules git@github.com:janjachnik-dyson/black-submodule-test-super.git
cd black-submodule-test-super
black --check subproject/file.py

The output:

Identified `/tmp/black-submodule-test-super/subproject` as project root containing a .git directory.
Sources to be formatted: "file.py"
subproject/file.py already well formatted, good job.

All done! ✨ 🍰 ✨
1 file would be left unchanged.

Expected behavior

subproject/file.py would be ignored because its path matches the force-exclude regex in pyproject.toml

Environment

  • Black's version: 22.3.0
  • OS and Python version: Ubuntu 20.04, Python 3.8.10

Additional context

We use black from within Visual Studio Code, where it is always called with the full filename. If black is manually run on the root folder of the super-project, the excludes are handled correctly. Within VSCode we pass the --config argument to specify the correct configuration file of the super-project.

Possible solution

The problem occurs because black decides the project root by recursively searching parent directories for .git, .hg and pyproject.toml files. In this case, the project root becomes the subproject folder (due to .git) and the exclude regex is compared against the path relative to the project root, which no longer contains the subproject string.

As a possible fix, I added the option to manually specify the project root:
janjachnik-dyson@6fc3368

Then, the following command works to properly exclude the file:

black --project-root . subproject/file.py

If this sounds like a valid fix/workaround, I'll raise a PR. Or is there a better way to handle excludes with git submodules?

Metadata

Metadata

Assignees

No one assigned

    Labels

    C: file collectionRelated to file collection (e.g. gitignore & cache) or file discovery and all of its configuration.T: bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions