internal/lsp: parse filenames only out of go list errors

This changes the packageErrorSpan function into the listErrorSpan.
Previously, this was causing the gopls-generated errors to get parsed,
which would result in attempts to send diagnostics for invalid filenames.

Fixes golang/go#32603

Change-Id: I7a54ed8884b78beb3f894598f18a24ed232f7412
Reviewed-on: https://go-review.googlesource.com/c/tools/+/182460
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
Rebecca Stambler 2019-06-14 14:23:47 -04:00
parent 61e0f78580
commit 9a3b5d688f
3 changed files with 12 additions and 9 deletions

View File

@ -83,13 +83,13 @@ func (v *view) checkMetadata(ctx context.Context, f *goFile) ([]packages.Error,
pkgs, err := packages.Load(v.buildConfig(), fmt.Sprintf("file=%s", f.filename())) pkgs, err := packages.Load(v.buildConfig(), fmt.Sprintf("file=%s", f.filename()))
if len(pkgs) == 0 { if len(pkgs) == 0 {
if err == nil { if err == nil {
err = fmt.Errorf("no packages found for %s", f.filename()) err = fmt.Errorf("go/packages.Load: no packages found for %s", f.filename())
} }
// Return this error as a diagnostic to the user. // Return this error as a diagnostic to the user.
return []packages.Error{ return []packages.Error{
{ {
Msg: err.Error(), Msg: err.Error(),
Kind: packages.ListError, Kind: packages.UnknownError,
}, },
}, err }, err
} }

View File

@ -42,6 +42,7 @@ func (s *Server) Diagnostics(ctx context.Context, v source.View, uri span.URI) {
if s.undelivered == nil { if s.undelivered == nil {
s.undelivered = make(map[span.URI][]source.Diagnostic) s.undelivered = make(map[span.URI][]source.Diagnostic)
} }
s.session.Logger().Errorf(ctx, "failed to deliver diagnostic for %s (will retry): %v", uri, err)
s.undelivered[uri] = diagnostics s.undelivered[uri] = diagnostics
continue continue
} }
@ -51,9 +52,8 @@ func (s *Server) Diagnostics(ctx context.Context, v source.View, uri span.URI) {
// Anytime we compute diagnostics, make sure to also send along any // Anytime we compute diagnostics, make sure to also send along any
// undelivered ones (only for remaining URIs). // undelivered ones (only for remaining URIs).
for uri, diagnostics := range s.undelivered { for uri, diagnostics := range s.undelivered {
err := s.publishDiagnostics(ctx, v, uri, diagnostics) if err := s.publishDiagnostics(ctx, v, uri, diagnostics); err != nil {
if err != nil { s.session.Logger().Errorf(ctx, "failed to deliver diagnostic for %s (will not retry): %v", uri, err)
s.session.Logger().Errorf(ctx, "failed to deliver diagnostic for %s: %v", uri, err)
} }
// If we fail to deliver the same diagnostics twice, just give up. // If we fail to deliver the same diagnostics twice, just give up.
delete(s.undelivered, uri) delete(s.undelivered, uri)

View File

@ -63,8 +63,11 @@ func Diagnostics(ctx context.Context, v View, f GoFile, disabledAnalyses map[str
} }
// Prepare any additional reports for the errors in this package. // Prepare any additional reports for the errors in this package.
for _, pkgErr := range pkg.GetErrors() { for _, err := range pkg.GetErrors() {
addReport(v, reports, packageErrorSpan(pkgErr).URI(), nil) if err.Kind != packages.ListError {
continue
}
addReport(v, reports, listErrorSpan(err).URI(), nil)
} }
// Run diagnostics for the package that this URI belongs to. // Run diagnostics for the package that this URI belongs to.
@ -108,7 +111,7 @@ func diagnostics(ctx context.Context, v View, pkg Package, reports map[span.URI]
diags = listErrors diags = listErrors
} }
for _, diag := range diags { for _, diag := range diags {
spn := packageErrorSpan(diag) spn := listErrorSpan(diag)
if spn.IsPoint() && diag.Kind == packages.TypeError { if spn.IsPoint() && diag.Kind == packages.TypeError {
spn = pointToSpan(ctx, v, spn) spn = pointToSpan(ctx, v, spn)
} }
@ -178,7 +181,7 @@ func parseDiagnosticMessage(input string) span.Span {
return span.Parse(input[:msgIndex]) return span.Parse(input[:msgIndex])
} }
func packageErrorSpan(pkgErr packages.Error) span.Span { func listErrorSpan(pkgErr packages.Error) span.Span {
if pkgErr.Pos == "" { if pkgErr.Pos == "" {
return parseDiagnosticMessage(pkgErr.Msg) return parseDiagnosticMessage(pkgErr.Msg)
} }