From f6a1a6ff8e04e3068b8a3cf01747c06092ffd1d0 Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Wed, 16 Oct 2019 16:31:27 -0400 Subject: [PATCH] internal/lsp: check if the go/packages context has been canceled Recently been noticing errors where we don't have full metadata for a given package. It seems to me that, since we added the context to the packages.Config, there have been cases where the context is canceled on the first load, and then we type-check with incomplete data. I'm still not sure if allowing go/packages to be canceled is the correct approach. Change-Id: I6767ce763538bd579458c8f8db07f15c9eec7b4a Reviewed-on: https://go-review.googlesource.com/c/tools/+/201518 Run-TryBot: Rebecca Stambler Reviewed-by: Muir Manders Reviewed-by: Michael Matloob --- internal/lsp/cache/load.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/internal/lsp/cache/load.go b/internal/lsp/cache/load.go index 48b89f82c4..4c3b7d8650 100644 --- a/internal/lsp/cache/load.go +++ b/internal/lsp/cache/load.go @@ -39,8 +39,14 @@ func (s *snapshot) load(ctx context.Context, uri span.URI) ([]*metadata, error) cfg := s.view.Config(ctx) pkgs, err := packages.Load(cfg, fmt.Sprintf("file=%s", uri.Filename())) - log.Print(ctx, "go/packages.Load", tag.Of("packages", len(pkgs))) + // If the context was canceled, return early. + // Otherwise, we might be type-checking an incomplete result. + if err == context.Canceled { + return nil, errors.Errorf("no metadata for %s: %v", uri.Filename(), err) + } + + log.Print(ctx, "go/packages.Load", tag.Of("packages", len(pkgs))) if len(pkgs) == 0 { if err == nil { err = errors.Errorf("go/packages.Load: no packages found for %s", uri)