Skip to content

Instantly share code, notes, and snippets.

@kylelemons
Created August 26, 2019 07:19
Show Gist options
  • Save kylelemons/4e1c6309716507bd30884fd3645c9b60 to your computer and use it in GitHub Desktop.
Save kylelemons/4e1c6309716507bd30884fd3645c9b60 to your computer and use it in GitHub Desktop.
diff --git a/internal/lsp/source/analysis.go b/internal/lsp/source/analysis.go
index e9c9ecc1..cea1e548 100644
--- a/internal/lsp/source/analysis.go
+++ b/internal/lsp/source/analysis.go
@@ -16,6 +16,7 @@ import (
"strings"
"sync"
"time"
+ "runtime/debug"
"golang.org/x/sync/errgroup"
"golang.org/x/tools/go/analysis"
@@ -171,14 +172,21 @@ func (act *Action) execOnce(ctx context.Context, fset *token.FileSet) error {
if act.Pkg.IsIllTyped() {
act.err = errors.Errorf("analysis skipped due to errors in package: %v", act.Pkg.GetErrors())
} else {
- act.result, act.err = pass.Analyzer.Run(pass)
- if act.err == nil {
- if got, want := reflect.TypeOf(act.result), pass.Analyzer.ResultType; got != want {
- act.err = errors.Errorf(
- "internal error: on package %s, analyzer %s returned a result of type %v, but declared ResultType %v",
- pass.Pkg.Path(), pass.Analyzer, got, want)
+ func() {
+ defer func() {
+ if r := recover(); r != nil {
+ act.err = fmt.Errorf("panic during analysis %q: %s\n%s", pass.Analyzer.Name, r, debug.Stack())
+ }
+ }()
+ act.result, act.err = pass.Analyzer.Run(pass)
+ if act.err == nil {
+ if got, want := reflect.TypeOf(act.result), pass.Analyzer.ResultType; got != want {
+ act.err = errors.Errorf(
+ "internal error: on package %s, analyzer %s returned a result of type %v, but declared ResultType %v",
+ pass.Pkg.Path(), pass.Analyzer, got, want)
+ }
}
- }
+ }()
}
// disallow calls after Run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment