astral-sh/ruff-vscode

Misconfigured Python interpreter leads to Ruff extension not loading

chrisk314 opened this issue · 14 comments

Recently Ruff stopped performing any code actions for my python files and the extension state icon remains spinning indefinitely (see screenshot below). This issue is similar to #609 in appearance in the VS Code UI, but different in that there appears to be no communication with the ruff server.
image

This started happening a couple of weeks ago without any changes to my vs code settings. I'm on auto update, so I guess an update caused this. I've tried with ruff.nativeServer set to on, off, and auto. This is all I get in the ruff logs (verbose logging enabled). Seeing this with all my environments. I've tried uninstalling the extension and reinstalling.

2024-09-19 20:57:24.113 [info] Name: Ruff
2024-09-19 20:57:24.113 [info] Module: ruff
2024-09-19 20:57:24.113 [info] Python extension loading
2024-09-19 20:57:24.113 [info] Waiting for interpreter from python extension.
2024-09-19 20:57:24.574 [info] Python extension loaded
2024-09-19 20:57:24.900 [info] Using interpreter: /home/csk/.pyenv/versions/plugboard-3.12/bin/python

User VS Code settings file Ruff config

{
    "ruff.nativeServer": "auto",
    "ruff.trace.server": "verbose",
}

Project VS Code settings file (.vscode/settings.json)

{
    "python.defaultInterpreterPath": "/home/csk/.pyenv/versions/plugboard-3.12/bin/python3.12",
    "python.envFile": "${workspaceFolder}/.env",
    "python.testing.unittestEnabled": false,
    "python.testing.pytestEnabled": true,
    "python.testing.autoTestDiscoverOnSaveEnabled": true,
    "python.testing.pytestArgs": [
        "-rs", "tests/"
    ],
    "editor.formatOnSave": false,
    "[python]": {
        "editor.formatOnSave": true,
        "editor.defaultFormatter": "charliermarsh.ruff",
        "editor.codeActionsOnSave": {
            "source.organizeImports": "explicit",
            "source.fixAll": "explicit"
        }
    },
    "mypy.configFile": "pyproject.toml",
    "mypy.targets": ["./plugboard", "./tests"],
    "mypy.runUsingActiveInterpreter": true,
}

Pyproject Ruff settings (pyproject.toml)

[tool.ruff]  # Code formatting and linting
line-length = 100
src = ["plugboard", "tests"]
extend-exclude = ["__pycache__"]
output-format = "grouped"

[tool.ruff.format]
quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = false
line-ending = "auto"

[tool.ruff.lint]
select = ["D", "E", "F", "I", "W"]
ignore = ["D105", "D107", "D401", "E203", "E226", "E24", "E731", "E741", "F541", "F821", "W605"]
exclude = ["docs/*"]

[tool.ruff.lint.pydocstyle]
convention = "google"

[tool.ruff.lint.isort]
known-first-party = ["plugboard"]
section-order = ["future", "standard-library", "third-party", "first-party", "local-folder"]
no-lines-before = ["local-folder"]
force-sort-within-sections = true
combine-as-imports = true
split-on-trailing-comma = true
relative-imports-order = "furthest-to-closest"
lines-after-imports = 2

My env:

  • VS Code v1.94.0 Insider
  • Ruff VS Code extension v2024.48.0
  • Windows 11
  • WSL2 with Ubuntu 24.04
  • Python 3.12.6 installed in pyenv
  • Python virtualenv installed with pyenv-virtualenv
  • Ruff v0.5.7 installed in venv (working correctly on CLI)

This problem is not limited to pyenv virtualenvs. I'm having the same issue with all VS Code projects some of which use virtualenvs created with the system python using python -m venv .venv etc.

Thanks for opening a new issue.

Looking into it. I believe the extension gets as far as startServer because that's the only path where we enable the spinner. But it doesn't make it as far as logging the startServer message...

updateStatus(undefined, LanguageStatusSeverity.Information, true);
const extensionSettings = await getExtensionSettings(serverId);
const globalSettings = await getGlobalSettings(serverId);
let newLSClient = await createServer(
workspaceSettings,
projectRoot,
serverId,
serverName,
outputChannel,
{
settings: extensionSettings,
globalSettings: globalSettings,
},
);
traceInfo(`Server: Start requested.`);

I suspect it fails somewhere before createServer completes because we don't see the server kind in the status bar.

const { useNativeServer, executable } = await resolveNativeServerSetting(
settings,
projectRoot,
serverId,
outputChannel,
);
updateServerKind(useNativeServer);
if (useNativeServer) {
return createNativeServer(
settings,
serverId,
serverName,
outputChannel,
initializationOptions,
executable,
);
} else {
return createLegacyServer(settings, serverId, serverName, outputChannel, initializationOptions);
}

Just wondering because we had something similar in the past. Is there a message pop up showing up? If so, could you try dismissing it?

If not, could you try setting "ruff.nativeServer": "on", for debug purposes. Just in case it's a problem with the new native server but something that works with the old python based LSP.

