Skip to content

Commit 204d844

Browse files
committed
internal/lsp: mitigate possiibility of a slow code action
Related to microsoft/vscode-go#3063. A small collection of fixes: (1) Don't reload packages with missing dependencies unless imports change. (2) Show the error message from the initial workspace load to the user. Also, a small fix for deletion suggested fixes - upgrading to the latest version of the VS Code language client revealed that I had made a mistake there. Change-Id: I7ab944ed27dc3da24a79d5d311531a1eb2b73102 Reviewed-on: https://go-review.googlesource.com/c/tools/+/221217 Run-TryBot: Rebecca Stambler <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent 2e887e3 commit 204d844

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

internal/lsp/cache/snapshot.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,13 @@ func (s *snapshot) PackageHandles(ctx context.Context, fh source.FileHandle) ([]
145145
// Reload package metadata if any of the metadata has missing
146146
// dependencies, in case something has changed since the last time we
147147
// reloaded it.
148-
if m := s.getMetadata(id); m == nil || len(m.missingDeps) > 0 {
148+
if m := s.getMetadata(id); m == nil {
149149
reload = true
150150
break
151151
}
152+
// TODO(golang/go#36918): Previously, we would reload any package with
153+
// missing dependencies. This is expensive and results in too many
154+
// calls to packages.Load. Determine what we should do instead.
152155
}
153156
if reload {
154157
if err := s.load(ctx, fileURI(fh.Identity().URI)); err != nil {

internal/lsp/diagnostics.go

+13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package lsp
66

77
import (
88
"context"
9+
"fmt"
910
"strings"
1011
"sync"
1112

@@ -86,6 +87,18 @@ func (s *Server) diagnose(ctx context.Context, snapshot source.Snapshot, alwaysA
8687
return nil
8788
}
8889
if err != nil {
90+
// If we encounter a genuine error when getting workspace packages,
91+
// notify the user.
92+
s.showedInitialErrorMu.Lock()
93+
if !s.showedInitialError {
94+
err := s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
95+
Type: protocol.Error,
96+
Message: fmt.Sprintf("Your workspace is misconfigured: %s. Please see https://github.com/golang/tools/blob/master/gopls/doc/troubleshooting.md for more information or file an issue (https://github.com/golang/go/issues/new) if you believe this is a mistake.", err.Error()),
97+
})
98+
s.showedInitialError = err == nil
99+
}
100+
s.showedInitialErrorMu.Unlock()
101+
89102
log.Error(ctx, "diagnose: no workspace packages", err, telemetry.Snapshot.Of(snapshot.ID()), telemetry.Directory.Of(snapshot.View().Folder))
90103
return nil
91104
}

internal/lsp/server.go

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ type Server struct {
5858
deliveredMu sync.Mutex
5959
delivered map[span.URI]sentDiagnostics
6060

61+
showedInitialError bool
62+
showedInitialErrorMu sync.Mutex
63+
6164
// diagnosticsSema limits the concurrency of diagnostics runs, which can be expensive.
6265
diagnosticsSema chan struct{}
6366
}

internal/lsp/source/diagnostics.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ func onlyDeletions(fixes []SuggestedFix) bool {
355355
}
356356
}
357357
}
358-
return true
358+
return len(fixes) > 0
359359
}
360360

361361
// hasUndeclaredErrors returns true if a package has a type error

0 commit comments

Comments
 (0)