internal/lsp/cache: tolerate analysis panics better

Panics in type error analyzers shouldn't block diagnostics.

Fixes golang/go#45075.

Change-Id: I897f0949551ab65276371f7ec8140ccb689e5a7b
Reviewed-on: https://go-review.googlesource.com/c/tools/+/302533
Trust: Heschi Kreinick <heschi@google.com>
Run-TryBot: Heschi Kreinick <heschi@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Heschi Kreinick 2021-03-17 13:24:21 -04:00
parent 9e9211a98e
commit 9b614f5d7b
2 changed files with 29 additions and 2 deletions

View File

@ -1885,3 +1885,30 @@ package main
)
})
}
// Tests golang/go#45075, a panic in fillreturns breaks diagnostics.
func TestFillReturnsPanic(t *testing.T) {
// At tip, the panic no longer reproduces.
testenv.SkipAfterGo1Point(t, 16)
const files = `
-- go.mod --
module mod.com
go 1.16
-- main.go --
package main
func foo() int {
return x, nil
}
`
Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("main.go")
env.Await(
env.DiagnosticAtRegexpWithMessage("main.go", `return x`, "wrong number of return values"),
LogMatching(protocol.Error, `.*analysis fillreturns.*panicked.*`, 2),
)
})
}

View File

@ -212,7 +212,6 @@ func runAnalysis(ctx context.Context, snapshot *snapshot, analyzer *analysis.Ana
}
defer func() {
if r := recover(); r != nil {
event.Log(ctx, fmt.Sprintf("analysis panicked: %s", r), tag.Package.Of(pkg.ID()))
data.err = errors.Errorf("analysis %s for package %s panicked: %v", analyzer.Name, pkg.PkgPath(), r)
}
}()
@ -408,7 +407,8 @@ func (s *snapshot) DiagnosePackage(ctx context.Context, spkg source.Package) (ma
var err error
errorAnalyzerDiag, err = s.Analyze(ctx, pkg.ID(), analyzers)
if err != nil {
return nil, err
// Keep going: analysis failures should not block diagnostics.
event.Error(ctx, "type error analysis failed", err, tag.Package.Of(pkg.ID()))
}
}
diags := map[span.URI][]*source.Diagnostic{}