Rust Analyzer very slow to format in VSCode

Rust analyzer is taking several seconds (or longer) to format files. cargo fmt can format the entire project in <50ms, and the Rust extension (RLS) is likewise near-instant

My current vscode settings in their entirety in case any of it is relevant (though this occurs with all rust-analyzer settings removed except LinkedProjects):

{
    "files.watcherExclude": {
        "**/target/**": true
    },
    "files.insertFinalNewline": true,
    "files.trimFinalNewlines": true,
    "editor.formatOnSave": true,
    "rust-analyzer.linkedProjects": [
        "lib/Cargo.toml",
        "client/Cargo.toml",
        "server/Cargo.toml"
    ],
    "rust-analyzer.updates.askBeforeDownload": false,
    "rust-analyzer.updates.channel": "nightly",
    "rust-analyzer.diagnostics.disabled": [
        "missing-unsafe"
    ],
    "rust-analyzer.checkOnSave.enable": false,
    "rust-analyzer.assist.importPrefix": "by_crate",
    "rust-analyzer.trace.server": "verbose",
    "rust-analyzer.trace.extension": true
}

status after saving 1 file:

workspaces:
40 packages loaded
235 packages loaded
137 packages loaded

analysis:
6354 (60mb) files
0 (0b) index symbols
4096 trees, 128 retained
55226 trees, 128 retained (macros)
1243mb total

counts:
all counts are zero


requests:
  103 textDocument/formatting             4057ms
  111 rust-analyzer/inlayHints            16ms
  112 textDocument/semanticTokens/full/delta10ms
  113 textDocument/foldingRange           1ms
  115 textDocument/codeAction             0ms
  117 textDocument/codeLens               0ms
  118 codeLens/resolve                    0ms
  119 textDocument/documentSymbol         1ms
* 106 textDocument/documentSymbol         4ms
  110 codeLens/resolve                    4ms

RALS trace output (what I hope is the relevant portion. this is followed by ~20k lines of other stuff that doesn't appear directly relevant):

[Trace - 3:33:54 PM] Sending request 'textDocument/formatting - (478)'.
Params: {
    "textDocument": {
        "uri": "file:///path/to/graphics.rs"
    },
    "options": {
        "tabSize": 4,
        "insertSpaces": true,
        "trimFinalNewlines": true,
        "insertFinalNewline": true
    }
}

[Trace - 3:33:59 PM] Received response 'textDocument/formatting - (478)' in 4780ms.
Result: [
    {
        "range": {
            "start": {
                "line": 33,
                "character": 0
            },
            "end": {
                "line": 33,
                "character": 1
            }
        },
        "newText": ""
    }
]

There is no RA client output.

There don't seem to be any errors in any log anywhere, it just takes 3-15 seconds to format a file.

I would just use RLS, but RA has better auto-completion, so I'd love any advice on how to figure out what the problem is.

Edit: I tested on a completely fresh (cargo new) project (with no settings except format on save) and it was faster (~1s on the default main.rs, 1-2 seconds on some examples I pasted from rust by example) but still dramatically slower than the Rust extension or cargo fmt.

Most of the time it is fast for me (i9 CPU), but very seldom it gets really slow.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.