internal/lsp: fix setting overlays in tests

This change fixes the test failure that has appeared in a few TryBot
runs.

Change-Id: If583555250d63b7f446ec7d8eb09810b842633ae
Reviewed-on: https://go-review.googlesource.com/c/tools/+/179437
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 bc68775..e6d8c27 100644
--- a/internal/lsp/cache/check.go
+++ b/internal/lsp/cache/check.go
@@ -141,7 +141,7 @@
 		if f, _ := v.getFile(span.FileURI(filename)); f != nil {
 			gof, ok := f.(*goFile)
 			if !ok {
-				v.Session().Logger().Errorf(ctx, "not a go file: %v", f.URI())
+				v.Session().Logger().Errorf(ctx, "not a Go file: %v", f.URI())
 				continue
 			}
 			gof.meta = m
diff --git a/internal/lsp/cache/file.go b/internal/lsp/cache/file.go
index f71a15e..36ec9e4 100644
--- a/internal/lsp/cache/file.go
+++ b/internal/lsp/cache/file.go
@@ -54,6 +54,7 @@
 func (f *fileBase) Content(ctx context.Context) *source.FileContent {
 	f.view.mu.Lock()
 	defer f.view.mu.Unlock()
+
 	f.read(ctx)
 	return f.fc
 }
diff --git a/internal/lsp/cache/session.go b/internal/lsp/cache/session.go
index aa4ea32..57735b2 100644
--- a/internal/lsp/cache/session.go
+++ b/internal/lsp/cache/session.go
@@ -94,12 +94,11 @@
 	s.viewMu.Lock()
 	defer s.viewMu.Unlock()
 
-	// check if we already know this file
+	// Check if we already know this file.
 	if v, found := s.viewMap[uri]; found {
 		return v
 	}
-
-	// pick the best view for this file and memoize the result
+	// Pick the best view for this file and memoize the result.
 	v := s.bestView(uri)
 	s.viewMap[uri] = v
 	return v
@@ -131,7 +130,7 @@
 	if longest != nil {
 		return longest
 	}
-	//TODO: are there any more heuristics we can use?
+	// TODO: are there any more heuristics we can use?
 	return s.views[0]
 }
 
@@ -175,13 +174,10 @@
 }
 
 func (s *session) ReadFile(uri span.URI) *source.FileContent {
-	s.overlayMu.Lock()
-	defer s.overlayMu.Unlock()
-	// We might have the content saved in an overlay.
-	if overlay, ok := s.overlays[uri]; ok {
+	if overlay := s.readOverlay(uri); overlay != nil {
 		return overlay
 	}
-	// fall back to the cache level file system
+	// Fall back to the cache-level file system.
 	return s.Cache().ReadFile(uri)
 }
 
@@ -191,6 +187,7 @@
 		s.overlayMu.Unlock()
 		s.filesWatchMap.Notify(uri)
 	}()
+
 	if data == nil {
 		delete(s.overlays, uri)
 		return
@@ -202,9 +199,21 @@
 	}
 }
 
+func (s *session) readOverlay(uri span.URI) *source.FileContent {
+	s.overlayMu.Lock()
+	defer s.overlayMu.Unlock()
+
+	// We might have the content saved in an overlay.
+	if overlay, ok := s.overlays[uri]; ok {
+		return overlay
+	}
+	return nil
+}
+
 func (s *session) buildOverlay() map[string][]byte {
 	s.overlayMu.Lock()
 	defer s.overlayMu.Unlock()
+
 	overlays := make(map[string][]byte)
 	for uri, overlay := range s.overlays {
 		if overlay.Error != nil {
diff --git a/internal/lsp/lsp_test.go b/internal/lsp/lsp_test.go
index 61156f2..d4a0d54 100644
--- a/internal/lsp/lsp_test.go
+++ b/internal/lsp/lsp_test.go
@@ -36,7 +36,6 @@
 const viewName = "lsp_test"
 
 func testLSP(t *testing.T, exporter packagestest.Exporter) {
-	ctx := context.Background()
 	data := tests.Load(t, exporter, "testdata")
 	defer data.Exported.Cleanup()
 
@@ -46,7 +45,7 @@
 	view := session.NewView(viewName, span.FileURI(data.Config.Dir))
 	view.SetEnv(data.Config.Env)
 	for filename, content := range data.Config.Overlay {
-		view.SetContent(ctx, span.FileURI(filename), content)
+		session.SetOverlay(span.FileURI(filename), content)
 	}
 	r := &runner{
 		server: &Server{
diff --git a/internal/lsp/util.go b/internal/lsp/util.go
index 8acf535..b931078 100644
--- a/internal/lsp/util.go
+++ b/internal/lsp/util.go
@@ -71,7 +71,7 @@
 	}
 	gof, ok := f.(source.GoFile)
 	if !ok {
-		return nil, nil, fmt.Errorf("not a go file %v", f.URI())
+		return nil, nil, fmt.Errorf("not a Go file %v", f.URI())
 	}
 	return gof, m, nil
 }