internal/lsp: fix race in delivering diagnostics to the command line client

Fixes golang/go#32091

Change-Id: I1399a596169384f48d9f2409988226708dcd3473
Reviewed-on: https://go-review.googlesource.com/c/tools/+/177937
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
diff --git a/internal/lsp/cmd/check.go b/internal/lsp/cmd/check.go
index 59f0599..c5d9960 100644
--- a/internal/lsp/cmd/check.go
+++ b/internal/lsp/cmd/check.go
@@ -62,6 +62,8 @@
 		case <-time.Tick(30 * time.Second):
 			return fmt.Errorf("timed out waiting for results from %v", file.uri)
 		}
+		file.diagnosticsMu.Lock()
+		defer file.diagnosticsMu.Unlock()
 		for _, d := range file.diagnostics {
 			spn, err := file.mapper.RangeSpan(d.Range)
 			if err != nil {
diff --git a/internal/lsp/cmd/cmd.go b/internal/lsp/cmd/cmd.go
index 9b676c9..6c9ab2d 100644
--- a/internal/lsp/cmd/cmd.go
+++ b/internal/lsp/cmd/cmd.go
@@ -220,6 +220,7 @@
 	err            error
 	added          bool
 	hasDiagnostics chan struct{}
+	diagnosticsMu  sync.Mutex
 	diagnostics    []protocol.Diagnostic
 }
 
@@ -306,6 +307,8 @@
 	defer c.filesMu.Unlock()
 	uri := span.URI(p.URI)
 	file := c.getFile(ctx, uri)
+	file.diagnosticsMu.Lock()
+	defer file.diagnosticsMu.Unlock()
 	hadDiagnostics := file.diagnostics != nil
 	file.diagnostics = p.Diagnostics
 	if file.diagnostics == nil {