gopls/internal/lsp/cache: move ignoreFilter onto View

Now that the View has immutable Options and Directories, we can move
the ignoreFilter to it.

Change-Id: I11d182ee25c294af6a5aa124ecc94f288f5180b5
Reviewed-on: https://go-review.googlesource.com/c/tools/+/545539
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
diff --git a/gopls/internal/lsp/cache/session.go b/gopls/internal/lsp/cache/session.go
index 9b632ca..0d36ca8 100644
--- a/gopls/internal/lsp/cache/session.go
+++ b/gopls/internal/lsp/cache/session.go
@@ -149,6 +149,22 @@
 		}
 	}
 
+	var ignoreFilter *ignoreFilter
+	{
+		var dirs []string
+		if len(def.workspaceModFiles) == 0 {
+			for _, entry := range filepath.SplitList(def.gopath) {
+				dirs = append(dirs, filepath.Join(entry, "src"))
+			}
+		} else {
+			dirs = append(dirs, def.gomodcache)
+			for m := range def.workspaceModFiles {
+				dirs = append(dirs, filepath.Dir(m.Path()))
+			}
+		}
+		ignoreFilter = newIgnoreFilter(dirs)
+	}
+
 	v := &View{
 		id:                   strconv.FormatInt(index, 10),
 		gocmdRunner:          s.gocmdRunner,
@@ -157,6 +173,7 @@
 		initializationSema:   make(chan struct{}, 1),
 		baseCtx:              baseCtx,
 		parseCache:           s.parseCache,
+		ignoreFilter:         ignoreFilter,
 		fs:                   s.overlayFS,
 		viewDefinition:       def,
 		importsState: &importsState{
diff --git a/gopls/internal/lsp/cache/snapshot.go b/gopls/internal/lsp/cache/snapshot.go
index c151ba1..a72b52d 100644
--- a/gopls/internal/lsp/cache/snapshot.go
+++ b/gopls/internal/lsp/cache/snapshot.go
@@ -188,11 +188,6 @@
 	// pkgIndex is an index of package IDs, for efficient storage of typerefs.
 	pkgIndex *typerefs.PackageIndex
 
-	// Only compute module prefixes once, as they are used with high frequency to
-	// detect ignored files.
-	ignoreFilterOnce sync.Once
-	ignoreFilter     *ignoreFilter
-
 	// moduleUpgrades tracks known upgrades for module paths in each modfile.
 	// Each modfile has a map of module name to upgrade version.
 	moduleUpgrades *persistent.Map[protocol.DocumentURI, map[string]string]
diff --git a/gopls/internal/lsp/cache/view.go b/gopls/internal/lsp/cache/view.go
index 8d52fc8..e39637d 100644
--- a/gopls/internal/lsp/cache/view.go
+++ b/gopls/internal/lsp/cache/view.go
@@ -85,6 +85,9 @@
 	knownFilesMu sync.Mutex
 	knownFiles   map[protocol.DocumentURI]bool
 
+	// ignoreFilter is used for fast checking of ignored files.
+	ignoreFilter *ignoreFilter
+
 	// initCancelFirstAttempt can be used to terminate the view's first
 	// attempt at initialization.
 	initCancelFirstAttempt context.CancelFunc
@@ -671,22 +674,7 @@
 		}
 	}
 
-	s.ignoreFilterOnce.Do(func() {
-		var dirs []string
-		if len(s.view.workspaceModFiles) == 0 {
-			for _, entry := range filepath.SplitList(s.view.gopath) {
-				dirs = append(dirs, filepath.Join(entry, "src"))
-			}
-		} else {
-			dirs = append(dirs, s.view.gomodcache)
-			for m := range s.view.workspaceModFiles {
-				dirs = append(dirs, filepath.Dir(m.Path()))
-			}
-		}
-		s.ignoreFilter = newIgnoreFilter(dirs)
-	})
-
-	return s.ignoreFilter.ignored(uri.Path())
+	return s.view.ignoreFilter.ignored(uri.Path())
 }
 
 // An ignoreFilter implements go list's exclusion rules via its 'ignored' method.
diff --git a/gopls/internal/lsp/diagnostics.go b/gopls/internal/lsp/diagnostics.go
index f94737c..b2d9026 100644
--- a/gopls/internal/lsp/diagnostics.go
+++ b/gopls/internal/lsp/diagnostics.go
@@ -341,6 +341,7 @@
 	if s.shouldIgnoreError(ctx, snapshot, err) {
 		return
 	}
+
 	criticalErr := snapshot.CriticalError(ctx)
 	if ctx.Err() != nil { // must check ctx after GetCriticalError
 		return