From a014e0aa6a8b69cf2fbc73be32c5b1bd180fe5b7 Mon Sep 17 00:00:00 2001 From: Rohan Challa Date: Fri, 31 Jan 2020 13:59:11 -0500 Subject: [PATCH] internal/lsp: skip packages load for auxilary go.mod changes This change will skip packages.load calls when a go.mod file changes if that go.mod file is not the go.mod file associated with the view. Change-Id: I23a214a89203dd58417f3e2f69725ce3b669a5ca Reviewed-on: https://go-review.googlesource.com/c/tools/+/217238 Reviewed-by: Rebecca Stambler Run-TryBot: Rohan Challa TryBot-Result: Gobot Gobot --- internal/lsp/cache/snapshot.go | 3 ++- internal/lsp/text_synchronization.go | 10 +++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/internal/lsp/cache/snapshot.go b/internal/lsp/cache/snapshot.go index 419c2a5ac4..fb66be0ff3 100644 --- a/internal/lsp/cache/snapshot.go +++ b/internal/lsp/cache/snapshot.go @@ -752,7 +752,8 @@ func (s *snapshot) shouldLoad(ctx context.Context, originalFH, currentFH source. } // If a go.mod file's contents have changed, always invalidate metadata. if kind := originalFH.Identity().Kind; kind == source.Mod { - return true + modfile, _ := s.view.ModFiles() + return originalFH.Identity().URI == modfile } // Get the original and current parsed files in order to check package name and imports. original, _, _, originalErr := s.view.session.cache.ParseGoHandle(originalFH, source.ParseHeader).Parse(ctx) diff --git a/internal/lsp/text_synchronization.go b/internal/lsp/text_synchronization.go index 3a96cebef8..3d95a85291 100644 --- a/internal/lsp/text_synchronization.go +++ b/internal/lsp/text_synchronization.go @@ -147,11 +147,15 @@ func (s *Server) didModifyFiles(ctx context.Context, modifications []source.File if err != nil { return nil, err } - // If a modification comes in for a go.mod file, - // and the view was never properly initialized, - // try to recreate the associated view. + // If a modification comes in for the view's go.mod file and the view + // was never properly initialized, or the view does not have + // a go.mod file, try to recreate the associated view. switch fh.Identity().Kind { case source.Mod: + modfile, _ := snapshot.View().ModFiles() + if modfile != "" || fh.Identity().URI != modfile { + continue + } newSnapshot, err := snapshot.View().Rebuild(ctx) if err != nil { return nil, err