简体   繁体   中英

How to make VSCode honor black excluded files in pyproject.toml configuration when using formatOnSave

I have the following pyproject.toml for configuring black:

[tool.black]
exclude = 'foo.py'

If I run black. from the project's root folder that only contains foo.py , I get No Python files are present to be formatted. Nothing to do � No Python files are present to be formatted. Nothing to do � as expected.

However, when I save foo.py from within VS Code (I have black configured as the formatter and enabled Format On Save), the file is still formatted by black.

Interestingly, VS Code seems to honor other configurations, eg line-length .

Is there a way to make VSCode honor the exclude configuration?

If you list a file explicitly, this takes precedence over the any excluded files. This seems to be what black's developers consider the expected behaviour, see for example this issue . Also, this won't be fixed within vscode, see this issue .

This is also a problem when using pre-commit .

Since I didn't want to maintain yet another list (I do this for pre-commit already) of files for which I do not want to use FormatOnSave I ended up using a different approach. Since all of the files that I don't want to auto-format on save reside in a specific folder within my project, I ended up using the workaround described in this answer and deactivate FormatOnSave in the sub-folder settings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM