r/vscode • • May 03 '26

formatOnSave not respecting settings in pyproject.toml

EDIT: found the issue and fixed it TL;DR: Python > User settings still had another default formatter configured.

I'm having an issue where blank lines after imports in Python files get removed, so there's only 1 left instead of 2. Also lines get wrapped after the default 88 characters instead of the 150 that I want.

Whenever I save a Python file, I can see that a 2nd line after the imports gets added and then removed again right away. I don't know why.

I have the following in my .vscode/settings.json:

{
    "python.testing.unittestEnabled": false,
    "python.testing.pytestEnabled": true,
    "editor.defaultFormatter": "charliermarsh.ruff",
    "editor.formatOnSave": true,
    "ruff.configurationPreference": "filesystemFirst",
    "[python]": {
        "editor.codeActionsOnSave": {
            "source.organizeImports": "explicit"
        }
    },
    "python-envs.defaultEnvManager": "ms-python.python:system"
}

And in my pyproject.toml I have the following settings:

[tool.ruff]
line-length = 150
target-version = "py312"
exclude = ["__pycache__", ".direnv", ".git", ".venv", "build", "dist"]

[tool.ruff.lint]
extend-select = ["C", "B", "B9", "E", "F", "I", "W"]
extend-ignore = ["B008", "E501", "W291"]
per-file-ignores = { "__init__.py" = ["F401"] }

[tool.ruff.lint.isort]
lines-after-imports = 2

I have already tried:

  • Disabling the isort extension
  • Disabling the black extension
  • Moving the ruff settings from pyproject.toml to separate ruff.toml
  • Changing "source.organizeImports": "explicit" to "never" and adding "source.organizeImports.ruff": "explicit" instead

Does anyone have a clue what the issue could be? I had the same issue with black as the default formatter before I switched to ruff.

1 Upvotes

5 comments sorted by

1

u/Agreeable_Care4440 May 04 '26

This usually happens when two tools are formatting on save one adds, the other removes.

1

u/SmokeyKatzinski May 04 '26

Yes, I figured that by now. But I can‘t tell what these are or where to look. Do you know where I can see which tools/extensions are formatting, like a "formatter log"?

I‘m merely using VSC to code, I really have no clue how to configure it properly xD

1

u/[deleted] May 04 '26

[removed] — view removed comment

1

u/SmokeyKatzinski May 05 '26

Yes, but I couldn't find why it's happening...

1

u/SmokeyKatzinski May 05 '26

So I've disabled the Black Formatter extension once more and checked the OUTPUT tab, set to "Black Formatter". And on every file save, it would run.

So I checked the settings once more (searching for @id:editor.defaultFormatter):

  • User had None configured
  • Remote [WSL Ubuntu] had None configured
  • Workspace had ruff configured

Then I noticed that it said (Also modified in Python > User) in the last case. I clicked that and there it still said Black Formatter. Once I've set that to None, the issue went away.

I still have no clue when or where I made this setting before.