Sub topic 14
Using Ruff in your editor
This chapter assumes you can run ruff check and ruff format from the terminal. It also assumes you have a [tool.ruff] section in pyproject.toml, and that you know the difference between safe and unsafe fixes. Here you connect Ruff to your editor so that diagnostics, fixes and formatting happen as you type, using the same configuration as the command line.
How the editor integration works
Editors do not call ruff check for each keystroke. Ruff ships a language server, started with ruff server. It speaks the Language Server Protocol (LSP), a standard protocol that editors use to talk to background analysis tools. The editor starts the server once per workspace and sends it the text of each open file. The server answers with:
- Diagnostics: the lint violations shown as squiggles, with the rule code (for example
F401) in the message. - Code actions: quick fixes for a single violation, plus "fix all" and "organize imports" actions for the whole file.
- Formatting edits: the result of
ruff formatfor the whole file or for a selected range.
Because the server reads the same pyproject.toml (or ruff.toml) as the command line, the editor and CI normally agree.
The server is part of the ruff executable, so you need Ruff version 0.5 or later. Check with ruff --version. Older editor plugins used a separate Python package called ruff-lsp. New setups should use the built-in server.
Visual Studio Code
Install the extension named Ruff, published by Astral Software (extension ID charliermarsh.ruff). The extension bundles a copy of Ruff, so it works even if Ruff is not installed in your virtual environment. That bundled version can differ from the one your project pins, which can cause editor results to differ from CI. To avoid this, point the extension at the project's own executable in .vscode/settings.json:
{
"ruff.path": ["${workspaceFolder}/.venv/bin/ruff"],
"[python]": {
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.ruff": "explicit",
"source.organizeImports.ruff": "explicit"
}
}
}On Windows the path is ${workspaceFolder}/.venv/Scripts/ruff.exe. Each setting does one job:
| Setting | Effect |
|---|---|
| `editor.defaultFormatter` | Makes Ruff the formatter for Python files, in place of Black or another extension. |
| `editor.formatOnSave` | Runs `ruff format` equivalent edits each time you save. |
| `source.fixAll.ruff` | On save, applies every fix that Ruff marks as safe, like `ruff check --fix`. |
| `source.organizeImports.ruff` | On save, sorts imports using the `I` rules. |
The value "explicit" means the action runs when you save by hand. It does not run on autosave. Use "always" if you want it on every save, and "never" to turn an action off.
The fix-all action fixes only what ruff check --fix would fix. Unsafe fixes still need to be applied from the lightblight-bulb menu on a single violation, or enabled with unsafe-fixes = true in your Ruff config.
Editor settings and the project configuration
The extension lets you set Ruff options in VS Code, such as ruff.lint.select or ruff.lineLength. The setting ruff.configurationPreference decides what wins when the editor and pyproject.toml disagree:
editorFirst(the default): editor settings override the configuration file.filesystemFirst: the configuration file overrides editor settings.editorOnly: configuration files are ignored.
For a team project, set "ruff.configurationPreference": "filesystemFirst", or leave the editor rule settings empty. The goal is that the repository is the single source of truth. A rule you enable only in your own editor will show squiggles that CI never reports. A rule you disable only in your editor will pass locally and fail in CI.
Neovim
Neovim has a built-in LSP client. With the nvim-lspconfig plugin, enabling Ruff takes a few lines of Lua:
require("lspconfig").ruff.setup({
init_options = {
settings = {
lineLength = 88,
},
},
})The init_options.settings table accepts the same names as the VS Code extension without the ruff. prefix. Ruff must be on your PATH. If you also run a type checker such as Pyright, you may see duplicate hover popups, because both servers answer. Ruff does not do type analysis, so let the type checker own hover:
vim.api.nvim_create_autocmd("LspAttach", {
callback = function(args)
local client = vim.lsp.get_client_by_id(args.data.client_id)
if client and client.name == "ruff" then
client.server_capabilities.hoverProvider = false
end
end,
})To format on save, call vim.lsp.buf.format() from a BufWritePre autocommand. Restrict it to the Ruff client with the name = "ruff" filter if other servers also offer formatting.
Helix and other editors
Any editor with an LSP client can run ruff server. In Helix, add the server to languages.toml:
[language-server.ruff]
command = "ruff"
args = ["server"]
[[language]]
name = "python"
language-servers = ["ruff"]
auto-format = trueThe command to run is ruff server and the transport is standard input and output, which is what most LSP clients assume. Zed, Sublime Text (through LSP-ruff), Emacs (through eglot or lsp-mode) and PyCharm (through its Ruff plugin or its external-tools support) follow the same pattern. Each has its own way of wiring "format on save" and "fix on save", but the server behind them is the same.
What you see in the editor
Diagnostics show the rule code, so you can look it up the same way as in the terminal, with ruff rule F401. Hovering over a # noqa: F401 comment shows the explanation for that code. The quick-fix menu usually offers these actions on a violation:
- Fix the violation, when Ruff has a fix for it.
- Disable for this line, which adds a targeted
# noqa: CODEcomment. - Fix all auto-fixable problems for the file.
- Organize imports, which sorts the import block.
Choose "Disable for this line" only when you have decided that the violation is acceptable. It adds the code to the comment, which is the targeted form from the suppression chapter. Avoid adding a bare # noqa by hand.
Common problems
Two formatters fight
If Black, autopep8 or a second Python formatter extension is also installed, saving the file may apply both. The result flips between two styles, or the editor asks which formatter to use. Set editor.defaultFormatter explicitly for [python], as shown above, and uninstall or disable the other formatter.
Fix on save removes imports you are about to use
With F401 enabled and fix-on-save active, a newly written import os disappears on save before you have typed the code that uses it. This is the safe fix doing its job. Workarounds:
- Write the code that uses the import first, then add the import.
- Add the import and use it in the same edit, for example by accepting an auto-import suggestion.
- Remove
source.fixAll.rufffrom on-save actions and run it on demand. - Use the
unfixable = ["F401"]option so the editor reports the violation but never removes the line.
[tool.ruff.lint]
unfixable = ["F401"]The editor shows nothing
Check the points below in order:
- Run
ruff check path/to/file.pyin the terminal. If it prints violations but the editor does not, the integration is the problem. - Open the editor's Ruff output or LSP log and look for startup errors, such as a bad
ruff.pathor an unsupported Ruff version. - Check that the file is inside the workspace, and that the file or a parent folder is not excluded by
excludeorextend-excludein your config. Excluded files are ignored by the server too. - Restart the language server after you change
pyproject.toml. Some editors pick up changes on their own, but not all of them do.
Different results from CI
When the editor and CI disagree, compare three things: the Ruff version, the configuration source, and the working directory. Pin Ruff in your dev dependencies, point the editor at that pinned executable, and use filesystemFirst. Then the editor and CI run the same code with the same settings.
Summary of the workflow
- Install Ruff in the project environment and pin its version.
- Install or configure the Ruff language server for your editor.
- Make Ruff the only Python formatter and enable format on save.
- Enable fix-all and organize-imports on save if you want them.
- Keep rule selection in
pyproject.tomlso the editor, pre-commit and CI agree.
Exercise
A teammate sees no lint errors in VS Code, but the CI job running ruff check . fails with B006 violations. The repository's pyproject.toml selects B. Their VS Code settings contain "ruff.lint.select": ["E", "F"]. Why does this happen, and what is the fix?
Show answer
The default ruff.configurationPreference is editorFirst, so the editor setting ruff.lint.select overrides the select list in pyproject.toml. The editor therefore never runs the B rules, while CI does. Fix it by removing ruff.lint.select from the VS Code settings (or setting "ruff.configurationPreference": "filesystemFirst"), so the repository configuration is used. Then restart the language server.
Exercise
You enable "source.fixAll.ruff": "explicit" and notice that an unused import is removed on save, but a violation whose fix is marked unsafe is left in place. Why, and how could you apply the unsafe fix?
Show answer
The fix-all action applies only safe fixes, the same as ruff check --fix. The unsafe fix can be applied from the quick-fix menu on that violation, or you can set unsafe-fixes = true under [tool.ruff] so that fixes marked unsafe are also applied, after which you should review the resulting diff.