internal/lsp: fix race condition in diagnostics show message

Detected on CL 263984. Logs:
https://storage.googleapis.com/go-build-log/62f18d07/linux-amd64-race_c6e9f2a0.log.

Change-Id: I07deb768eea43a277d67fbe72cabed885af41b93
Reviewed-on: https://go-review.googlesource.com/c/tools/+/264202
Trust: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
diff --git a/internal/lsp/diagnostics.go b/internal/lsp/diagnostics.go
index 7469a36..cd0f408 100644
--- a/internal/lsp/diagnostics.go
+++ b/internal/lsp/diagnostics.go
@@ -206,8 +206,9 @@
 		return nil, nil
 	}
 	var (
-		showMsg *protocol.ShowMessageParams
-		wg      sync.WaitGroup
+		showMsgMu sync.Mutex
+		showMsg   *protocol.ShowMessageParams
+		wg        sync.WaitGroup
 	)
 	for _, pkg := range wsPkgs {
 		wg.Add(1)
@@ -236,10 +237,12 @@
 			// Check if might want to warn the user about their build configuration.
 			// Our caller decides whether to send the message.
 			if warn && !snapshot.ValidBuildConfiguration() {
+				showMsgMu.Lock()
 				showMsg = &protocol.ShowMessageParams{
 					Type:    protocol.Warning,
 					Message: `You are neither in a module nor in your GOPATH. If you are using modules, please open your editor to a directory in your module. If you believe this warning is incorrect, please file an issue: https://github.com/golang/go/issues/new.`,
 				}
+				showMsgMu.Unlock()
 			}
 			if err != nil {
 				event.Error(ctx, "warning: diagnose package", err, tag.Snapshot.Of(snapshot.ID()), tag.Package.Of(pkg.ID()))