Skip to content

gopls/internal/server: add diagnosticsScope setting - #707

Open
rafiss wants to merge 1 commit into
golang:masterfrom
cockroachdb:upstream-diagnostics-scope
Open

rafiss wants to merge 1 commit into
golang:masterfrom
cockroachdb:upstream-diagnostics-scope

Conversation

@rafiss

@rafiss rafiss commented Nov 4, 2026 •

Copy link
Copy Markdown

By default, gopls type-checks every workspace package to report its
compilation errors, and it publishes diagnostics for files that are
not open. In a workspace with thousands of packages, this takes
minutes of CPU and gigabytes of memory after gopls starts. Some
clients also have no use for these diagnostics. For example, the LSP
client of an AI coding agent can add each published diagnostic to the
model's context, including diagnostics for files that the agent never
opened.

Add an experimental "diagnosticsScope" setting. The default,
"workspace", keeps the current behavior. With "openPackages", the
diagnose function type-checks only the workspace packages that
contain an open file. Analysis already uses this set of packages.

The final updateDiagnostics call publishes empty diagnostics for each
file that is not in the result. Thus gopls clears the diagnostics of
a package when its last open file closes or when the setting changes.
diagnoseChangedFiles considers only open files, so it does not change.

With "openPackages", gopls does not report errors in a package that
an edit breaks until a file of that package is open. Also, the first
references or implementation query must type-check the reverse
dependencies. With "workspace", the diagnostics pass type-checks them
in advance.

I measured the effect on a CockroachDB checkout that has about 2,500
workspace packages and 11,800 packages in total. The checkout has no
generated code, so the workspace has about 114,000 compiler errors.
The client opens one file, waits for its diagnostics, adds a type
error, removes it, and then waits 60s. Each cell shows two runs.

                          workspace        openPackages
  cold cache
    peak RSS              11.4, 11.8 GB    3.3, 3.4 GB
    CPU                   205, 253 s       38, 39 s
  warm cache
    peak RSS              5.4, 1.9 GB      1.2, 1.4 GB
    CPU                   54, 33 s         21, 21 s
  files with diagnostics  4,760            12
  diagnostics             114,530          99

In one run with a 240s wait after the edit, the cold "workspace" run
used 263s of CPU and the cold "openPackages" run used 38s.

For both values, the time from didOpen to the first diagnostics for
the open file was 13-26s with a cold cache and 11-16s with a warm
cache. After an edit, diagnostics arrived in 30ms or less.

I wrote this change with Claude Code.

Updates golang/go#69523

By default, gopls type-checks every workspace package to report its
compilation errors, and it publishes diagnostics for files that are
not open. In a workspace with thousands of packages, this takes
minutes of CPU and gigabytes of memory after gopls starts. Some
clients also have no use for these diagnostics. For example, the LSP
client of an AI coding agent can add each published diagnostic to the
model's context, including diagnostics for files that the agent never
opened.

Add an experimental "diagnosticsScope" setting. The default,
"workspace", keeps the current behavior. With "openPackages", the
diagnose function type-checks only the workspace packages that
contain an open file. Analysis already uses this set of packages.

The final updateDiagnostics call publishes empty diagnostics for each
file that is not in the result. Thus gopls clears the diagnostics of
a package when its last open file closes or when the setting changes.
diagnoseChangedFiles considers only open files, so it does not change.

With "openPackages", gopls does not report errors in a package that
an edit breaks until a file of that package is open. Also, the first
references or implementation query must type-check the reverse
dependencies. With "workspace", the diagnostics pass type-checks them
in advance.

I measured the effect on a CockroachDB checkout that has about 2,500
workspace packages and 11,800 packages in total. The checkout has no
generated code, so the workspace has about 114,000 compiler errors.
The client opens one file, waits for its diagnostics, adds a type
error, removes it, and then waits 60s. Each cell shows two runs.

                          workspace        openPackages
  cold cache
    peak RSS              11.4, 11.8 GB    3.3, 3.4 GB
    CPU                   205, 253 s       38, 39 s
  warm cache
    peak RSS              5.4, 1.9 GB      1.2, 1.4 GB
    CPU                   54, 33 s         21, 21 s
  files with diagnostics  4,760            12
  diagnostics             114,530          99

In one run with a 240s wait after the edit, the cold "workspace" run
used 263s of CPU and the cold "openPackages" run used 38s.

For both values, the time from didOpen to the first diagnostics for
the open file was 13-26s with a cold cache and 11-16s with a warm
cache. After an edit, diagnostics arrived in 30ms or less.

I wrote this change with Claude Code.

Updates golang/go#69523
@google-cla

google-cla Bot commented Nov 4, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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