2019-12-17 13:43:36 -07:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2019-10-20 17:57:03 -06:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-04-07 20:54:42 -06:00
|
|
|
"fmt"
|
2019-10-20 17:57:03 -06:00
|
|
|
"go/scanner"
|
2019-10-21 15:49:45 -06:00
|
|
|
"go/token"
|
2019-10-20 17:57:03 -06:00
|
|
|
"go/types"
|
2019-12-11 12:30:48 -07:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2019-10-20 17:57:03 -06:00
|
|
|
"strings"
|
|
|
|
|
2019-10-21 15:25:09 -06:00
|
|
|
"golang.org/x/tools/go/analysis"
|
2019-10-20 17:57:03 -06:00
|
|
|
"golang.org/x/tools/go/packages"
|
2020-03-10 08:14:56 -06:00
|
|
|
"golang.org/x/tools/internal/analysisinternal"
|
2020-04-17 07:32:56 -06:00
|
|
|
"golang.org/x/tools/internal/event"
|
2020-03-10 21:09:39 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/debug/tag"
|
2019-10-20 17:57:03 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
|
|
"golang.org/x/tools/internal/span"
|
2019-10-21 15:49:45 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-10-20 17:57:03 -06:00
|
|
|
)
|
|
|
|
|
2019-10-29 16:13:19 -06:00
|
|
|
func sourceError(ctx context.Context, fset *token.FileSet, pkg *pkg, e interface{}) (*source.Error, error) {
|
2019-10-20 17:57:03 -06:00
|
|
|
var (
|
2019-10-21 15:25:09 -06:00
|
|
|
spn span.Span
|
|
|
|
err error
|
|
|
|
msg, category string
|
|
|
|
kind source.ErrorKind
|
|
|
|
fixes []source.SuggestedFix
|
|
|
|
related []source.RelatedInformation
|
2019-10-20 17:57:03 -06:00
|
|
|
)
|
|
|
|
switch e := e.(type) {
|
|
|
|
case packages.Error:
|
2019-12-11 12:30:48 -07:00
|
|
|
kind = toSourceErrorKind(e.Kind)
|
|
|
|
var ok bool
|
2020-01-25 17:58:28 -07:00
|
|
|
if msg, spn, ok = parseGoListImportCycleError(ctx, fset, e, pkg); ok {
|
|
|
|
kind = source.TypeError
|
2019-12-11 12:30:48 -07:00
|
|
|
break
|
|
|
|
}
|
2019-10-20 17:57:03 -06:00
|
|
|
if e.Pos == "" {
|
|
|
|
spn = parseGoListError(e.Msg)
|
2020-01-21 18:47:39 -07:00
|
|
|
|
|
|
|
// We may not have been able to parse a valid span.
|
2020-03-27 10:52:40 -06:00
|
|
|
if _, err := spanToRange(pkg, spn); err != nil {
|
2020-01-21 18:47:39 -07:00
|
|
|
return &source.Error{
|
|
|
|
URI: spn.URI(),
|
|
|
|
Message: msg,
|
|
|
|
Kind: kind,
|
|
|
|
}, nil
|
|
|
|
}
|
2019-10-20 17:57:03 -06:00
|
|
|
} else {
|
|
|
|
spn = span.Parse(e.Pos)
|
|
|
|
}
|
|
|
|
case *scanner.Error:
|
|
|
|
msg = e.Msg
|
2019-10-21 15:25:09 -06:00
|
|
|
kind = source.ParseError
|
2020-03-27 10:52:40 -06:00
|
|
|
spn, err = scannerErrorRange(fset, pkg, e.Pos)
|
2019-10-21 15:49:45 -06:00
|
|
|
if err != nil {
|
2020-03-30 10:45:15 -06:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
2020-03-10 21:09:39 -06:00
|
|
|
event.Error(ctx, "no span for scanner.Error pos", err, tag.Package.Of(pkg.ID()))
|
2019-11-11 16:27:04 -07:00
|
|
|
spn = span.Parse(e.Pos.String())
|
2019-10-21 15:49:45 -06:00
|
|
|
}
|
|
|
|
|
2019-10-20 17:57:03 -06:00
|
|
|
case scanner.ErrorList:
|
|
|
|
// The first parser error is likely the root cause of the problem.
|
2019-10-21 15:49:45 -06:00
|
|
|
if e.Len() <= 0 {
|
|
|
|
return nil, errors.Errorf("no errors in %v", e)
|
2019-10-20 17:57:03 -06:00
|
|
|
}
|
2019-10-21 15:49:45 -06:00
|
|
|
msg = e[0].Msg
|
|
|
|
kind = source.ParseError
|
2020-03-27 10:52:40 -06:00
|
|
|
spn, err = scannerErrorRange(fset, pkg, e[0].Pos)
|
2019-10-21 15:49:45 -06:00
|
|
|
if err != nil {
|
2020-03-30 10:45:15 -06:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
2020-03-10 21:09:39 -06:00
|
|
|
event.Error(ctx, "no span for scanner.Error pos", err, tag.Package.Of(pkg.ID()))
|
2019-11-11 16:27:04 -07:00
|
|
|
spn = span.Parse(e[0].Pos.String())
|
2019-10-21 15:49:45 -06:00
|
|
|
}
|
2019-10-20 17:57:03 -06:00
|
|
|
case types.Error:
|
|
|
|
msg = e.Msg
|
2019-10-21 15:25:09 -06:00
|
|
|
kind = source.TypeError
|
2020-04-07 20:54:42 -06:00
|
|
|
if !e.Pos.IsValid() {
|
|
|
|
return nil, fmt.Errorf("invalid position for type error %v", e)
|
|
|
|
}
|
2019-10-21 15:49:45 -06:00
|
|
|
spn, err = typeErrorRange(ctx, fset, pkg, e.Pos)
|
2019-10-22 15:27:17 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-21 15:49:45 -06:00
|
|
|
|
2019-10-21 15:25:09 -06:00
|
|
|
case *analysis.Diagnostic:
|
2019-10-23 14:10:24 -06:00
|
|
|
spn, err = span.NewRange(fset, e.Pos, e.End).Span()
|
2019-10-21 15:25:09 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
msg = e.Message
|
|
|
|
kind = source.Analysis
|
|
|
|
category = e.Category
|
2020-03-27 10:52:40 -06:00
|
|
|
fixes, err = suggestedFixes(fset, pkg, e)
|
2019-10-21 15:25:09 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-27 10:52:40 -06:00
|
|
|
related, err = relatedInformation(fset, pkg, e)
|
2019-10-21 15:25:09 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-20 17:57:03 -06:00
|
|
|
}
|
2020-03-27 10:52:40 -06:00
|
|
|
rng, err := spanToRange(pkg, spn)
|
2019-10-20 17:57:03 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &source.Error{
|
2020-01-13 17:43:43 -07:00
|
|
|
URI: spn.URI(),
|
2019-10-21 15:25:09 -06:00
|
|
|
Range: rng,
|
|
|
|
Message: msg,
|
|
|
|
Kind: kind,
|
|
|
|
Category: category,
|
|
|
|
SuggestedFixes: fixes,
|
|
|
|
Related: related,
|
2019-10-20 17:57:03 -06:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-03-27 10:52:40 -06:00
|
|
|
func suggestedFixes(fset *token.FileSet, pkg *pkg, diag *analysis.Diagnostic) ([]source.SuggestedFix, error) {
|
2019-10-21 15:25:09 -06:00
|
|
|
var fixes []source.SuggestedFix
|
|
|
|
for _, fix := range diag.SuggestedFixes {
|
|
|
|
edits := make(map[span.URI][]protocol.TextEdit)
|
|
|
|
for _, e := range fix.TextEdits {
|
2019-10-23 14:10:24 -06:00
|
|
|
spn, err := span.NewRange(fset, e.Pos, e.End).Span()
|
2019-10-21 15:25:09 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-27 10:52:40 -06:00
|
|
|
rng, err := spanToRange(pkg, spn)
|
2019-10-21 15:25:09 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
edits[spn.URI()] = append(edits[spn.URI()], protocol.TextEdit{
|
|
|
|
Range: rng,
|
|
|
|
NewText: string(e.NewText),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
fixes = append(fixes, source.SuggestedFix{
|
|
|
|
Title: fix.Message,
|
|
|
|
Edits: edits,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return fixes, nil
|
|
|
|
}
|
|
|
|
|
2020-03-27 10:52:40 -06:00
|
|
|
func relatedInformation(fset *token.FileSet, pkg *pkg, diag *analysis.Diagnostic) ([]source.RelatedInformation, error) {
|
2019-10-21 15:25:09 -06:00
|
|
|
var out []source.RelatedInformation
|
|
|
|
for _, related := range diag.Related {
|
2019-10-23 14:10:24 -06:00
|
|
|
spn, err := span.NewRange(fset, related.Pos, related.End).Span()
|
2019-10-21 15:25:09 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-27 10:52:40 -06:00
|
|
|
rng, err := spanToRange(pkg, spn)
|
2019-10-21 15:25:09 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
out = append(out, source.RelatedInformation{
|
|
|
|
URI: spn.URI(),
|
|
|
|
Range: rng,
|
|
|
|
Message: related.Message,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toSourceErrorKind(kind packages.ErrorKind) source.ErrorKind {
|
|
|
|
switch kind {
|
|
|
|
case packages.ListError:
|
|
|
|
return source.ListError
|
|
|
|
case packages.ParseError:
|
|
|
|
return source.ParseError
|
|
|
|
case packages.TypeError:
|
|
|
|
return source.TypeError
|
|
|
|
default:
|
|
|
|
return source.UnknownError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-21 15:49:45 -06:00
|
|
|
func typeErrorRange(ctx context.Context, fset *token.FileSet, pkg *pkg, pos token.Pos) (span.Span, error) {
|
|
|
|
posn := fset.Position(pos)
|
2020-02-12 14:36:46 -07:00
|
|
|
ph, _, err := source.FindFileInPackage(pkg, span.URIFromPath(posn.Filename))
|
2019-10-21 15:49:45 -06:00
|
|
|
if err != nil {
|
2019-12-30 12:41:33 -07:00
|
|
|
return span.Span{}, err
|
2019-10-21 15:49:45 -06:00
|
|
|
}
|
2020-02-10 21:10:59 -07:00
|
|
|
_, _, m, _, err := ph.Cached()
|
2019-10-21 15:49:45 -06:00
|
|
|
if err != nil {
|
2019-12-30 12:41:33 -07:00
|
|
|
return span.Span{}, err
|
|
|
|
}
|
2019-10-21 15:49:45 -06:00
|
|
|
if err != nil {
|
2019-12-30 12:41:33 -07:00
|
|
|
return span.Span{}, err
|
2019-10-21 15:49:45 -06:00
|
|
|
}
|
|
|
|
data, _, err := ph.File().Read(ctx)
|
|
|
|
if err != nil {
|
2019-12-30 12:41:33 -07:00
|
|
|
return span.Span{}, err
|
2019-10-21 15:49:45 -06:00
|
|
|
}
|
2020-03-10 08:14:56 -06:00
|
|
|
return span.Range{
|
|
|
|
FileSet: fset,
|
|
|
|
Start: pos,
|
|
|
|
End: analysisinternal.TypeErrorEndPos(fset, data, pos),
|
|
|
|
Converter: m.Converter,
|
|
|
|
}.Span()
|
2019-10-21 15:49:45 -06:00
|
|
|
}
|
|
|
|
|
2020-03-27 10:52:40 -06:00
|
|
|
func scannerErrorRange(fset *token.FileSet, pkg *pkg, posn token.Position) (span.Span, error) {
|
2020-02-12 14:36:46 -07:00
|
|
|
ph, _, err := source.FindFileInPackage(pkg, span.URIFromPath(posn.Filename))
|
2019-10-21 15:49:45 -06:00
|
|
|
if err != nil {
|
|
|
|
return span.Span{}, err
|
|
|
|
}
|
2020-02-10 21:10:59 -07:00
|
|
|
file, _, _, _, err := ph.Cached()
|
2019-10-21 15:49:45 -06:00
|
|
|
if err != nil {
|
|
|
|
return span.Span{}, err
|
|
|
|
}
|
|
|
|
tok := fset.File(file.Pos())
|
|
|
|
if tok == nil {
|
|
|
|
return span.Span{}, errors.Errorf("no token.File for %s", ph.File().Identity().URI)
|
|
|
|
}
|
|
|
|
pos := tok.Pos(posn.Offset)
|
|
|
|
return span.NewRange(fset, pos, pos).Span()
|
|
|
|
}
|
|
|
|
|
2019-10-20 17:57:03 -06:00
|
|
|
// spanToRange converts a span.Span to a protocol.Range,
|
|
|
|
// assuming that the span belongs to the package whose diagnostics are being computed.
|
2020-03-27 10:52:40 -06:00
|
|
|
func spanToRange(pkg *pkg, spn span.Span) (protocol.Range, error) {
|
2020-02-06 13:43:39 -07:00
|
|
|
ph, _, err := source.FindFileInPackage(pkg, spn.URI())
|
2019-10-20 17:57:03 -06:00
|
|
|
if err != nil {
|
|
|
|
return protocol.Range{}, err
|
|
|
|
}
|
2020-02-10 21:10:59 -07:00
|
|
|
_, _, m, _, err := ph.Cached()
|
2019-10-20 17:57:03 -06:00
|
|
|
if err != nil {
|
|
|
|
return protocol.Range{}, err
|
|
|
|
}
|
|
|
|
return m.Range(spn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseGoListError attempts to parse a standard `go list` error message
|
|
|
|
// by stripping off the trailing error message.
|
|
|
|
//
|
|
|
|
// It works only on errors whose message is prefixed by colon,
|
|
|
|
// followed by a space (": "). For example:
|
|
|
|
//
|
|
|
|
// attributes.go:13:1: expected 'package', found 'type'
|
|
|
|
//
|
|
|
|
func parseGoListError(input string) span.Span {
|
|
|
|
input = strings.TrimSpace(input)
|
|
|
|
msgIndex := strings.Index(input, ": ")
|
|
|
|
if msgIndex < 0 {
|
|
|
|
return span.Parse(input)
|
|
|
|
}
|
|
|
|
return span.Parse(input[:msgIndex])
|
|
|
|
}
|
2019-12-11 12:30:48 -07:00
|
|
|
|
|
|
|
func parseGoListImportCycleError(ctx context.Context, fset *token.FileSet, e packages.Error, pkg *pkg) (string, span.Span, bool) {
|
|
|
|
re := regexp.MustCompile(`(.*): import stack: \[(.+)\]`)
|
|
|
|
matches := re.FindStringSubmatch(strings.TrimSpace(e.Msg))
|
|
|
|
if len(matches) < 3 {
|
|
|
|
return e.Msg, span.Span{}, false
|
|
|
|
}
|
|
|
|
msg := matches[1]
|
|
|
|
importList := strings.Split(matches[2], " ")
|
|
|
|
// Since the error is relative to the current package. The import that is causing
|
|
|
|
// the import cycle error is the second one in the list.
|
|
|
|
if len(importList) < 2 {
|
|
|
|
return msg, span.Span{}, false
|
|
|
|
}
|
|
|
|
// Imports have quotation marks around them.
|
|
|
|
circImp := strconv.Quote(importList[1])
|
|
|
|
for _, ph := range pkg.compiledGoFiles {
|
2020-02-10 21:10:59 -07:00
|
|
|
fh, _, _, _, err := ph.Parse(ctx)
|
2019-12-11 12:30:48 -07:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Search file imports for the import that is causing the import cycle.
|
|
|
|
for _, imp := range fh.Imports {
|
|
|
|
if imp.Path.Value == circImp {
|
|
|
|
spn, err := span.NewRange(fset, imp.Pos(), imp.End()).Span()
|
|
|
|
if err != nil {
|
|
|
|
return msg, span.Span{}, false
|
|
|
|
}
|
|
|
|
return msg, spn, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return msg, span.Span{}, false
|
|
|
|
}
|