Search code examples
pythonvisual-studio-codecode-formattingpyproject.tomlblack-code-formatter

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 �😴 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, e.g. line-length.

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


Solution

  • The --force-exclude option also excludes files if they are explicitly listed. Thus, it also works with formatOnSave in VS Code.

    In the example above, simply use

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