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

Honour pyproject.toml when using stdin and calling black from parent directory #3718

Closed
renan-r-santos opened this issue Jun 5, 2023 · 0 comments · Fixed by #3719
Closed
Labels
T: bug Something isn't working

Comments

@renan-r-santos
Copy link
Contributor

Describe the bug

When running black with --stdin-filename, the pyproject.toml configuration isn't respected if running black from a parent directory relative to the location of pyproject.toml.

To Reproduce

Suppose you have a directory structure as follows:

backends
├── pyproject.toml
└── src
    └── app
        └── main.py

where main.py has just one print statement such as print("This line is too long") and pyproject.toml has this black config

[tool.black]
line-length = 20

If your current working directory is the parent of backends and you run black by passing the path to main.py it reformats the file as expected

❯ black -v backends/src/app/main.py
Identified `/Users/rrodrigues/Downloads/backends` as project root containing a pyproject.toml.
Sources to be formatted: "src/app/main.py"
Using configuration from project root.
line_length: 20
reformatted backends/src/app/main.py

All done! ✨ 🍰 ✨
1 file reformatted.

If you use stdin it reports the file was reformatted but the output shows it wasn't

❯ cat backends/src/app/main.py | black -v --stdin-filename backends/src/app/main.py -
Identified `/Users/rrodrigues/Downloads/backends` as project root containing a pyproject.toml.
Sources to be formatted: "-"
print("This line is too long")
reformatted backends/src/app/main.py

All done! ✨ 🍰 ✨
1 file reformatted.

Expected behavior

If your current working directory is backends, using stdin works as expected as shown below. The expected behavior is that going up one directory shouldn't change black's output when using stdin, as is the case if not using stdin.

cat src/app/main.py | black -v --stdin-filename src/app/main.py -
Identified `/Users/rrodrigues/Downloads/backends` as project root containing a pyproject.toml.
Sources to be formatted: "-"
Using configuration from project root.
line_length: 20
print(
    "This line is too long"
)
reformatted src/app/main.py

All done! ✨ 🍰 ✨
1 file reformatted.

Environment

  • Black's version: latest main
  • OS and Python version: MacOS 13, Python 3.11.3

Additional context

Issue originally reported at the Black VSCode extension repo.

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
1 participant