There is no message popping up related to Ruff. I tried setting "ruff.nativeServer": "on" but the Ruff output is just the same as before. Btw my VS Code just auto-updated Ruff and the version is now v2024.50.0. Problem persists.

I don't know what the problem is but I noticed that we aren't correctly logging errors if the extension fails to handle some request. I'll put up a PR improving logging and that should hopefully help us to narrow this down. Sorry that you're running into this.

Maybe we can get the logs sooner. Do you see any error messages in the Output - Extension Host panel?

It should look like this (I intentionally introduced a bug to demonstrate this):

2024-09-20 16:43:52.542 [warning] [charliermarsh.ruff] Accessing a window scoped configuration for a resource is not expected. To associate 'ruff.showSyntaxErrors' to a resource, define its scope to 'resource' in configuration contributions in 'package.json'.
2024-09-20 16:43:53.546 [error] ReferenceError: setings is not defined
	at resolveNativeServerSetting (/home/micha/astral/ruff-vscode/dist/extension.js:21676:13)
	at createServer (/home/micha/astral/ruff-vscode/dist/extension.js:21727:51)
	at startServer (/home/micha/astral/ruff-vscode/dist/extension.js:21741:29)
	at async runServer (/home/micha/astral/ruff-vscode/dist/extension.js:25609:24)
	at async /home/micha/astral/ruff-vscode/dist/extension.js:25625:13

OK I believe I have found and fixed the root cause by observing the Extension Host (Remote) output as you suggested. I noticed some error messages related to misconfigured python interpreter paths like those shown below (some info redacted). Recently I did a bit of cleanup of my drive, removing many old, no longer required virtualenvs. I still have the code for a few of the affected projects in my VS Code workspace for reference, but I'm not actively developing them, hence they don't need working python envs.

2024-09-20 15:56:08.862 [error] Error: Failed to resolve env "/home/csk/src/org/an-org/a-project/.venv-dev/bin/python"
    at ae (/home/csk/.vscode-server-insiders/extensions/ms-python.python-2024.14.1-linux-x64/out/client/extension.js:2:2041224)
    at oe (/home/csk/.vscode-server-insiders/extensions/ms-python.python-2024.14.1-linux-x64/out/client/extension.js:2:2039184)
    at Immediate.<anonymous> (/home/csk/.vscode-server-insiders/extensions/ms-python.python-2024.14.1-linux-x64/out/client/extension.js:2:2035478)
    at processImmediate (node:internal/timers:478:21)

After removing the problematic projects from VS Code the Ruff extension started working correctly again. Adding these problematic projects back into VS Code causes the Ruff extension to stop working once again. Perhaps this could be a useful test case.

For me the problem is resolved. I'll leave it up to you to either close this now, or keep it open to address the underlying issue if that makes sense. It's worth noting that other VS Code python extensions such as Mypy do continue to function correctly in the presence of these misconfigured python envs, so it feels like an issue with the Ruff extension, though I'm not knowledgable enough to make that determination.

Thanks for all your help @MichaReiser. I was surviving with my ruff pre-commit hooks but having the extension working again is great.

Thanks. That's helpful. Yeah I think we can improve the handling of errors or being more resilient against misconfigured interpreters. I'll mark this as an improvement.

I am also seeing the stuck spinner. With the following error message in the Extension host Output:

2024-09-20 15:51:45.586 [error] Error: spawn UNKNOWN
	at ChildProcess.spawn (node:internal/child_process:421:11)
	at spawn (node:child_process:800:9)
	at execFile (node:child_process:352:17)
	at t.<computed> (node:electron/js2c/node_init:2:2610)
	at c:\Users\lonni\.vscode\extensions\charliermarsh.ruff-2024.50.0-win32-x64\dist\extension.js:1:51373
	at new Promise (<anonymous>)
	at S (c:\Users\lonni\.vscode\extensions\charliermarsh.ruff-2024.50.0-win32-x64\dist\extension.js:1:51338)
	at C (c:\Users\lonni\.vscode\extensions\charliermarsh.ruff-2024.50.0-win32-x64\dist\extension.js:1:51485)
	at c:\Users\lonni\.vscode\extensions\charliermarsh.ruff-2024.50.0-win32-x64\dist\extension.js:1:54135
	at async w (c:\Users\lonni\.vscode\extensions\charliermarsh.ruff-2024.50.0-win32-x64\dist\extension.js:1:53145)
	at async t.startServer (c:\Users\lonni\.vscode\extensions\charliermarsh.ruff-2024.50.0-win32-x64\dist\extension.js:1:56839)
	at async S (c:\Users\lonni\.vscode\extensions\charliermarsh.ruff-2024.50.0-win32-x64\dist\extension.js:1:397112)
	at async i.value (c:\Users\lonni\.vscode\extensions\charliermarsh.ruff-2024.50.0-win32-x64\dist\extension.js:1:397249)
	```

Related: #598