internal/lsp: move hasValidBuildConfiguration into the snapshot

Update hasValidBuildConfiguration as modules are created and deleted.

Change-Id: I9196611225d42a87ea5790c564bc9ac1ea1871f1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/257968
Trust: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
diff --git a/internal/lsp/cache/load.go b/internal/lsp/cache/load.go
index 63254f0..b44be38 100644
--- a/internal/lsp/cache/load.go
+++ b/internal/lsp/cache/load.go
@@ -63,7 +63,7 @@
 		case viewLoadScope:
 			// If we are outside of GOPATH, a module, or some other known
 			// build system, don't load subdirectories.
-			if !s.view.hasValidBuildConfiguration {
+			if !s.ValidBuildConfiguration() {
 				query = append(query, "./")
 			} else {
 				query = append(query, "./...")
diff --git a/internal/lsp/cache/session.go b/internal/lsp/cache/session.go
index 7de3bc8..61595a2 100644
--- a/internal/lsp/cache/session.go
+++ b/internal/lsp/cache/session.go
@@ -185,20 +185,19 @@
 	backgroundCtx, cancel := context.WithCancel(baseCtx)
 
 	v := &View{
-		session:                    s,
-		initialized:                make(chan struct{}),
-		initializationSema:         make(chan struct{}, 1),
-		initializeOnce:             &sync.Once{},
-		id:                         strconv.FormatInt(index, 10),
-		options:                    options,
-		baseCtx:                    baseCtx,
-		backgroundCtx:              backgroundCtx,
-		cancel:                     cancel,
-		name:                       name,
-		folder:                     folder,
-		filesByURI:                 make(map[span.URI]*fileBase),
-		filesByBase:                make(map[string][]*fileBase),
-		hasValidBuildConfiguration: validBuildConfiguration,
+		session:            s,
+		initialized:        make(chan struct{}),
+		initializationSema: make(chan struct{}, 1),
+		initializeOnce:     &sync.Once{},
+		id:                 strconv.FormatInt(index, 10),
+		options:            options,
+		baseCtx:            baseCtx,
+		backgroundCtx:      backgroundCtx,
+		cancel:             cancel,
+		name:               name,
+		folder:             folder,
+		filesByURI:         make(map[span.URI]*fileBase),
+		filesByBase:        make(map[string][]*fileBase),
 		processEnv: &imports.ProcessEnv{
 			GocmdRunner: s.gocmdRunner,
 			WorkingDir:  folder.Filename(),
diff --git a/internal/lsp/cache/snapshot.go b/internal/lsp/cache/snapshot.go
index 1021133..759cb33 100644
--- a/internal/lsp/cache/snapshot.go
+++ b/internal/lsp/cache/snapshot.go
@@ -125,6 +125,17 @@
 	return s.view.session.cache.fset
 }
 
+func (s *snapshot) ModFiles() []span.URI {
+	if s.view.modURI == "" {
+		return nil
+	}
+	return []span.URI{s.view.modURI}
+}
+
+func (s *snapshot) ValidBuildConfiguration() bool {
+	return validBuildConfiguration(s.view.rootURI, &s.view.workspaceInformation, s.modules)
+}
+
 // config returns the configuration used for the snapshot's interaction with
 // the go/packages API. It uses the given working directory.
 //
@@ -811,7 +822,7 @@
 func (s *snapshot) reloadWorkspace(ctx context.Context) error {
 	// If the view's build configuration is invalid, we cannot reload by
 	// package path. Just reload the directory instead.
-	if !s.view.hasValidBuildConfiguration {
+	if !s.ValidBuildConfiguration() {
 		return s.load(ctx, viewLoadScope("LOAD_INVALID_VIEW"))
 	}
 
@@ -1213,7 +1224,7 @@
 		result.workspacePackages[id] = pkgPath
 	}
 
-	if shouldReinitializeView && s.view.hasValidBuildConfiguration {
+	if shouldReinitializeView {
 		s.view.definitelyReinitialize()
 	}
 
diff --git a/internal/lsp/cache/view.go b/internal/lsp/cache/view.go
index 5be4c00..fca69e8 100644
--- a/internal/lsp/cache/view.go
+++ b/internal/lsp/cache/view.go
@@ -121,10 +121,6 @@
 	// workspaceMode describes the way in which the view's workspace should be
 	// loaded.
 	workspaceMode workspaceMode
-
-	// True if the view is either in GOPATH, a module, or some other
-	// non go command build system.
-	hasValidBuildConfiguration bool
 }
 
 type workspaceInformation struct {
@@ -209,17 +205,6 @@
 
 func (v *View) ID() string { return v.id }
 
-func (s *snapshot) ValidBuildConfiguration() bool {
-	return s.view.hasValidBuildConfiguration
-}
-
-func (s *snapshot) ModFiles() []span.URI {
-	if s.view.modURI == "" {
-		return nil
-	}
-	return []span.URI{s.view.modURI}
-}
-
 // tempModFile creates a temporary go.mod file based on the contents of the
 // given go.mod file. It is the caller's responsibility to clean up the files
 // when they are done using them.
@@ -376,7 +361,7 @@
 		s.view.folder.Filename(),
 		s.view.rootURI.Filename(),
 		goVersion.String(),
-		s.view.hasValidBuildConfiguration,
+		s.ValidBuildConfiguration(),
 		buildFlags)
 	for k, v := range fullEnv {
 		fmt.Fprintf(w, "%s=%s\n", k, v)