gopls/internal/lsp/cache: delete Snapshot.KnownPackages

It is redundant w.r.t. AllValidMetadata (now named AllMetadata)
followed by TypeCheck.

Change-Id: Ibca1516e3d9f036e4d2682b43cd73c29e8d5d981
Reviewed-on: https://go-review.googlesource.com/c/tools/+/456975
Run-TryBot: Alan Donovan <adonovan@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
diff --git a/gopls/internal/lsp/cache/snapshot.go b/gopls/internal/lsp/cache/snapshot.go
index 88ae7fe..c518aca 100644
--- a/gopls/internal/lsp/cache/snapshot.go
+++ b/gopls/internal/lsp/cache/snapshot.go
@@ -1136,27 +1136,7 @@
 	return result
 }
 
-func (s *snapshot) KnownPackages(ctx context.Context) ([]source.Package, error) {
-	if err := s.awaitLoaded(ctx); err != nil {
-		return nil, err
-	}
-
-	s.mu.Lock()
-	g := s.meta
-	s.mu.Unlock()
-
-	pkgs := make([]source.Package, 0, len(g.metadata))
-	for id := range g.metadata {
-		pkg, err := s.checkedPackage(ctx, id, s.workspaceParseMode(id))
-		if err != nil {
-			return nil, err
-		}
-		pkgs = append(pkgs, pkg)
-	}
-	return pkgs, nil
-}
-
-func (s *snapshot) AllValidMetadata(ctx context.Context) ([]*source.Metadata, error) {
+func (s *snapshot) AllMetadata(ctx context.Context) ([]*source.Metadata, error) {
 	if err := s.awaitLoaded(ctx); err != nil {
 		return nil, err
 	}
diff --git a/gopls/internal/lsp/source/implementation.go b/gopls/internal/lsp/source/implementation.go
index ca62f4e..315e388 100644
--- a/gopls/internal/lsp/source/implementation.go
+++ b/gopls/internal/lsp/source/implementation.go
@@ -68,11 +68,19 @@
 		allNamed []*types.Named
 		pkgs     = make(map[*types.Package]Package)
 	)
-	knownPkgs, err := s.KnownPackages(ctx)
+	metas, err := s.AllMetadata(ctx)
 	if err != nil {
 		return nil, err
 	}
-	for _, pkg := range knownPkgs {
+	ids := make([]PackageID, len(metas))
+	for i, m := range metas {
+		ids[i] = m.ID
+	}
+	packages, err := s.TypeCheck(ctx, TypecheckFull, ids...)
+	if err != nil {
+		return nil, err
+	}
+	for _, pkg := range packages {
 		pkgs[pkg.GetTypes()] = pkg
 		for _, obj := range pkg.GetTypesInfo().Defs {
 			obj, ok := obj.(*types.TypeName)
diff --git a/gopls/internal/lsp/source/known_packages.go b/gopls/internal/lsp/source/known_packages.go
index 19abf8f..d84febe 100644
--- a/gopls/internal/lsp/source/known_packages.go
+++ b/gopls/internal/lsp/source/known_packages.go
@@ -58,7 +58,7 @@
 	}
 
 	// Now find candidates among known packages.
-	knownPkgs, err := snapshot.AllValidMetadata(ctx)
+	knownPkgs, err := snapshot.AllMetadata(ctx)
 	if err != nil {
 		return nil, err
 	}
@@ -86,7 +86,7 @@
 		if isDirectlyCyclical(current, knownPkg) {
 			continue
 		}
-		// AllValidMetadata may have multiple variants of a pkg.
+		// AllMetadata may have multiple variants of a pkg.
 		seen[knownPkg.PkgPath] = true
 	}
 
diff --git a/gopls/internal/lsp/source/rename.go b/gopls/internal/lsp/source/rename.go
index 6855e20..9b089cf 100644
--- a/gopls/internal/lsp/source/rename.go
+++ b/gopls/internal/lsp/source/rename.go
@@ -204,7 +204,7 @@
 			return nil, true, fmt.Errorf("cannot rename to _test package")
 		}
 
-		metadata, err := s.AllValidMetadata(ctx)
+		metadata, err := s.AllMetadata(ctx)
 		if err != nil {
 			return nil, true, err
 		}
diff --git a/gopls/internal/lsp/source/view.go b/gopls/internal/lsp/source/view.go
index 493553b..6b32639 100644
--- a/gopls/internal/lsp/source/view.go
+++ b/gopls/internal/lsp/source/view.go
@@ -222,13 +222,6 @@
 	// It is intended for use only in completions.
 	CachedImportPaths(ctx context.Context) (map[PackagePath]Package, error)
 
-	// KnownPackages returns a new unordered list of all packages
-	// loaded in this snapshot, checked in TypecheckWorkspace mode.
-	//
-	// TODO(adonovan): opt: rewrite 'implementations' to avoid the
-	// need ever to "load everything at once" using this function.
-	KnownPackages(ctx context.Context) ([]Package, error)
-
 	// ActiveMetadata returns a new, unordered slice containing
 	// metadata for all packages considered 'active' in the workspace.
 	//
@@ -236,8 +229,8 @@
 	// mode, this is just the reverse transitive closure of open packages.
 	ActiveMetadata(ctx context.Context) ([]*Metadata, error)
 
-	// AllValidMetadata returns all valid metadata loaded for the snapshot.
-	AllValidMetadata(ctx context.Context) ([]*Metadata, error)
+	// AllMetadata returns a new unordered array of metadata for all packages in the workspace.
+	AllMetadata(ctx context.Context) ([]*Metadata, error)
 
 	// Symbols returns all symbols in the snapshot.
 	Symbols(ctx context.Context) map[span.URI][]Symbol
diff --git a/gopls/internal/vulncheck/command.go b/gopls/internal/vulncheck/command.go
index 45c7076..c450966 100644
--- a/gopls/internal/vulncheck/command.go
+++ b/gopls/internal/vulncheck/command.go
@@ -202,18 +202,18 @@
 func vulnerablePackages(ctx context.Context, snapshot source.Snapshot, modfile source.FileHandle) (*govulncheck.Result, error) {
 	// We want to report the intersection of vulnerable packages in the vulndb
 	// and packages transitively imported by this module ('go list -deps all').
-	// We use snapshot.AllValidMetadata to retrieve the list of packages
+	// We use snapshot.AllMetadata to retrieve the list of packages
 	// as an approximation.
 	//
-	// TODO(hyangah): snapshot.AllValidMetadata is a superset of
+	// TODO(hyangah): snapshot.AllMetadata is a superset of
 	// `go list all` - e.g. when the workspace has multiple main modules
 	// (multiple go.mod files), that can include packages that are not
 	// used by this module. Vulncheck behavior with go.work is not well
 	// defined. Figure out the meaning, and if we decide to present
 	// the result as if each module is analyzed independently, make
 	// gopls track a separate build list for each module and use that
-	// information instead of snapshot.AllValidMetadata.
-	metadata, err := snapshot.AllValidMetadata(ctx)
+	// information instead of snapshot.AllMetadata.
+	metadata, err := snapshot.AllMetadata(ctx)
 	if err != nil {
 		return nil, err
 	}