Skip to content

lsp: compile tree-sitter before first completion - #314

Merged
vito merged 1 commit into
mainfrom
lsp-warm-tree-sitter
Nov 13, 2026
Merged

vito merged 1 commit into
mainfrom
lsp-warm-tree-sitter

Conversation

@vito

@vito vito commented Nov 13, 2026

Copy link
Copy Markdown
Owner

Fixes the TestLSP/TestNeovimCompletion flake that failed #313's CI twice (trace c728d6607bc56ea4ce96cf9e2bf7a8a1). The L006 ^ea<C-x><C-o> step sent textDocument/completion, but no response came back within its 1s window.

The cause was already on main. Since #307, completion classifies the cursor with tree-sitter compiled to WebAssembly. Its module is compiled on first use, which means the first completion request pays for it. Measured in-process on testdata/complete.dang:

first completion later ones
cursor classification ~58ms ~0.2ms
directory analysis ~68ms

In the failing CI run, analysis of the same file took ~650ms. #313 touches pkg/dang, so every package's tests reran at once and the runner was loaded. At that rate the cold compile is roughly 600ms, which together with Neovim's round trip ran past the 1s window. One more problem: the compile ran inside the parse's 1s treeSitterTimeout, so a slow enough compile could cancel the parse and silently drop completions or parse-error messages.

Changes:

  • tswasm.Load and dang.LoadTreeSitter compile the module ahead of first use. LoadTreeSitter does nothing in no-wasm builds.
  • The LSP starts that compile on initialize, in the background, so it runs alongside the first file's analysis. updateFile releases waiting readers only once the compile is done. Requests that wait on a file, and the test's hover readiness probe, therefore never wait on the compile. After the first time, this costs nothing.
  • inspectEditedTree and parseSyntaxTree compile the module before starting their timeout, so the timeout covers only the parse.

Tested: pkg/lsp (including TestLSP), pkg/dang, pkg/dang/analysis, pkg/dang/tswasm, cmd/dang and test/nocgo all pass.

Completion classifies the cursor with tree-sitter, whose WebAssembly
module was compiled on first use: by the first completion request. That
takes ~60ms idle (vs ~0.2ms for later classifications) and roughly ten
times that on a loaded CI runner, where TestNeovimCompletion's first
completion (`hel` → `hello`) then missed its 1s window. The compile also
ran inside the parse's 1s timeout, so a slow enough compile could cut the
parse short and drop completions (or parse error messages) entirely.

The server now starts compiling the module on initialize, alongside the
first file's analysis, and an update finishes only once it's compiled,
so requests waiting on the file never wait on the compile. Tree-sitter
parses compile the module, if nothing has yet, before starting their
timeout.

Signed-off-by: Alex Suraci <[email protected]>
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying dang with  Cloudflare Pages  Cloudflare Pages

Latest commit: 0987304
Status: ✅  Deploy successful!
Preview URL: https://edd4c538.dang-3kk.pages.dev
Branch Preview URL: https://lsp-warm-tree-sitter.dang-3kk.pages.dev

View logs

@vito
vito merged commit 57f499c into main Nov 13, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant