Are you tired of rust-analyzer diagnostics being slow?
LSP Server wrapper for the exceptional Bacon exposing textDocument/diagnostic and workspace/diagnostic capabilities.
bacon-ls π½ does not substitute rust-analyzer, it's a companion tool that can help with large
codebases where rust-analyzer can become slow dealing with diagnostics.
bacon-ls π½ does not help with completion, analysis, refactor, etc... For these, rust-analyzer must be running.
- Features
- Installation
- Configuration
- Migrating from 0.26.x and earlier
- Editor setup
- Troubleshooting
- How does it work?
- Thanks
- Roadmap to 1.0 - β done π in progress π future
See bacon-ls π½ blog post: https://lmno.lol/crisidev/bacon-language-server
bacon-ls π½ is meant to be easy to include in your IDE configuration.
- Two backends to produce diagnostics:
- Cargo (default since 0.23.0): runs
cargo check(orcargo clippy) directly with JSON output, parses the messages and publishes them. Faster, lighter and zero extra dependencies. - Bacon: reads the export file produced by Bacon
and publishes those diagnostics. Useful when you already have
baconrunning.
- Cargo (default since 0.23.0): runs
- Push diagnostics to the LSP client on file save, open, close and rename.
- Precise diagnostic positions and macro-expanded spans pointed back at the call-site.
- Replacement code actions as suggested by
cargo/clippy. - Streaming partial publishes during a long
cargorun (configurable refresh interval) so the editor lights up as soon as the first errors are known. - Manual
bacon_ls.runLSP command to re-trigger a check on demand. - Bacon backend extras: automatic validation of
baconpreferences, optional creation of the preferences file, optional automatic backgroundbaconprocess (requiresbacon3.8.0), open-file diagnostic synchronization. - Support for cargo workspaces.
- Windows support is not tested and probably broken - #10
First, install Bacon.
The VSCode extension is available on both VSCE and OVSX:
VSCEhttps://marketplace.visualstudio.com/items?itemName=MatteoBigoi.bacon-ls-vscodeOVSXhttps://open-vsx.org/extension/MatteoBigoi/bacon-ls-vscode
Both Bacon and Bacon-ls are installable via mason.nvim:
:MasonInstall bacon bacon-lsFirst, install Bacon and bacon-ls π½
β―β―β― cargo install --locked bacon bacon-ls
β―β―β― bacon --version
bacon 3.8.0 # make sure you have at least 3.8.0
β―β―β― bacon-ls --version
0.14.0 # make sure you have at least 0.14.0Both bacon and bacon-ls can be consumed from their Nix flakes.
bacon-ls π½ reads its configuration from the bacon_ls section of the LSP
client settings. All fields are optional β if you provide nothing the cargo
backend starts with sensible defaults. The complete schema is:
The backend is chosen once, when the server initializes, and cannot be switched at runtime (you have to restart the server). The choice is resolved as follows:
- If
bacon_ls.backendis set to"cargo"or"bacon", that wins. - Otherwise, if only one of
bacon_ls.cargoorbacon_ls.baconis present in the settings, that backend is selected. - Otherwise (both sections present without an explicit
backend, or no settings at all), the default is cargo.
Providing both cargo and bacon sections without an explicit backend
key is reported as a configuration error.
Available since bacon-ls 0.23.0, default since 0.26.0. Runs cargo directly with
--message-format=json-diagnostic-rendered-ansi, parses the stream and publishes
diagnostics β no bacon process required.
command(default"check"): which cargo subcommand to run. Most useful values are"check"and"clippy".features: list of features passed as--features a,b,c.package: when set, passed as-p <package>(useful in workspaces).extraArgs: appended verbatim after the subcommand. Use this for e.g.["--workspace", "--all-targets", "--all-features"].env: map of additional environment variables for the cargo invocation.cancelRunning(defaulttrue): when a new run is requested while another is still running, cancel the in-flight one. Set tofalseto instead queue at most one follow-up run after the current one completes.refreshIntervalSeconds(default5): how often to publish a partial snapshot of the diagnostics gathered so far while cargo is still running. Set tonullor a negative number to only publish once cargo has finished.separateChildDiagnostics(defaultnull): cargo emits some hints as children of a parent diagnostic. Whennullwe follow the client'srelatedInformationcapability; set totrueto always emit children as standalone diagnostics,falseto always nest them.checkOnSave(defaulttrue): trigger a cargo run ontextDocument/didSave. Set tofalseif you only want to drive runs manually viabacon_ls.run.clearDiagnosticsOnCheck(defaultfalse): publish empty diagnostics for all files that previously had any before starting the new run. Useful if you want the editor's diagnostic counters to drop to zero immediately at the start of a check.
Reads diagnostics from the file produced by Bacon's export-locations feature.
Configure Bacon with the bacon-ls π½ export format in the bacon preference
file (bacon --prefs shows where it lives):
[jobs.bacon-ls]
command = [
"cargo", "clippy",
"--workspace", "--all-targets", "--all-features",
"--message-format", "json-diagnostic-rendered-ansi",
]
analyzer = "cargo_json"
need_stdout = true
[exports.cargo-json-spans]
auto = true
exporter = "analyzer"
line_format = """\
{diagnostic.level}|:|{span.file_name}|:|{span.line_start}|:|{span.line_end}|:|\
{span.column_start}|:|{span.column_end}|:|{diagnostic.message}|:|{diagnostic.rendered}|:|\
{span.suggested_replacement}\
"""
path = ".bacon-locations"bacon itself must be running to keep the export file fresh
(bacon -j bacon-ls). When runInBackground is true (the default since
0.10.0), bacon-ls starts and supervises it for you.
locationsFile(default".bacon-locations"): bacon export file to read.runInBackground(defaulttrue): startbaconautomatically and tear it down on shutdown.runInBackgroundCommand(default"bacon"): command to spawn. Override ifbaconis not in$PATH.runInBackgroundCommandArguments(default"--headless -j bacon-ls"): command-line arguments passed to the backgroundbaconprocess.validatePreferences(defaulttrue): verify the bacon preferences file contains a workingbacon-lsjob and matching export configuration. Errors are surfaced to the LSP client.createPreferencesFile(defaulttrue): if validation fails because the preferences file is missing, generate one with thebacon-lsjob and export defined.synchronizeAllOpenFilesWaitMillis(default2000): how often the background loop re-publishes diagnostics for every open file (so a fix in file A also clears the now-stale error in file B).updateOnSave(defaulttrue): re-publish diagnostics ontextDocument/didSave.updateOnSaveWaitMillis(default1000): delay before reading the locations file after a save, to give bacon time to finish its run.
bacon-ls π½ registers a single workspace/executeCommand named bacon_ls.run.
Invoking it triggers an immediate cargo run when the cargo backend is active
(the bacon backend ignores it β there is nothing for it to drive directly).
This is how clients can offer a "run check now" command without relying on save events. Example from a Neovim mapping:
vim.keymap.set("n", "<leader>cb", function()
vim.lsp.buf.execute_command({ command = "bacon_ls.run" })
end, { desc = "bacon-ls: run check" })bacon-ls honours workspace/didChangeConfiguration and re-reads its settings,
but with one important constraint: the backend choice is fixed for the
lifetime of the process. Trying to switch from cargo to bacon (or vice
versa) without restarting the server is reported as an error to the client and
ignored. All other options (cargo command, features, bacon update interval, β¦)
can be changed live.
PR #113 reorganised the configuration into per-backend sections. If you were on 0.26.x or earlier, the following changes apply:
useBaconBackendis gone. Replace it with either the explicit"backend": "bacon"or simply by providing a"bacon": { ... }section.- All
runBaconInBackground*,validateBaconPreferences,createBaconPreferencesFile,synchronizeAllOpenFilesWaitMillis,updateOnSave,updateOnSaveWaitMillisandlocationsFilekeys have moved insidebacon_ls.bacon.*and dropped theBaconprefix where it was redundant (e.g.runBaconInBackgroundβbacon.runInBackground,validateBaconPreferencesβbacon.validatePreferences). - All cargo-related keys live under
bacon_ls.cargo.*. - The backend can no longer be changed live β restart the server to switch.
Old config:
{
"bacon_ls": {
"useBaconBackend": true,
"runBaconInBackground": true,
"validateBaconPreferences": true,
"updateOnSave": true
}
}New equivalent:
{
"bacon_ls": {
"backend": "bacon",
"bacon": {
"runInBackground": true,
"validatePreferences": true,
"updateOnSave": true
}
}
}vim.g.lazyvim_rust_diagnostics = "bacon-ls"NeoVim requires nvim-lspconfig to be configured
and rust-analyzer diagnostics must be turned off for bacon-ls π½
to properly function.
bacon-ls is part of nvim-lspconfig from commit
6d2ae9f
and it can be configured like any other LSP server works best when
vim.diagnostics.opts.update_in_insert
is set to true.
vim.lsp.config('bacon-ls', {
settings = {
bacon_ls = {
backend = "cargo",
cargo = {
command = "clippy",
checkOnSave = true,
},
},
},
})Settings can also be passed via init_options as the same bacon_ls = { ... }
table β the server reads from both sources.
When using codesettings to manage project local settings
vim.lsp.config("*", {
before_init = function(_, config)
local codesettings = require("codesettings")
if config.name == "bacon_ls" then
local settings = codesettings.local_settings()["_settings"]["bacon_ls"]
if settings ~= nil then
config["settings"]["bacon_ls"] = settings
vim.print(config["settings"]["bacon_ls"])
end
return config
end
return codesettings.with_local_settings(config.name, config)
end,
})
For rust-analyzer, these 2 options must be turned off:
rust-analyzer.checkOnSave.enable = false
rust-analyzer.diagnostics.enable = falseThe extension can be configured using the VSCode settings interface.
It is very important that rust-analyzer Check On Save and Diagnostics are turned off for bacon-ls to work properly:
- Untick
Rust-analyzer -> general -> Check On Save - Untick
Rust-analyzer -> diagnostics -> Enable
call coc#config('languageserver', {
\ 'bacon-ls': {
\ 'command': '~/.cargo/bin/bacon-ls',
\ 'filetypes': ['rust'],
\ 'rootPatterns': ['.git/', 'Cargo.lock', 'Cargo.toml'],
\ 'settings': {
\ 'cargo': {
\ 'cancelRunning': true,
\ }
\ }
\ }
\ }
\ })Extend your languages.toml with the following:
[[language]]
name = "rust"
language-servers = ["rust-analyzer", "bacon-ls"]
[language-server.rust-analyzer.config]
checkOnSave = { enable = false }
diagnostics = { enable = false }
[language-server.bacon-ls]
command = "bacon-ls"
[language-server.bacon-ls.config.bacon_ls]
backend = "cargo"
[language-server.bacon-ls.config.bacon_ls.cargo]
command = "clippy"bacon-ls π½ can produce a log file in the folder where its running by exporting the RUST_LOG variable in the shell:
If the bacon preference are not correct, an error message will be published to the LSP client, advising the user to
check the README.
β―β―β― export RUST_LOG=debug
β―β―β― nvim src/some-file.rs # or vim src/some-file.rs
# the variable can also be exported for the current command and not for the whole shell
β―β―β― RUST_LOG=debug nvim src/some-file.rs # or RUST_LOG=debug vim src/some-file.rs
β―β―β― tail -F ./bacon-ls.logEnable debug logging in the extension options.
β―β―β― tail -F ./bacon-ls.logbacon-ls π½ speaks LSP over STDIO and publishes diagnostics to the client via
textDocument/publishDiagnostics. How those diagnostics are produced depends on
the active backend.
Cargo backend (default). On each trigger (initial start, file save, or a
manual bacon_ls.run), bacon-ls runs cargo check (or cargo clippy) with
--message-format=json-diagnostic-rendered-ansi from the project root. The JSON
stream is parsed as it arrives, spans from macro expansions are walked back to
the original call site, and diagnostics are published per file. With
refreshIntervalSeconds set, partial snapshots are pushed while cargo is still
running so the editor shows errors as soon as they are known. The previous run
is cancelled when a newer one starts (or queued, depending on cancelRunning).
Bacon backend. Bacon runs in a watch loop and
writes diagnostics to its export file (default .bacon-locations) using a
custom line_format. bacon-ls reads that file on save / open / close / rename
events and on a periodic open-file synchronization tick, parses the lines, and
publishes the resulting diagnostics. When runInBackground is on, bacon-ls
also spawns and supervises the bacon process itself.
Both backends share the same code-actions pipeline: when a diagnostic carries a
suggested replacement, it is exposed as a quickfix code action via
textDocument/codeAction.
bacon-ls π½ has been inspired by typos-lsp.
- β
Implement LSP server interface for
textDocument/diagnosticandworkspace/diagnostic - β Manual Neovim configuration
- β Manual LazyVim configuration
- β
Automatic NeoVim configuration
- β
Add
bacon-lsto nvim-lspconfig - neovim/nvim-lspconfig#3160 - β
Add
baconandbacon-lsto mason.nvim - mason-org/mason-registry#5774 - β
Add
bacon-lsto LazyVim Rust extras - LazyVim/LazyVim#3212
- β
Add
- β Add compiler hints to Bacon export locations - Canop/bacon#187 Canop/bacon#188
- β
Support correct span in Bacon export locations - working from
bacon3.7 andbacon-ls0.6.0 - β VSCode extension and configuration - available on the release page from 0.6.0
- β VSCode extension published available on Marketplace
- β
Add
bacon-lstobaconwebsite - Canop/bacon#289 - β Smarter handling of parsing the Bacon locations file
- β Faster response after a save event
- β Replacement code actions
- β
Validate
baconpreferences and return an error to the LSP client if they are not compatible withbacon- working frombacon-ls0.9.0 - β
Create
baconpreferences file if not found on disk - working frombacon-ls0.10.0 - β
Start
baconin background based on user preferences - working frombacon-ls0.10.0 - β
Synchronize diagnostics for all open files - working from
bacon-ls0.10.0 - β
Support Helix editor - working from
bacon-ls0.12.0 - β Nix flake support
- β
Support cargo workspaces - working from
bacon-ls0.14.0 - β
Faster native cargo backend - default from
bacon-ls0.23.0 - π Emacs configuration


{ "bacon_ls": { // "cargo" or "bacon". Optional β see "Choosing a backend" below. "backend": "cargo", "cargo": { "command": "check", // "check" or "clippy" "features": [], // cargo --features list "package": null, // cargo -p <package> "extraArgs": [], // appended verbatim after the cargo command "env": {}, // extra environment variables (string -> string) "cancelRunning": true, // cancel an in-flight run when a new one is triggered "refreshIntervalSeconds": 5, // partial publish interval; null/negative = wait until done "separateChildDiagnostics": null, // override "related information" support; null = follow client "checkOnSave": true, // trigger cargo on textDocument/didSave "clearDiagnosticsOnCheck": false // clear existing diagnostics before each run }, "bacon": { "locationsFile": ".bacon-locations", "runInBackground": true, "runInBackgroundCommand": "bacon", "runInBackgroundCommandArguments": "--headless -j bacon-ls", "validatePreferences": true, "createPreferencesFile": true, "synchronizeAllOpenFilesWaitMillis": 2000, "updateOnSave": true, "updateOnSaveWaitMillis": 1000 } } }