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
|
|
|
)
|
|
|
|
|
2020-07-16 15:37:12 -06:00
|
|
|
func sourceError(ctx context.Context, snapshot *snapshot, pkg *pkg, e interface{}) (*source.Error, error) {
|
|
|
|
fset := snapshot.view.session.cache.fset
|
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-07-16 15:37:12 -06:00
|
|
|
if msg, spn, ok = parseGoListImportCycleError(ctx, snapshot, e, pkg); ok {
|
2020-01-25 17:58:28 -07:00
|
|
|
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
|
|
|
|
}
|
internal/lsp: read files eagerly
We use file identities pervasively throughout gopls. Prior to this
change, the identity is the modification date of an unopened file, or
the hash of an opened file. That means that opening a file changes its
identity, which causes unnecessary churn in the cache.
Unfortunately, there isn't an easy way to fix this. Changing the
cache key to something else, such as the modification time, means that
we won't unify cache entries if a change is made and then undone. The
approach here is to read files eagerly in GetFile, so that we know their
hashes immediately. That resolves the churn, but means that we do a ton
of file IO at startup.
Incidental changes:
Remove the FileSystem interface; there was only one implementation and
it added a fair amount of cruft. We have many other places that assume
os.Stat and such work.
Add direct accessors to FileHandle for URI, Kind, and Version. Most uses
of (FileHandle).Identity were for stuff that we derive solely from the
URI, and this helped me disentangle them. It is a *ton* of churn,
though. I can revert it if you want.
Change-Id: Ia2133bc527f71daf81c9d674951726a232ca5bc9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237037
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-06-08 13:21:24 -06:00
|
|
|
data, err := ph.File().Read()
|
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-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-07-20 00:32:34 -06:00
|
|
|
pgh, _, err := source.FindFileInPackage(pkg, span.URIFromPath(posn.Filename))
|
2019-10-21 15:49:45 -06:00
|
|
|
if err != nil {
|
|
|
|
return span.Span{}, err
|
|
|
|
}
|
2020-07-20 00:32:34 -06:00
|
|
|
data, err := pgh.(*parseGoHandle).cached()
|
|
|
|
if data.tok == nil {
|
|
|
|
if err != nil {
|
|
|
|
return span.Span{}, err
|
|
|
|
}
|
|
|
|
return span.Span{}, errors.Errorf("no token.File for %s: %v", pgh.File().URI(), data.err)
|
2019-10-21 15:49:45 -06:00
|
|
|
}
|
2020-07-20 00:32:34 -06:00
|
|
|
pos := data.tok.Pos(posn.Offset)
|
2019-10-21 15:49:45 -06:00
|
|
|
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-07-20 00:32:34 -06:00
|
|
|
pgh, _, err := source.FindFileInPackage(pkg, spn.URI())
|
2019-10-20 17:57:03 -06:00
|
|
|
if err != nil {
|
|
|
|
return protocol.Range{}, err
|
|
|
|
}
|
2020-07-20 00:32:34 -06:00
|
|
|
_, _, m, _, err := pgh.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
|
|
|
|
2020-07-16 15:37:12 -06:00
|
|
|
func parseGoListImportCycleError(ctx context.Context, snapshot *snapshot, e packages.Error, pkg *pkg) (string, span.Span, bool) {
|
2019-12-11 12:30:48 -07:00
|
|
|
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-07-16 15:37:12 -06:00
|
|
|
fh, _, _, _, err := ph.Parse(ctx, snapshot.view)
|
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 {
|
2020-07-16 15:37:12 -06:00
|
|
|
spn, err := span.NewRange(snapshot.view.session.cache.fset, imp.Pos(), imp.End()).Span()
|
2019-12-11 12:30:48 -07:00
|
|
|
if err != nil {
|
|
|
|
return msg, span.Span{}, false
|
|
|
|
}
|
|
|
|
return msg, spn, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return msg, span.Span{}, false
|
|
|
|
}
|