astral-sh/ruff-vscode

Ruff format/fix on save not working in devcontainer until after window reload

eschnitzler opened this issue · 8 comments

I am encountering an issue with the Ruff VSCode extension where the autofix on save feature does not work initially when using a devcontainer. The feature only starts working after I reload the VSCode window.

Steps to Reproduce:

  1. Open a project in a VSCode devcontainer.
  2. Ensure that the recommended settings are configured in devcontainer.json:
    "[python]": {
                     "editor.formatOnSave": true,
                     "editor.codeActionsOnSave": {
                         "source.fixAll": "explicit",
                         "source.organizeImports": "explicit"
                     },
                     "editor.defaultFormatter": "charliermarsh.ruff"
                 }
  3. Make an auto-fixable linting error in a python file and try to save it.

Expected Behavior:
The Ruff extension should automatically format and fix issues on save without needing to reload the VSCode window first.

Additional Information:

VSCode Version: 1.90.2
Ruff Extension Version: v2024.28.0
Operating System: Pop_Os 22.04

I see the same thing happening (settings specified via [python] ... in devcontainer.json not taking effect until after detach+reattach to dev container) for editor.rulers. So, my guess would be a bug in the VSCode dev-container plugin, not in ruff-vscode.

(I think I also saw this happening for editor.formatOnSave, like this issue reports)

Can you provide some additional information?

  1. Any ruff.* scoped settings if set
  2. Content of the Ruff configuration is it's being used (pyproject.toml, ruff.toml, etc.)

Do you face this issue when using VS Code locally (not inside devcontainer)?

I am not sure whether "you" is referring to me or the original poster @eschnitzler but I will leave the keyboard for several weeks within minutes, so I will not be able to investigate this.

Sorry for the confusion. I'm referring to the issue author @eschnitzler

@dhruvmanila Here are my ruff configured settings in my pyproject.toml file:

[tool.ruff]
exclude = [
    ".git",
    ".pytest_cache",
    ".ruff_cache",
    ".venv",
    ".vscode",
    "__pypackages__",
    "_build",
    "build",
    "dist",
    "node_modules",
    "site-packages",
    "venv",
]
line-length = 105
indent-width = 4

[tool.ruff.lint]
select = [
    "B",  # flake8-bugbear
    "C4", # flake8-comprehensions
    "E",  # Error (pycodesyle)
    "W",  # Warning (pycodesyle)
    "F",  # pyflakes
    "I",  # isort
    "ISC", # flake8-implicit-str-concat
    "PGH",  # pygrep-hooks
    "Q",  # flake8-quotes
    "DJ", # flake8-django
    "SIM",  # flake8-simplify
    "UP",  # pyupgrade
    "C4", # flake8-comprehensions
    "T10", # flake8-debugger
    "PT", # flake8-ptest-style
    "C90", # flake8-mccabe
]
ignore = [
    "W191",  # Indentation contains tabs
    "E111",  # Indentation is not a multiple of four
    "E114",  # Indentation is not a multiple of four (comment)
    "E117",  # Over-indented
    "E501",  # Line too long
    "Q000",  # Remove bad quotes (inline)
    "Q001",  # Remove bad quotes (multiline)
    "Q002",  # Remove bad quotes (docstring)
    "Q003",  # Remove avoidable escaped quote
    "ISC001",  # Implicit string concatenation (single-line)
    "ISC002",  # Implicit string concatenation (multi-line)
    "B026", # Star-arg unpacking after a keyword argument is strongly discouraged
    "B904", # 'assertRaises' should be used as a context manager
    "DJ001" # Avoid using null=True on string-based fields such as CharField and TextField
]

[tool.ruff.lint.per-file-ignores]
"**/tests/**" = ["C90"]


[tool.ruff.format]
docstring-code-format = true
exclude = [
    "app/migrations/**",
]

[tool.ruff.lint.mccabe]
max-complexity = 15

The issue only occurs when inside a devcontainer.