internal/lsp: ignore files that begin with underscores

Fixes golang/go#33540

Change-Id: I5b55c23ac8ff54db94387ed6b70ba39c61ba6108
Reviewed-on: https://go-review.googlesource.com/c/tools/+/189940
Reviewed-by: Ian Cottrell <iancottrell@google.com>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
diff --git a/internal/lsp/cache/session.go b/internal/lsp/cache/session.go
index 3511bb3..fce1475 100644
--- a/internal/lsp/cache/session.go
+++ b/internal/lsp/cache/session.go
@@ -7,6 +7,7 @@
 import (
 	"context"
 	"os"
+	"path/filepath"
 	"sort"
 	"strconv"
 	"strings"
@@ -184,6 +185,17 @@
 // TODO: Propagate the language ID through to the view.
 func (s *session) DidOpen(ctx context.Context, uri span.URI, _ source.FileKind, text []byte) {
 	ctx = telemetry.File.With(ctx, uri)
+
+	// Files with _ prefixes are ignored.
+	if strings.HasPrefix(filepath.Base(uri.Filename()), "_") {
+		for _, view := range s.views {
+			view.ignoredURIsMu.Lock()
+			view.ignoredURIs[uri] = struct{}{}
+			view.ignoredURIsMu.Unlock()
+		}
+		return
+	}
+
 	// Mark the file as open.
 	s.openFiles.Store(uri, true)
 
diff --git a/internal/lsp/cache/view.go b/internal/lsp/cache/view.go
index 4f529f1..ba99eb5 100644
--- a/internal/lsp/cache/view.go
+++ b/internal/lsp/cache/view.go
@@ -84,7 +84,8 @@
 	builtinPkg *ast.Package
 
 	// ignoredURIs is the set of URIs of files that we ignore.
-	ignoredURIs map[span.URI]struct{}
+	ignoredURIsMu sync.Mutex
+	ignoredURIs   map[span.URI]struct{}
 }
 
 type metadataCache struct {
@@ -289,6 +290,9 @@
 // Ignore checks if the given URI is a URI we ignore.
 // As of right now, we only ignore files in the "builtin" package.
 func (v *view) Ignore(uri span.URI) bool {
+	v.ignoredURIsMu.Lock()
+	defer v.ignoredURIsMu.Unlock()
+
 	_, ok := v.ignoredURIs[uri]
 	return ok
 }
@@ -326,7 +330,10 @@
 			return
 		}
 		files[filename] = file
+
+		v.ignoredURIsMu.Lock()
 		v.ignoredURIs[span.NewURI(filename)] = struct{}{}
+		v.ignoredURIsMu.Unlock()
 	}
 	v.builtinPkg, _ = ast.NewPackage(cfg.Fset, files, nil, nil)
 }
@@ -341,7 +348,9 @@
 	v.cancel()
 	v.backgroundCtx, v.cancel = context.WithCancel(v.baseCtx)
 
-	v.session.SetOverlay(uri, content)
+	if !v.Ignore(uri) {
+		v.session.SetOverlay(uri, content)
+	}
 
 	return nil
 }