gopls/internal/regtest/codelens: avoid a race in TestUpgradeCodelens

Avoid (but do not fix) a race in TestUpgradeCodelens: because upgrade
and vuln diagnostics access state on the View, they race with
asynchronous diagnostics, which assume that diagnostics are idempotent.

Diagnostics _should_ be idempotent: we should create a new snapshot when
the view state changes (and inject that state into the snapshot). But
that is a larger change, and this CL will confirm the hypothesis above
if the flakes go away.

For golang/go#58750

Change-Id: If827aab0ae187c5c377d830d76caf626b51bc3bc
Reviewed-on: https://go-review.googlesource.com/c/tools/+/521895
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
diff --git a/gopls/internal/regtest/codelens/codelens_test.go b/gopls/internal/regtest/codelens/codelens_test.go
index 8f71885..b72e598 100644
--- a/gopls/internal/regtest/codelens/codelens_test.go
+++ b/gopls/internal/regtest/codelens/codelens_test.go
@@ -199,13 +199,25 @@
 	}
 	for _, vendoring := range []bool{false, true} {
 		t.Run(fmt.Sprintf("Upgrade individual dependency vendoring=%v", vendoring), func(t *testing.T) {
-			WithOptions(ProxyFiles(proxyWithLatest)).Run(t, shouldUpdateDep, func(t *testing.T, env *Env) {
+			WithOptions(
+				ProxyFiles(proxyWithLatest),
+			).Run(t, shouldUpdateDep, func(t *testing.T, env *Env) {
 				if vendoring {
 					env.RunGoCommandInDirWithEnv("a", []string{"GOWORK=off"}, "mod", "vendor")
 				}
 				env.AfterChange()
 				env.OpenFile("a/go.mod")
 				env.OpenFile("b/go.mod")
+
+				// Await the diagnostics resulting from opening the modfiles, because
+				// otherwise they may cause races when running asynchronously to the
+				// explicit re-diagnosing below.
+				//
+				// TODO(golang/go#58750): there is still a race here, inherent to
+				// accessing state on the View; we should create a new snapshot when
+				// the view diagnostics change.
+				env.AfterChange()
+
 				env.ExecuteCodeLensCommand("a/go.mod", command.CheckUpgrades, nil)
 				d := &protocol.PublishDiagnosticsParams{}
 				env.OnceMet(