Skip to content

Black stops looking for config at first pyproject.toml file even if there's no black section in it #2863

@slafs

Description

@slafs

Disclaimer: I was hesitant whether I should report this as a bug or rather a "Feature request".

Describe the bug

In a project dealing with multiple sub-projects in a single repository (aka. mono-repo style) having more than one pyproject.toml file confuses black.
In our example we wanted to have one top-level black configuration,
while also having sub-project specific pyproject.toml files (without any black config) in each sub-directory.
Unfortunately, running black on a sub-directory
uses default configuration instead of the one defined in the top-level config file.

To Reproduce

Given this file structure:

.
|-- pyproject.toml
|-- sub1
|   |-- pyproject.toml
|   `-- test.py
`-- sub2
    `-- test.py

With the following contents:

==> ./pyproject.toml <==
[tool.black]
line-length = 20

==> ./sub1/pyproject.toml <==
[tool.isort]
profile = "black"

==> ./sub1/test.py <==
a_very_long_line = {"that should be wrapped": "to fit 20 characters line length"}

==> ./sub2/test.py <==
a_very_long_line = {"that should be wrapped": "to fit 20 characters line length"}

Running black on both test.py files:

$ black -v sub1/test.py
Identified `/private/tmp/black-many-configs/sub1` as project root containing a pyproject.toml.
Sources to be formatted: "test.py"
sub1/test.py already well formatted, good job.

All done! ✨ 🍰 ✨
1 file left unchanged.

$ black -v sub2/test.py
Identified `/private/tmp/black-many-configs` as project root containing a pyproject.toml.
Sources to be formatted: "sub2/test.py"
Using configuration from project root.
reformatted sub2/test.py

All done! ✨ 🍰 ✨
1 file reformatted.

Modifies only the file inside sub2 directory (the one without pyproject.toml file)
even though sub1/pyproject.toml doesn't contain any black config section.

Expected behavior

Both files should be modified
and black should continue looking for a file
that is relevant for its configuration (in this case, the one that has a [tool.black] section).

Environment

  • Black's version: black, 22.1.0 (compiled: yes)
  • OS and Python version: OSX - Python 3.9.9

Additional context

A variation of this problem has already been reported in #1826, where the proposed solution is to introduce support for the config option inside the sub-project's pyproject.toml config file.

Other (potentially) relevant issues: #2537, #2850.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions