internal/lsp: invalidate directories if we have no direct IDs

Previously, we were only invalidating packages in directories if we
didn't have a file handle. We should also invalidate if we have an
unparseable file handle - that is, a file with no content or no package
name.

Fixes golang/go#36608.

Change-Id: Ia12fad962c06ddeeac382185d3220961f5c584e0
Reviewed-on: https://go-review.googlesource.com/c/tools/+/215318
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
diff --git a/internal/lsp/cache/load.go b/internal/lsp/cache/load.go
index 8f8df23..98b083a 100644
--- a/internal/lsp/cache/load.go
+++ b/internal/lsp/cache/load.go
@@ -74,7 +74,7 @@
 		return nil, ctx.Err()
 	}
 
-	log.Print(ctx, "go/packages.Load", tag.Of("query", query), tag.Of("packages", len(pkgs)))
+	log.Print(ctx, "go/packages.Load", tag.Of("snapshot", s.ID()), tag.Of("query", query), tag.Of("packages", len(pkgs)))
 	if len(pkgs) == 0 {
 		if err == nil {
 			err = errors.Errorf("no packages found for query %s", query)
diff --git a/internal/lsp/cache/snapshot.go b/internal/lsp/cache/snapshot.go
index d06253d..d24c9bc 100644
--- a/internal/lsp/cache/snapshot.go
+++ b/internal/lsp/cache/snapshot.go
@@ -8,7 +8,6 @@
 	"context"
 	"os"
 	"path/filepath"
-	"strings"
 	"sync"
 
 	"golang.org/x/tools/go/analysis"
@@ -629,10 +628,10 @@
 	// Make a rough estimate of what metadata to invalidate by finding the package IDs
 	// of all of the files in the same directory as this one.
 	// TODO(rstambler): Speed this up by mapping directories to filenames.
-	if originalFH == nil {
-		if dirStat, err := os.Stat(dir(withoutURI.Filename())); err == nil {
+	if len(directIDs) == 0 {
+		if dirStat, err := os.Stat(filepath.Dir(withoutURI.Filename())); err == nil {
 			for uri := range s.files {
-				if fdirStat, err := os.Stat(dir(uri.Filename())); err == nil {
+				if fdirStat, err := os.Stat(filepath.Dir(uri.Filename())); err == nil {
 					if os.SameFile(dirStat, fdirStat) {
 						for _, id := range s.ids[uri] {
 							directIDs[id] = struct{}{}
@@ -643,13 +642,6 @@
 		}
 	}
 
-	// If there is no known FileHandle and no known IDs for the given file,
-	// there is nothing to invalidate.
-	if len(directIDs) == 0 && originalFH == nil {
-		// TODO(heschi): clone anyway? Seems like this is just setting us up for trouble.
-		return s
-	}
-
 	// Invalidate reverse dependencies too.
 	// TODO(heschi): figure out the locking model and use transitiveReverseDeps?
 	transitiveIDs := make(map[packageID]struct{})
@@ -743,10 +735,6 @@
 	return result
 }
 
-func dir(filename string) string {
-	return strings.ToLower(filepath.Dir(filename))
-}
-
 func (s *snapshot) ID() uint64 {
 	return s.id
 }