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 also when it is not in the root folder of the project #236

Closed
japsu opened this issue May 12, 2023 · 8 comments
Closed
Assignees
Labels
info-needed Issue requires more information from poster triage-needed Issue is not triaged.

Comments

@japsu
Copy link

japsu commented May 12, 2023

Consider a project layout in which you have

  • myproject/ (project root, opened in VS Code)
    • backend/ (root of Python universe)
      • pyproject.toml with [tool.black] section
    • frontend/ (root of Node.js universe)

Now when editing Python files under myproject/backend, the [tool.black] section in myproject/backend/pyproject.toml will not be honoured.

If the pyproject.toml resides in the root folder myproject/, it is honoured.

If the myproject/backend folder is opened in VS Code instead of myproject, it is honoured.

Please make the Black extension also honour the pyproject.toml file that is in a parent folder of the file being edited even if that folder is not the project root.

@japsu japsu changed the title Honor pyproject.toml also when it is not in the root folder of the project Honour pyproject.toml also when it is not in the root folder of the project May 12, 2023
@github-actions github-actions bot added the triage-needed Issue is not triaged. label May 12, 2023
@karthiknadig
Copy link
Member

@japsu We don't control how black automatically finds pyproject.toml. From black's own documentation https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#where-black-looks-for-the-file . Black looks for config files in the current working directory and its parents, this is why it works when you just open backend.

Option 1:

You could pin black to always use the pyproject.toml from a particular location. Here is what you can do in the workspace settings:

"black.args":  ["--config", "${workspaceFolder}/backend/pyproject.toml"] 

This way you can open myproject and black should be able to use the [tool.black] from the pyproject.toml.

Option 2:

Another way to solve this is to make your project a multi-root project. In this mode all of your python configurations will sit in backend. You can do this by creating a myproject.code-workspace file, with following contents:

{
  "folders": [
    {
      "name": "myproject",
      "path": "."
    },
    {
      "name": "backend",
      "path": "./backend"
    },
    {
      "name": "frontend",
      "path": "./frontend"
    },
  ],
  "settings": {
  },
}

@karthiknadig karthiknadig self-assigned this May 15, 2023
@karthiknadig karthiknadig added the info-needed Issue requires more information from poster label May 15, 2023
@renan-abm
Copy link

renan-abm commented Jun 1, 2023

I don't know exactly why but I see the same behavior as @japsu

In the Black Formatter output tab, I have this:

2023-06-01 15:44:23.449 [info] /opt/conda/envs/abm/bin/python -m black -v --stdin-filename /workspaces/software/backends/src/app/main.py -
2023-06-01 15:44:23.454 [info] CWD formatter: /workspaces/software
2023-06-01 15:44:23.618 [info] Identified `/workspaces/software/backends` as project root containing a pyproject.toml.
Sources to be formatted: "-"
reformatted /workspaces/software/backends/src/app/main.py

All done! ✨ 🍰 ✨
1 file reformatted.

If I run a similar command in a terminal inside VSCode I get different results:

/opt/conda/envs/abm/bin/python -m black -v /workspaces/software/backends/src/app/main.py
Identified `/workspaces/software/backends` as project root containing a pyproject.toml.
Sources to be formatted: "src/app/main.py"
Using configuration from project root.
line_length: 120
/workspaces/software/backends/src/app/main.py already well formatted, good job.

All done! ✨ 🍰 ✨
1 file left unchanged.

Both report Identified /workspaces/software/backends as project root containing a pyproject.toml but only one of them honours the config.

@karthiknadig
Copy link
Member

@renan-abm There might be a behavior difference in black when using stdin.
Try this (don't forget the - at the end):

cat /workspaces/software/backends/src/app/main.py | /opt/conda/envs/abm/bin/python -m black -v --stdin-filename /workspaces/software/backends/src/app/main.py -

If that behaves differently, when compared with:

/opt/conda/envs/abm/bin/python -m black -v /workspaces/software/backends/src/app/main.py

Then you have an excellent repro and you should file this bug on black. Basically it is not honoring the pyproject.toml when using stdin mode. We have to use stdin mode to work with unsaved buffers in any editor.

@renan-abm
Copy link

You're right @karthiknadig that's an issue with black and stdin indeed, similar to isort I guess (fixed but not released yet). Thanks, I'll file a bug on black.

@renan-r-santos
Copy link

FYI: I made a PR upstream with a potential fix to this

@renan-r-santos
Copy link

Yesterday black 23.7.0 was released containing the fix to this issue and after some testing, it seems to be working properly. Feel free to close it.

@karthiknadig
Copy link
Member

@renan-r-santos Thank you!!!

@karthiknadig karthiknadig closed this as not planned Won't fix, can't repro, duplicate, stale Jul 11, 2023
@karthiknadig
Copy link
Member

For anyone running into this issue with bundled black which is 23.3.0, this is due to python 3.7 support which we keep for 3 months post EOL on python. We will make a release after that 3 month mark that will use python 3.8 as min and update black to latest on python 3.8.

For now, install black in your environment and use fromEnvironment strategy if you need this fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
info-needed Issue requires more information from poster triage-needed Issue is not triaged.
Projects
None yet
Development

No branches or pull requests

4 participants