internal/lsp: propagate errors from in diagnostics

An ignored error in toProtocolDiagnostics could result in empty
diagnostics being (incorrectly) sent to the user.

Change-Id: I34c86a1f5bbf28888bedad094f596cc27a52b86d
Reviewed-on: https://go-review.googlesource.com/c/tools/+/167714
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
Rebecca Stambler 2019-03-14 16:55:23 -04:00
parent 1286b2016b
commit 8781451fe3
2 changed files with 12 additions and 7 deletions

View File

@ -27,8 +27,12 @@ func (s *server) cacheAndDiagnose(ctx context.Context, uri span.URI, content str
return // handle error?
}
for uri, diagnostics := range reports {
protocolDiagnostics, err := toProtocolDiagnostics(ctx, s.view, diagnostics)
if err != nil {
continue // handle errors?
}
s.client.PublishDiagnostics(ctx, &protocol.PublishDiagnosticsParams{
Diagnostics: toProtocolDiagnostics(ctx, s.view, diagnostics),
Diagnostics: protocolDiagnostics,
URI: protocol.NewURI(uri),
})
}
@ -40,14 +44,12 @@ func (s *server) setContent(ctx context.Context, uri span.URI, content []byte) e
return s.view.SetContent(ctx, uri, content)
}
func toProtocolDiagnostics(ctx context.Context, v source.View, diagnostics []source.Diagnostic) []protocol.Diagnostic {
func toProtocolDiagnostics(ctx context.Context, v source.View, diagnostics []source.Diagnostic) ([]protocol.Diagnostic, error) {
reports := []protocol.Diagnostic{}
for _, diag := range diagnostics {
_, m, err := newColumnMap(ctx, v, diag.Span.URI)
if err != nil {
//TODO: if we can't find the file we cannot map
//the diagnostic, but also this should never happen
continue
return nil, err
}
src := diag.Source
if src == "" {
@ -67,7 +69,7 @@ func toProtocolDiagnostics(ctx context.Context, v source.View, diagnostics []sou
Source: src,
})
}
return reports
return reports, nil
}
func sorted(d []protocol.Diagnostic) {

View File

@ -162,7 +162,10 @@ func (d diagnostics) test(t *testing.T, v source.View) int {
if err != nil {
t.Fatal(err)
}
got := toProtocolDiagnostics(ctx, v, sourceDiagnostics[uri])
got, err := toProtocolDiagnostics(ctx, v, sourceDiagnostics[uri])
if err != nil {
t.Fatal(err)
}
sorted(got)
if diff := diffDiagnostics(uri, want, got); diff != "" {
t.Error(diff)