microsoft / vscode-black-formatter

Formatting support for Python using the Black formatter
https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter
MIT License
146 stars 35 forks source link

.vscode/settings.json black settings in multi root project are not used #407

Closed tiborrr closed 5 months ago

tiborrr commented 6 months ago

I have a project in which I have a top level .vscode/settings.json with only some basic settings.

{
    "python.testing.unittestArgs": [
        "-v",
        "-s",
        "./tests",
        "-p",
        "*test*.py"
    ],
    "python.testing.pytestEnabled": false,
    "python.testing.unittestEnabled": true,
}

I have a folder to which I want to apply custom black settings. To do so I have created:

src/main/xml_generators/.vscode/settings.json

with the following settings:

{
    "black-formatter.args": ["--line-length", "200"],
}

This however does not work. Looking at the log. I see that setting are not applied:

2024-01-03 14:31:59.295 [info] /home/tibor/project/.venv/bin/python -m black --stdin-filename /home/tibor/project/src/main/xml_generators/de.py -
2024-01-03 14:31:59.295 [info] CWD formatter: /home/tibor/project
2024-01-03 14:31:59.426 [info] reformatted /home/tibor/project/src/main/xml_generators/de.py

All done! ✨ 🍰 ✨
1 file reformatted.
karthiknadig commented 6 months ago

@tiborrr Are you using *.code-workspace to define the various project roots? VS code will treat each folder added in .code-workspace as a project root and only then it can use the .vscode/setting.json if you have multiple projects in the same workspace.

What I recommend is using black config like this https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#configuration-via-a-file , and set the black-formatter.cwd to ${fileDirname} this will allow black to automatically pick up the settings. This way it will work in your CI and in VS code the same way. If you use the other approach you will have to handle it separately in CI.

Lastly, the initial part of the Black formatter logs shows you the settings the extension sees.

karthiknadig commented 5 months ago

Because we have not heard back with the information we requested, we are closing this issue for now. If you are able to provide the info later on then we will be happy to re-open this issue to pick up where we left off.