gopls/internal/lsp/cache: improve ad-hoc warning for nested modules

Our warning for the specific (and somewhat arbitrary) case of opening a
nested module was stale. Update it for workspaces.

This is a very weak distillation of CL 448695, targeted at improving
this one error message.

Change-Id: Ic78e2f8ff8004a78ac2a0650b40767de9dfe153c
Reviewed-on: https://go-review.googlesource.com/c/tools/+/454563
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
diff --git a/gopls/internal/lsp/cache/snapshot.go b/gopls/internal/lsp/cache/snapshot.go
index 0b56120..9650655 100644
--- a/gopls/internal/lsp/cache/snapshot.go
+++ b/gopls/internal/lsp/cache/snapshot.go
@@ -1212,6 +1212,8 @@
 	return results, nil
 }
 
+// TODO(rfindley): clarify that this is only active modules. Or update to just
+// use findRootPattern.
 func (s *snapshot) GoModForFile(uri span.URI) span.URI {
 	return moduleForURI(s.workspace.activeModFiles, uri)
 }
@@ -1396,13 +1398,31 @@
 		//
 		// TODO(rfindley): re-evaluate this heuristic.
 		if containsCommandLineArguments(wsPkgs) {
-			return s.workspaceLayoutError(ctx)
+			err, diags := s.workspaceLayoutError(ctx)
+			if err != nil {
+				if ctx.Err() != nil {
+					return nil // see the API documentation for source.Snapshot
+				}
+				return &source.CriticalError{
+					MainError:   err,
+					Diagnostics: diags,
+				}
+			}
 		}
 		return nil
 	}
 
 	if errMsg := loadErr.MainError.Error(); strings.Contains(errMsg, "cannot find main module") || strings.Contains(errMsg, "go.mod file not found") {
-		return s.workspaceLayoutError(ctx)
+		err, diags := s.workspaceLayoutError(ctx)
+		if err != nil {
+			if ctx.Err() != nil {
+				return nil // see the API documentation for source.Snapshot
+			}
+			return &source.CriticalError{
+				MainError:   err,
+				Diagnostics: diags,
+			}
+		}
 	}
 	return loadErr
 }