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

No configuration used for paths after the first #4461

Open
tmct opened this issue Sep 18, 2024 · 2 comments
Open

No configuration used for paths after the first #4461

tmct opened this issue Sep 18, 2024 · 2 comments
Labels
T: bug Something isn't working

Comments

@tmct
Copy link

tmct commented Sep 18, 2024

Describe the bug

Black configurations are seemingly not taken into account for any paths after the first one passed to CLI - I'm seeing this in particular for "line-length" but have no reason to expect the behaviour is specifically related to that option.

Specifically this prevents me from running black on multiple projects in one command. Perhaps this is by design.

To Reproduce

Black version

black, 24.8.0 (compiled: yes)
Python (CPython) 3.10.10

Project setup

# repro/proj1/hi.py
print("hi")
# repro/proj1/pyproject.toml
[project]
name = "proj1"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = []

[tool.black]
line-length = 120
# repro/proj2/hello.py
import os


def main():
    return "abcdefghijk" if os.getcwd() is not None else "aeheahaehaeh;kaehbnmieasolngoaeinaeghaehgoiaen"


if __name__ == "__main__":
    main()
# repro/proj2/pyproject.toml
[project]
name = "proj2"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = []

[tool.black]
line-length = 120

Actual behaviour

(running commands from inside proj1)

$ black --check --diff . 
All done! ✨ 🍰 ✨
1 file would be left unchanged.
$ black --check --diff ../proj2
All done! ✨ 🍰 ✨
1 file would be left unchanged.
$ black --check --diff . ../proj2
--- /home/tom/code/repro/proj2/hello.py  2024-09-18 13:23:06.381794+00:00
+++ /home/tom/code/repro/proj2/hello.py  2024-09-18 13:44:24.114345+00:00
@@ -1,9 +1,13 @@
 import os
 
 
 def main():
-    return "abcdefghijk" if os.getcwd() is not None else "aeheahaehaeh;kaehbnmieasolngoaeinaeghaehgoiaen"
+    return (
+        "abcdefghijk"
+        if os.getcwd() is not None
+        else "aeheahaehaeh;kaehbnmieasolngoaeinaeghaehgoiaen"
+    )
 
 
 if __name__ == "__main__":
     main()
would reformat /home/tom/code/repro/proj2/hello.py

Oh no! 💥 💔 💥
1 file would be reformatted, 1 file would be left unchanged.

Expected behavior

Either one of the configurations would be taken into account, and I would be able to check both folders at once. (I don't mind which in my use case - these particular projects have identical tooling configurations.)

Environment

  • Black's version: 24.8.0
  • OS and Python version: [Ubuntu 20.04/Python (CPython) 3.10.10]

Thanks

@tmct tmct added the T: bug Something isn't working label Sep 18, 2024
@MeGaGiGaGon
Copy link
Contributor

MeGaGiGaGon commented Sep 24, 2024

This appears to be by design. From https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#where-black-looks-for-the-file

By default Black looks for pyproject.toml containing a [tool.black] section starting from the common base directory of all files and directories passed on the command line. If it’s not there, it looks in parent directories. It stops looking when it finds the file, or a .git directory, or a .hg directory, or the root of the file system, whichever comes first.

The common parent of repro/proj1 and repro/proj2 is repro, which based on what you said doesn't have a pyproject.toml with the config you want.

If you’re running with --verbose, you will see a message if a file was found and used.

So if you run black --check --diff . ../proj2 with --verbose, you should get some message saying no pyproject.toml was used

As for how to get black to use your config, either create a pyproject.toml in that shared repro directory, or

You can also explicitly specify the path to a particular file that you want with --config. In this situation Black will not look for any other file.

@jasmeen04
Copy link

jasmeen04 commented Sep 30, 2024

Hi team,

I’ve reviewed the issue and understand that Black currently only recognizes configurations from the common base directory when multiple paths are passed, which seems to be by design. However, I’d like to propose an enhancement to improve usability in multi-project setups.

Proposed Solution:
I can work on a feature that allows Black to recognize multiple pyproject.toml files across different project directories in a single command. This would allow Black to apply the relevant configuration for each project, eliminating the need for separate runs.

Alternatively, if adding this feature doesn’t align with Black’s design philosophy, I’d be happy to contribute to the documentation. I can provide clear guidelines on how Black handles configurations with multiple paths and offer best practices for users running Black on several projects.

Approach:
I’ll start by adjusting Black’s configuration discovery logic, ensuring backward compatibility. If needed, I’ll update the documentation with detailed examples for users managing multiple projects.

Please assign me this issue to work upon if possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants