Ruff linting plugin for pylsp
python-lsp-ruff is a plugin for python-lsp-server that integrates Ruff's linting, code actions, and formatting capabilities. It leverages Ruff, an extremely fast Python linter and formatter written in Rust, providing a fast and comprehensive code analysis experience. The library is actively maintained, with frequent updates (current version 2.3.1) for bug fixes and compatibility with newer Ruff versions.
Common errors
-
Plugin 'ruff' did not respond in time / Ruff not running in editor
cause The `ruff` executable is not found in the system's PATH, or `python-lsp-server` and `python-lsp-ruff` are not installed within the same Python virtual environment.fixEnsure `ruff` is installed (`pip install ruff`) and accessible, and that both `python-lsp-server` and `python-lsp-ruff` are installed in the *same* virtual environment where your LSP client expects them. Reinstalling all three into a fresh virtual environment is a common fix. -
Code formatting not working as expected / Imports are not sorted automatically
cause Since `python-lsp-ruff` v2.0.0, the default import sorting functionality was removed when `ruff format` was integrated.fixAdd `"format": ["I"]` to your `ruff` formatting configuration within your LSP client settings to re-enable import sorting. -
Custom linting rules or ignores in LSP client settings are being ignored
cause A `pyproject.toml` or `ruff.toml` file in the project root overrides `ruff` configurations provided via the LSP client's settings.fixConfigure `ruff`'s rules (e.g., `select`, `ignore`, `line-length`) directly in your `pyproject.toml` (under `[tool.ruff.lint]`) or `ruff.toml` file to ensure they are respected by the LSP server. -
Linter/formatter not working for Jupyter notebooks (.ipynb files)
cause Older versions of `python-lsp-ruff` or `ruff` might struggle with `.ipynb` file extensions, preventing proper detection as Python code by the linter/formatter.fixUpdate both `python-lsp-ruff` and `ruff` to their latest versions. Ensure your LSP client and any `jupyter-lsp` integrations are also updated. Some specific editor configurations might be needed for full notebook support.
Warnings
- breaking Version 2.0.0 introduced breaking changes to code formatting. It now uses `ruff format` directly, and the default import sorting was removed.
- breaking Versions 2.1.0 and 1.6.0 introduced breaking compatibility requirements with `ruff`. Older `python-lsp-ruff` versions might not work with newer `ruff` versions, and vice-versa.
- gotcha Ruff itself ships with a built-in LSP server (`ruff-lsp` or `ruff server`) since `ruff>0.4.5`. This can be confused with `python-lsp-ruff`, which is a plugin for `python-lsp-server`. They serve different integration patterns.
- gotcha When a `pyproject.toml` or `ruff.toml` file is present in your project, `python-lsp-ruff` will often defer to its configuration, ignoring some options passed via `pylsp` settings.
- gotcha `python-lsp-ruff` disables other linters/formatters (e.g., `pycodestyle`, `pyflakes`, `mccabe`, `autopep8`, `yapf`, `flake8`, `pyls_isort`) by default to avoid duplicate diagnostics.
Install
-
pip install python-lsp-server ruff python-lsp-ruff
Imports
- Not directly imported
Quickstart
{
"python": {
"linting": {
"ruff": {
"enabled": true,
"config": "/path/to/pyproject.toml"
}
},
"formatting": {
"ruff": {
"enabled": true,
"format": ["I"]
}
},
"plugins": {
"pycodestyle": {"enabled": false},
"pyflakes": {"enabled": false}
}
}
}