Skip to content

Fix stale pull diagnostics after document edits#4074

Closed
Kelpy2004 wants to merge 2 commits into
microsoft:mainfrom
Kelpy2004:fix/stale-native-diagnostics
Closed

Fix stale pull diagnostics after document edits#4074
Kelpy2004 wants to merge 2 commits into
microsoft:mainfrom
Kelpy2004:fix/stale-native-diagnostics

Conversation

@Kelpy2004
Copy link
Copy Markdown

Addresses microsoft/vscode#317522

Summary

This prevents stale textDocument/diagnostic responses from being applied after a document has changed.

Diagnostic requests now keep the request-time snapshot, compute diagnostics against it, and then verify that the snapshot still matches the session's current document/default-project state before sending the response. If the document or project has moved on, the server returns ContentModified so the client can retry with fresh state instead of showing outdated errors.

Changes

  • Added a diagnostic-specific request handler that preserves the request snapshot.
  • Added a session freshness check for request-time document snapshots.
  • Added regression coverage for a diagnostic request invalidated by a later edit.

Validation

  • go test ./internal/lsp -run TestDocumentDiagnosticCancelsStaleSnapshot -v -count=1 -timeout=45s
  • go test ./internal/lsp -count=1 -timeout=5m
  • go test ./internal/project -run TestSession -count=1 -timeout=2m
  • npx hereby check:format
  • npx hereby lint
  • npx hereby build
  • npx hereby test

Note: hereby test passed with the repo warning that the TypeScript submodule is not cloned, so submodule-dependent tests may be skipped.

Disclosure

AI assistance was used to prepare this patch; I reviewed, tested, and take responsibility for the changes.

@Kelpy2004 Kelpy2004 marked this pull request as ready for review May 29, 2026 10:12
Copilot AI review requested due to automatic review settings May 29, 2026 10:12
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds a stale-snapshot guard for document diagnostics requests so that responses computed against an out-of-date snapshot are rejected with ContentModified, allowing the client to re-request fresh diagnostics.

Changes:

  • Introduces Session.IsSnapshotCurrentForDocument to compare a request-time snapshot with the session's current snapshot for a document.
  • Adds a dedicated registerDocumentDiagnosticHandler that captures the snapshot at request time and returns ContentModified if it becomes stale.
  • Adds a test verifying that stale diagnostic responses are rejected after the underlying file changes.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
internal/project/session.go Adds IsSnapshotCurrentForDocument and sameFileHandle helpers to detect snapshot staleness.
internal/lsp/server.go Replaces generic diagnostic handler registration with a custom one that checks snapshot freshness before sending the result.
internal/lsp/server_diagnostics_test.go New test verifying diagnostic requests against stale snapshots return ContentModified.

Comment thread internal/lsp/server.go Outdated
Comment thread internal/project/session.go
Comment thread internal/lsp/server.go
@Kelpy2004
Copy link
Copy Markdown
Author

Hi @jakebailey, would you be the right person to take a look at this? This follows up on microsoft/vscode#317522 and touches LSP pull diagnostics / snapshot freshness. Happy to adjust the approach if there’s a better place to handle it.

@jakebailey
Copy link
Copy Markdown
Member

I'm not sure why this is required? Pull diags are requested at a specific point in time, and the answer at that point is valid.

If there's a stale bug here, surely it's an LSP client bug.

@jakebailey
Copy link
Copy Markdown
Member

The comments on the linked issue also mentions the client so I'm not sure where this comes from

@Kelpy2004 Kelpy2004 closed this May 29, 2026
@Kelpy2004 Kelpy2004 deleted the fix/stale-native-diagnostics branch May 29, 2026 16:59
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.

3 participants