internal/lsp: cache file objects for every dependency

Change-Id: I68eedc49a07aa9ba3328a4380e97ed03d1b75749
Reviewed-on: https://go-review.googlesource.com/c/tools/+/170180
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
diff --git a/internal/lsp/cache/check.go b/internal/lsp/cache/check.go
index 1fdbf4a..1b5e5ca 100644
--- a/internal/lsp/cache/check.go
+++ b/internal/lsp/cache/check.go
@@ -43,6 +43,7 @@
 	imp := &importer{
 		view:     v,
 		circular: make(map[string]struct{}),
+		ctx:      ctx,
 	}
 	// Start prefetching direct imports.
 	for importPath := range f.meta.children {
@@ -53,8 +54,6 @@
 	if pkg == nil || pkg.GetTypes() == nil {
 		return nil, err
 	}
-	// Add every file in this package to our cache.
-	v.cachePackage(ctx, pkg)
 
 	// If we still have not found the package for the file, something is wrong.
 	if f.pkg == nil {
@@ -150,7 +149,7 @@
 	m.name = pkg.Name
 	m.files = pkg.CompiledGoFiles
 	for _, filename := range m.files {
-		if f, _ := v.findFile(span.FileURI(filename)); f != nil {
+		if f, _ := v.getFile(span.FileURI(filename)); f != nil {
 			f.meta = m
 		}
 	}
@@ -182,6 +181,8 @@
 	// circular maintains the set of previously imported packages.
 	// If we have seen a package that is already in this map, we have a circular import.
 	circular map[string]struct{}
+
+	ctx context.Context
 }
 
 func (imp *importer) Import(pkgPath string) (*types.Package, error) {
@@ -259,11 +260,15 @@
 		Importer: &importer{
 			view:     imp.view,
 			circular: newCircular,
+			ctx:      imp.ctx,
 		},
 	}
 	check := types.NewChecker(cfg, imp.view.Config.Fset, pkg.types, pkg.typesInfo)
 	check.Files(pkg.syntax)
 
+	// Add every file in this package to our cache.
+	imp.view.cachePackage(imp.ctx, pkg)
+
 	// Set imports of package to correspond to cached packages.
 	// We lock the package cache, but we shouldn't get any inconsistencies
 	// because we are still holding the lock on the view.