From f41d27ef3ace9e6edd63207b6a566cc2dc7b552c Mon Sep 17 00:00:00 2001 From: Robert Findley Date: Mon, 8 Apr 2024 13:47:47 -0400 Subject: [PATCH] gopls/internal/cache: avoid panic when the primary diagnostic is broken If the primary diagnostic has invalid position information (due to a bug in the parser or AST fix logic), gopls may panics when linking primary and related information. Avoid this panic. Fixes golang/go#66731 Change-Id: Ie2f95d158a1c93d00603a7ce4d38d8097bd8cb08 Reviewed-on: https://go-review.googlesource.com/c/tools/+/577259 LUCI-TryBot-Result: Go LUCI Reviewed-by: Alan Donovan --- gopls/internal/cache/check.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/gopls/internal/cache/check.go b/gopls/internal/cache/check.go index dee4eec47e5..2978d14b46e 100644 --- a/gopls/internal/cache/check.go +++ b/gopls/internal/cache/check.go @@ -1968,7 +1968,10 @@ func typeErrorsToDiagnostics(pkg *syntaxPackage, errs []types.Error, linkTarget // This is because go/types assumes that errors are read top-down, such as // in the cycle error "A refers to...". The structure of the secondary // error set likely only makes sense for the primary error. - if i > 0 { + // + // NOTE: len(diags) == 0 if the primary diagnostic has invalid positions. + // See also golang/go#66731. + if i > 0 && len(diags) > 0 { primary := diags[0] primary.Related = append(primary.Related, protocol.DiagnosticRelatedInformation{ Location: protocol.Location{URI: diag.URI, Range: diag.Range},