Skip to content

Commit ae45d7c

Browse files
committed
internal/lsp: avoid diagnosing unopened non-workspace packages
This fixes an issue where we were reloading packages in the vendor directory when they changed on-disk. We should only do this if the packages are part of the workspace or the files are opened. Change-Id: Iefbd690ec0c096d9a40c62ce567c18335024ea15 Reviewed-on: https://go-review.googlesource.com/c/tools/+/268038 Trust: Rebecca Stambler <[email protected]> Run-TryBot: Rebecca Stambler <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Findley <[email protected]> (cherry picked from commit 169ad6d) Reviewed-on: https://go-review.googlesource.com/c/tools/+/268657
1 parent ced7ecb commit ae45d7c

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

internal/lsp/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func (s *Server) runCommand(ctx context.Context, work *workDone, command *source
235235
}
236236
snapshot, release := sv.Snapshot(ctx)
237237
defer release()
238-
s.diagnoseSnapshot(snapshot, nil)
238+
s.diagnoseSnapshot(snapshot, nil, false)
239239
case source.CommandGenerateGoplsMod:
240240
var v source.View
241241
if len(args) == 0 {

internal/lsp/diagnostics.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (s *Server) diagnoseDetached(snapshot source.Snapshot) {
8282
s.publishReports(ctx, snapshot, reports, false)
8383
}
8484

85-
func (s *Server) diagnoseSnapshot(snapshot source.Snapshot, changedURIs []span.URI) {
85+
func (s *Server) diagnoseSnapshot(snapshot source.Snapshot, changedURIs []span.URI, onDisk bool) {
8686
ctx := snapshot.View().BackgroundContext()
8787

8888
delay := snapshot.View().Options().ExperimentalDiagnosticsDelay
@@ -93,7 +93,7 @@ func (s *Server) diagnoseSnapshot(snapshot source.Snapshot, changedURIs []span.U
9393
// by file modifications (no analysis).
9494
//
9595
// The second phase does everything, and is debounced by the configured delay.
96-
reports, err := s.diagnoseChangedFiles(ctx, snapshot, changedURIs)
96+
reports, err := s.diagnoseChangedFiles(ctx, snapshot, changedURIs, onDisk)
9797
if err != nil {
9898
if !errors.Is(err, context.Canceled) {
9999
event.Error(ctx, "diagnosing changed files", err)
@@ -112,11 +112,16 @@ func (s *Server) diagnoseSnapshot(snapshot source.Snapshot, changedURIs []span.U
112112
s.publishReports(ctx, snapshot, reports, false)
113113
}
114114

115-
func (s *Server) diagnoseChangedFiles(ctx context.Context, snapshot source.Snapshot, uris []span.URI) (*reportSet, error) {
115+
func (s *Server) diagnoseChangedFiles(ctx context.Context, snapshot source.Snapshot, uris []span.URI, onDisk bool) (*reportSet, error) {
116116
ctx, done := event.Start(ctx, "Server.diagnoseChangedFiles")
117117
defer done()
118118
packages := make(map[source.Package]struct{})
119119
for _, uri := range uris {
120+
// If the change is only on-disk and the file is not open, don't
121+
// directly request its package. It may not be a workspace package.
122+
if onDisk && !snapshot.IsOpen(uri) {
123+
continue
124+
}
120125
pkgs, err := snapshot.PackagesForFile(ctx, uri, source.TypecheckWorkspace)
121126
if err != nil {
122127
// TODO (rFindley): we should probably do something with the error here,

internal/lsp/text_synchronization.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func (s *Server) didModifyFiles(ctx context.Context, modifications []source.File
236236
diagnosticWG.Add(1)
237237
go func(snapshot source.Snapshot, uris []span.URI) {
238238
defer diagnosticWG.Done()
239-
s.diagnoseSnapshot(snapshot, uris)
239+
s.diagnoseSnapshot(snapshot, uris, cause == FromDidChangeWatchedFiles)
240240
}(snapshot, uris)
241241
}
242242

0 commit comments

Comments
 (0)