astral-sh/ruff-vscode

ruff.interpreter causes ruff 2024.32.0 and later to fail silently

nwns opened this issue · 4 comments

nwns commented

When using extension version newer than 2024.30.0 ruff cannot format documents (according to VSCode, see error dialogs below) and generally doesn't work at all. In my experimenting I have found that the linting sort of works when I select the system python 3.12 but still no formatter. At this point, effectively all of my python coding is done in a dev container.

When I am running VSCode locally I get the following error:

Configure Default Formatter
Extension 'Ruff' is configure as formatter but it cannot format 'Python'-files

When I run VSCode in a local Dev Container I get the following:

There is no formatter for 'python' files installed.

I assume the difference is because the local VSCode still has the Black Formatter extension installed but the Dev Container only has Ruff.

Either way the log is similar to the below Ruff log from the minimum reproducible workspace, which is just minimum.py in by itself. The log below is what is output when I try to format minimum.py using 2024.34.0, 2024.32.0, and 2024.30.0 (which does actually format minimum.py by removing the import.

from django.utils import timezone

def test_format():
    pass
2024-07-19 09:32:09.922 [info] Name: Ruff
2024-07-19 09:32:09.922 [info] Module: ruff
2024-07-19 09:37:08.792 [info] Name: Ruff
2024-07-19 09:37:08.792 [info] Module: ruff
2024-07-19 09:37:37.334 [info] Name: Ruff
2024-07-19 09:37:37.334 [info] Module: ruff
2024-07-19 09:37:37.334 [info] Server run command: python3 /home/nigel/.vscode/extensions/charliermarsh.ruff-2024.30.0-linux-x64/bundled/tool/ruff_server.py server --preview
2024-07-19 09:37:37.334 [info] Server: Start requested.

Select vscode settings.json parts that mention ruff. The day to day configuration is done with the pyproject.toml (relevant part included below)

    "[python]": {
        "editor.defaultFormatter": "charliermarsh.ruff",
        "editor.formatOnType": true
    },
    "dev.containers.defaultExtensions": [
        "codeium.codeium",
        "ms-python.python",
        "tamasfe.even-better-toml",
        "streetsidesoftware.code-spell-checker",
        "charliermarsh.ruff",
        "Codium.codium"
    ],
    "ruff.showNotifications": "always",
    "ruff.interpreter": [
        "python3"
    ],
    "ruff.logLevel": "trace",
[tool.ruff]
ignore = [
  'ANN', # Annotations
  'ANN101', # Missing Self type
  'B905', # zip() without an explicit strict= parameter
  'D', # pydocstyle linting
  'D203', # Blank Line before class Docstring (D202 is inverted)
  'D212', # Multiline Doc string starts following quotes. D213 one line after quotes
  'EM101', # Exception must not use a string literal, assign to variable first
  'EM102', # Exception must not use an f-string literal, assign to variable first
  'ERA001', # Commented out code
  'FBT', # Boolean Trap (boolean arguments to functions and methods)
  'G004', # Logging statement uses f-string
  'INP001', # File is part of an implicit namespace package. Add an __init__.py.
  'N818', # Exception name should be named with an Error suffix
  'N999', # Snake Case Module Names
  'PGH003', # Use specific rule codes when ignoring type issues
  'PLR2004', # Magic value used in comparison
  'PT', # pytest rules
  'PTH123', # open() should be replaced by Path.open()
  'RET505', # Unnecessary else/elif after return statement
  'SIM108', # Ternary operator rather than if-else block
  'SIM118', # prevent use of keys() method on dictionaries
  'TID252', # Require absolute imports
  'TRY', # tryceratops (exception handling)
]
line-length = 79
select = ["ALL"]
src = ["."]
target-version = "py311"

[tool.ruff.per-file-ignores]
"**/*_test.py" = [
  "S101", # Used assert
  "SLF001", # Private member accessed
]

Software versions

vscode:

Version: 1.91.1
Commit: f1e16e1e6214d7c44d078b1f0607b2388f29d729
Date: 2024-07-09T22:08:12.169Z
Electron: 29.4.0
ElectronBuildId: 9728852
Chromium: 122.0.6261.156
Node.js: 20.9.0
V8: 12.2.281.27-electron.0
OS: Linux x64 6.8.0-38-generic snap
Distributor ID:	Ubuntu
Description:	Ubuntu 24.04 LTS
Release:	24.04
Codename:	noble

Custom Dev Container version

Distributor ID: Debian
Description:    Debian GNU/Linux 12 (bookworm)
Release:        12
Codename:       bookworm

In your assessment, is this something that changed between extension versions at some point?

nwns commented

Seems removing

    "ruff.interpreter": [
        "python3"
    ],

from the settings.json fixed the issue.

I posted even though I have already fixed this because I spent too long trying to figure this out and hopefully this will help someone in the future. I have no idea at this point why that setting was set to what it was.

nwns commented

@charliermarsh it seems to be, but what that would be, I have no idea.

As already mentioned. Not really sure why I had that setting set to begin with, but everything seems to be working without it. 🤷‍♂️

nwns commented

Now that I have looked at the documentation for the ruff.interpreter setting. I think I know why it was set.

When I initially started to use ruff in vscode, the system and/or the dev container did not have a python executable (ruff is too new for python to be 2.7, at least on my workstations), so it was having issues finding a python to use. Now the system and the dev container have python aliased to python3 so it is not an issue.

To test, I just tried removing the python aliases in the dev container and it did break but ruff did output a message to its log:

2024-07-19 16:25:54.493 [error] Python interpreter missing:
[Option 1] Select Python interpreter using the ms-python.python.
[Option 2] Set an interpreter using "ruff.interpreter" setting.
Please use Python 3.7 or greater.

Selecting the interpreter using ms-python did work and formatting and linting started to work.

I then set ruff.interpreter to python3 and later /usr/bin/python3. Both seems to break 2024.34.0 (I didn't try .32). Rolling back to 2024.30.0 and it starts working again. Removing ruff.interpreter setting works fine for me on .30 and .34. Note that for all of this, the ms-python was set as above.

My feel of the behavior is that ruff.interpreter overrides ms-python (which is fine, more specific setting and such), but the change seems to be that .34 fails to run the interpreter specified by ruff.interpreter but doesn't log anything about it and then either because ruff half crashed or just otherwise didn't offer some features, vscode then says that there is no formatter.

At this point, I think I will just leave ruff.interpreter unset until I run into a reason to set it. It is possible, but I don't know the history well enough to say, that the reason ruff.interpreter was set at all was because the initial version of the ruff vscode extension didn't support following the ms-python setting.