Skip to content

fix: prevent unnecessary diagnostic refreshes on irrelevant watch events (#4589)#4604

Open
ibesuperv wants to merge 10 commits into
microsoft:mainfrom
ibesuperv:fix-4589
Open

fix: prevent unnecessary diagnostic refreshes on irrelevant watch events (#4589)#4604
ibesuperv wants to merge 10 commits into
microsoft:mainfrom
ibesuperv:fix-4589

Conversation

@ibesuperv

Copy link
Copy Markdown

Fixes #4589

This PR optimizes Session.DidChangeWatchedFiles to only trigger the expensive workspace/diagnostic/refresh RPC round-trip when the watch events contain files that could actually affect the TypeScript program.

Changes

  • Gated Refresh: ScheduleDiagnosticsRefresh() is now only invoked if the batch contains files with relevant extensions (using the canonical extension list from isRelevantFileName), or directories (files with no extension).
  • Invariant Preservation: Cache invalidation functions (cancelWarmAutoImportCache, scheduleIdleCacheClean) continue to execute unconditionally on all watch events, ensuring that package installs (which generate noise like .svg and .md) still correctly invalidate stale cache state.
  • Testing: Added a comprehensive test suite in TestSession/DidChangeWatchedFiles/skips_irrelevant_extensions to verify that diagnostic refreshes correctly trigger (or skip) for .ts, tsconfig.json, directories, and irrelevant files.

Why this matters

Currently, when a user installs a package (e.g., via npm install), the LSP receives hundreds of watch events for irrelevant files like .svg, .png, and .md. Previously, this unconditionally triggered ScheduleDiagnosticsRefresh(), causing a CPU and RPC spike on the VS Code client. This patch filters the noise at the transport layer, eliminating the spurious refreshes while maintaining correctness.

@ibesuperv
ibesuperv marked this pull request as ready for review July 11, 2026 08:37
Copilot AI review requested due to automatic review settings July 11, 2026 08:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes the language server’s Session.DidChangeWatchedFiles path by gating workspace/diagnostic/refresh so it only runs when a watch batch includes changes that could affect the TypeScript program, reducing client RPC/CPU spikes during noisy filesystem activity (e.g. npm install).

Changes:

  • Added relevance detection in DidChangeWatchedFiles to conditionally schedule ScheduleDiagnosticsRefresh().
  • Preserved unconditional cache invalidation behavior on all watch events.
  • Added a Go regression test sub-suite to verify refresh behavior for relevant vs irrelevant watch events.

Reviewed changes

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

File Description
internal/project/session.go Adds relevance gating for ScheduleDiagnosticsRefresh() based on watched file URI extensions / extensionless paths.
internal/project/session_test.go Adds a new DidChangeWatchedFiles test sub-suite covering refresh-triggering behavior across several event scenarios.

Comment thread internal/project/session.go
Comment thread internal/project/session_test.go
Comment thread internal/project/session_test.go

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

Comment thread internal/project/session.go Outdated
Comment thread internal/project/session_test.go
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

Comment thread internal/project/session.go
Comment thread internal/project/session_test.go
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

Comment thread internal/project/session_test.go
Comment thread internal/project/session_test.go
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

Comment thread internal/project/session.go
Comment thread internal/project/session_test.go Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread internal/project/session.go Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

Comment thread internal/project/session.go Outdated
Comment thread internal/project/session.go Outdated
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.

workspace/diagnostic/refresh is triggered for watch events that cannot affect type checking

2 participants