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