Skip to content

Clear stale LSP diagnostics when a file is closed or deleted#4514

Merged
stefanvanburen merged 2 commits intomainfrom
svanburen/fix-deleted-issue
Apr 27, 2026
Merged

Clear stale LSP diagnostics when a file is closed or deleted#4514
stefanvanburen merged 2 commits intomainfrom
svanburen/fix-deleted-issue

Conversation

@stefanvanburen
Copy link
Copy Markdown
Member

@stefanvanburen stefanvanburen commented Apr 27, 2026

When a file was closed or deleted, the LSP server never published empty diagnostics to clear the client's Problems panel.

Root cause: the workspace's indexFiles path calls fileManager.Track for every file in the module, so the ref count for an editor-open file is typically 2 (one from DidOpen, one from the workspace). When DidClose decremented the count to 1, Reset was never called and diagnostics were never cleared.

Fix by making file.Close call clearEditorState before decrementing the ref count. clearEditorState cancels in-flight lint checks, publishes empty diagnostics (only when non-empty, to skip the no-op network write), and sets version=-1 so subsequent RunChecks won't re-publish lint diagnostics.

Also implement DidDeleteFiles (workspace/didDeleteFiles), previously a NYI stub, so files deleted from disk via the OS or IDE file explorer are handled the same way.

Also extract the "buf lint" diagnostic source string into a named constant lintSourceName.

Fixes bufbuild/vscode-buf#626.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 27, 2026

The latest Buf updates on your PR. Results from workflow Buf CI / buf (pull_request).

BuildFormatLintBreakingUpdated (UTC)
✅ passed✅ passed✅ passed✅ passedApr 27, 2026, 8:17 PM

@stefanvanburen stefanvanburen force-pushed the svanburen/fix-deleted-issue branch 3 times, most recently from 3a319f6 to 2504d03 Compare April 27, 2026 20:13
@stefanvanburen stefanvanburen changed the title Clear stale LSP diagnostics when a file is closed Clear stale LSP diagnostics when a file is closed or deleted Apr 27, 2026
When the user closed a file, the LSP server never published empty
diagnostics to clear the Problems panel. The root cause is that the
workspace's indexFiles path calls fileManager.Track for every file in
the module, incrementing the reference count beyond the editor's own
reference. When DidClose decrements the count, it rarely drops to zero,
so fileManager.Close never calls Reset and diagnostics were never cleared.

Fix this by handling the clear explicitly in DidClose: cancel in-flight
checks, publish empty diagnostics (while IsOpenInEditor is still true),
then set version=-1 to mark the file as no longer open in the editor so
subsequent RunChecks skip re-publishing lint diagnostics.

Also update Reset to be defensive: publish empty diagnostics if
IsOpenInEditor is still true at Reset time (e.g. for workspace-external
files whose ref count drops to zero without going through DidClose).

Extract the "buf lint" diagnostic source string into a named constant
lintSourceName alongside the existing serverName constant.

Add a regression test that opens a file with a known lint violation,
waits for diagnostics, sends DidClose, then asserts the Problems panel
is cleared.

Fixes bufbuild/vscode-buf#626.
@stefanvanburen stefanvanburen marked this pull request as ready for review April 27, 2026 20:15
@stefanvanburen stefanvanburen force-pushed the svanburen/fix-deleted-issue branch from 2504d03 to 386c0e1 Compare April 27, 2026 20:16
Comment thread CHANGELOG.md Outdated
@stefanvanburen stefanvanburen merged commit 677b6ae into main Apr 27, 2026
10 checks passed
@stefanvanburen stefanvanburen deleted the svanburen/fix-deleted-issue branch April 27, 2026 21:03
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.

Deleted file linting issues persist as problems in VSC after file deletion

3 participants