internal/lsp: fix bug in missing imports tracking

Change-Id: Iaba7e9cd8a8fb54f8e78f52afa0ef98790288c93
Reviewed-on: https://go-review.googlesource.com/c/tools/+/184161
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/load.go b/internal/lsp/cache/load.go
index 789784d..2d707ed 100644
--- a/internal/lsp/cache/load.go
+++ b/internal/lsp/cache/load.go
@@ -78,9 +78,6 @@
 	f.mu.Lock()
 	defer f.mu.Unlock()
 
-	// Save the metadata's current missing imports, if any.
-	originalMissingImports := f.missingImports
-
 	if !v.parseImports(ctx, f) {
 		return f.meta, nil, nil
 	}
@@ -107,10 +104,8 @@
 		}, err
 	}
 
-	// Clear missing imports.
-	for k := range f.missingImports {
-		delete(f.missingImports, k)
-	}
+	// Track missing imports as we look at the package's errors.
+	missingImports := make(map[packagePath]struct{})
 	for _, pkg := range pkgs {
 		// If the package comes back with errors from `go list`,
 		// don't bother type-checking it.
@@ -120,10 +115,7 @@
 		for importPath, importPkg := range pkg.Imports {
 			// If we encounter a package we cannot import, mark it as missing.
 			if importPkg.PkgPath != "unsafe" && len(importPkg.CompiledGoFiles) == 0 {
-				if f.missingImports == nil {
-					f.missingImports = make(map[packagePath]struct{})
-				}
-				f.missingImports[packagePath(importPath)] = struct{}{}
+				missingImports[packagePath(importPath)] = struct{}{}
 			}
 		}
 		// Build the import graph for this package.
@@ -135,11 +127,13 @@
 		return nil, nil, fmt.Errorf("loadParseTypecheck: no metadata found for %v", f.filename())
 	}
 
-	// If we have already seen these missing imports before, and we still have type information,
+	// If we have already seen these missing imports before, and we have type information,
 	// there is no need to continue.
-	if sameSet(originalMissingImports, f.missingImports) && len(f.pkgs) != 0 {
+	if sameSet(missingImports, f.missingImports) && len(f.pkgs) != 0 {
 		return nil, nil, nil
 	}
+	// Otherwise, update the missing imports map.
+	f.missingImports = missingImports
 
 	return f.meta, nil, nil
 }