Skip to content

fix(lsp): discard stale diagnostics for outdated document versions#760

Merged
psteinroe merged 1 commit into
supabase-community:mainfrom
mvanhorn:fix/744-discard-stale-lsp-diagnostics
Jun 18, 2026
Merged

fix(lsp): discard stale diagnostics for outdated document versions#760
psteinroe merged 1 commit into
supabase-community:mainfrom
mvanhorn:fix/744-discard-stale-lsp-diagnostics

Conversation

@mvanhorn

Copy link
Copy Markdown
Contributor

Summary

Discard LSP diagnostics that were computed for an outdated document version, so stale results are never published over a newer (or freshly reopened) document.

Session::update_diagnostics awaits the DB-backed pull_file_diagnostics. During rapid edits to a large SQL file, each textDocument/didChange runs this synchronously, and responses for older document versions arrive after newer edits have already been applied. Previously those stale results were still published, causing diagnostics computed against superseded snapshots to overwrite newer ones.

This change captures the document version before computing diagnostics, re-reads the current version after the await point, and skips publishing when the versions no longer match. Any version mismatch is treated as stale (not just a strictly newer version), which also covers the close/reopen case where a client resets the document version to a lower number.

Why this matters

Refs #744. The issue reports that editing large SQL files triggers a backlog of sequential analysis requests whose stale responses keep getting published after the user stops typing. Discarding results for non-current document versions is one of the fixes the reporter asked for ("Track document version and discard results for non-current versions"), and it removes the visible flicker of stale diagnostics with a minimal, low-risk change. The Document struct already carries version, so no schema change is needed.

This is a partial fix: debounce, in-flight cancellation tokens, and per-document queue-depth coalescing would reduce the redundant analysis work itself, but they require a dedicated server-side scheduler and are left as a separate, larger change. The version guard is independent and useful on its own.

Testing

  • Added a unit test for the version-comparison decision (session::tests::stale_diagnostics_are_detected_by_version), covering: a newer version arriving (stale), the same version (publish), and a reopened document reset to a lower version (stale). This test has no database dependency.
  • cargo check -p pgls_lsp, cargo fmt -p pgls_lsp --check, and cargo clippy -p pgls_lsp all pass.
  • The existing pgls_lsp integration tests are #[sqlx::test] and require a live Postgres test database; they were not run as part of this change (no behavioral change to those paths beyond the version guard).

When editing large SQL files, rapid didChange notifications each run
Session::update_diagnostics, which awaits the DB-backed
pull_file_diagnostics. Results for older document versions arrive after
newer edits and were still published, causing stale diagnostics to
overwrite newer ones.

Capture the document version before computing diagnostics and re-read the
current version after the await point. If a newer version has arrived,
skip publishing so superseded results are discarded. Refs supabase-community#744.

@psteinroe psteinroe left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for taking a look! I like the simplicity of it. I was planning to add a more sophisticated debounce layer but this is might even be sufficient already. Let's try!

@psteinroe psteinroe merged commit 4b58d7a into supabase-community:main Jun 18, 2026
9 checks passed
@mvanhorn

Copy link
Copy Markdown
Contributor Author

Appreciate you merging this, @psteinroe. Discarding diagnostics computed for an outdated document version should stop the stale results.

@mvanhorn

Copy link
Copy Markdown
Contributor Author

Thanks @psteinroe - discarding diagnostics computed for an outdated document version should stop stale LSP results from clobbering newer ones.

@psteinroe

Copy link
Copy Markdown
Collaborator

Thanks @psteinroe - discarding diagnostics computed for an outdated document version should stop stale LSP results from clobbering newer ones.

thanks for taking care! pushing a new release now :)

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.

2 participants