Skip to content

Commit

Permalink
gopls/internal/cache: avoid panic when the primary diagnostic is broken
Browse files Browse the repository at this point in the history
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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
  • Loading branch information
findleyr committed Apr 8, 2024
1 parent c0ab013 commit f41d27e
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion gopls/internal/cache/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit f41d27e

Please sign in to comment.