internal/lsp: do not allow non-Go files to be loaded in packages.load

Fixes golang/go#41962

Change-Id: I47ec376213541095386add09e63a5127473a6d27
Reviewed-on: https://go-review.googlesource.com/c/tools/+/263983
Trust: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
diff --git a/internal/lsp/cache/load.go b/internal/lsp/cache/load.go
index a5ae427..12a09b6 100644
--- a/internal/lsp/cache/load.go
+++ b/internal/lsp/cache/load.go
@@ -60,9 +60,10 @@
 			uri := span.URI(scope)
 			// Don't try to load a file that doesn't exist.
 			fh := s.FindFile(uri)
-			if fh != nil {
-				query = append(query, fmt.Sprintf("file=%s", uri.Filename()))
+			if fh == nil || fh.Kind() != source.Go {
+				continue
 			}
+			query = append(query, fmt.Sprintf("file=%s", uri.Filename()))
 		case moduleLoadScope:
 			query = append(query, fmt.Sprintf("%s/...", scope))
 		case viewLoadScope:
diff --git a/internal/lsp/cache/snapshot.go b/internal/lsp/cache/snapshot.go
index 93be75a..021c534 100644
--- a/internal/lsp/cache/snapshot.go
+++ b/internal/lsp/cache/snapshot.go
@@ -388,6 +388,13 @@
 	// Check if we should reload metadata for the file. We don't invalidate IDs
 	// (though we should), so the IDs will be a better source of truth than the
 	// metadata. If there are no IDs for the file, then we should also reload.
+	fh, err := s.GetFile(ctx, uri)
+	if err != nil {
+		return nil, err
+	}
+	if fh.Kind() != source.Go {
+		return nil, fmt.Errorf("no packages for non-Go file %s", uri)
+	}
 	ids := s.getIDsForURI(uri)
 	reload := len(ids) == 0
 	for _, id := range ids {