2018-11-12 12:15:47 -07:00
|
|
|
// Copyright 2018 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.
|
|
|
|
|
|
|
|
package source
|
|
|
|
|
|
|
|
import (
|
2018-12-18 13:46:14 -07:00
|
|
|
"bytes"
|
2018-11-12 12:15:47 -07:00
|
|
|
"context"
|
2018-12-18 13:46:14 -07:00
|
|
|
"fmt"
|
2019-05-17 11:45:50 -06:00
|
|
|
"strings"
|
2019-02-07 15:27:10 -07:00
|
|
|
|
2019-02-06 16:47:00 -07:00
|
|
|
"golang.org/x/tools/go/analysis"
|
2018-11-12 12:15:47 -07:00
|
|
|
"golang.org/x/tools/go/packages"
|
2019-08-14 18:12:18 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2019-07-14 21:08:10 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/telemetry"
|
2019-02-19 19:11:15 -07:00
|
|
|
"golang.org/x/tools/internal/span"
|
2019-08-13 13:07:39 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry/log"
|
|
|
|
"golang.org/x/tools/internal/telemetry/trace"
|
2019-08-14 18:12:18 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
2018-11-12 12:15:47 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type Diagnostic struct {
|
2019-08-14 18:12:18 -06:00
|
|
|
URI span.URI
|
|
|
|
Range protocol.Range
|
2019-02-27 16:08:04 -07:00
|
|
|
Message string
|
|
|
|
Source string
|
2019-09-24 22:46:57 -06:00
|
|
|
Severity protocol.DiagnosticSeverity
|
2019-09-24 14:28:59 -06:00
|
|
|
Tags []protocol.DiagnosticTag
|
2019-06-20 14:57:45 -06:00
|
|
|
|
2019-09-06 12:55:14 -06:00
|
|
|
SuggestedFixes []SuggestedFix
|
2019-06-20 14:57:45 -06:00
|
|
|
}
|
|
|
|
|
2019-02-27 16:08:04 -07:00
|
|
|
type DiagnosticSeverity int
|
|
|
|
|
|
|
|
const (
|
|
|
|
SeverityWarning DiagnosticSeverity = iota
|
|
|
|
SeverityError
|
|
|
|
)
|
|
|
|
|
2019-09-06 16:25:36 -06:00
|
|
|
func Diagnostics(ctx context.Context, view View, f GoFile, disabledAnalyses map[string]struct{}) (map[span.URI][]Diagnostic, string, error) {
|
2019-07-09 18:16:21 -06:00
|
|
|
ctx, done := trace.StartSpan(ctx, "source.Diagnostics", telemetry.File.Of(f.URI()))
|
|
|
|
defer done()
|
2019-07-09 15:52:23 -06:00
|
|
|
|
2019-09-09 17:26:26 -06:00
|
|
|
cphs, err := f.CheckPackageHandles(ctx)
|
2019-07-09 15:52:23 -06:00
|
|
|
if err != nil {
|
2019-09-06 16:25:36 -06:00
|
|
|
return nil, "", err
|
2019-07-09 15:52:23 -06:00
|
|
|
}
|
2019-09-17 15:19:48 -06:00
|
|
|
cph := WidestCheckPackageHandle(cphs)
|
2019-09-23 18:19:50 -06:00
|
|
|
|
|
|
|
// If we are missing dependencies, it may because the user's workspace is
|
|
|
|
// not correctly configured. Report errors, if possible.
|
|
|
|
var warningMsg string
|
|
|
|
if len(cph.MissingDependencies()) > 0 {
|
|
|
|
warningMsg, err = checkCommonErrors(ctx, view, f.URI())
|
|
|
|
if err != nil {
|
|
|
|
log.Error(ctx, "error checking common errors", err, telemetry.File.Of(f.URI))
|
|
|
|
}
|
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
pkg, err := cph.Check(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(ctx, "no package for file", err)
|
2019-09-06 16:25:36 -06:00
|
|
|
return singleDiagnostic(f.URI(), "%s is not part of a package", f.URI()), "", nil
|
2019-03-13 17:31:41 -06:00
|
|
|
}
|
2019-09-06 16:25:36 -06:00
|
|
|
|
2019-05-29 12:59:35 -06:00
|
|
|
// Prepare the reports we will send for the files in this package.
|
2019-02-19 19:11:15 -07:00
|
|
|
reports := make(map[span.URI][]Diagnostic)
|
2019-09-09 17:26:26 -06:00
|
|
|
for _, fh := range pkg.Files() {
|
2019-08-06 16:51:17 -06:00
|
|
|
clearReports(view, reports, fh.File().Identity().URI)
|
2018-11-12 12:15:47 -07:00
|
|
|
}
|
2019-05-17 11:45:50 -06:00
|
|
|
|
2019-05-29 12:59:35 -06:00
|
|
|
// Prepare any additional reports for the errors in this package.
|
2019-06-14 12:23:47 -06:00
|
|
|
for _, err := range pkg.GetErrors() {
|
|
|
|
if err.Kind != packages.ListError {
|
|
|
|
continue
|
|
|
|
}
|
2019-06-20 14:57:45 -06:00
|
|
|
clearReports(view, reports, packagesErrorSpan(err).URI())
|
2019-05-17 11:45:50 -06:00
|
|
|
}
|
|
|
|
|
2019-05-01 20:46:07 -06:00
|
|
|
// Run diagnostics for the package that this URI belongs to.
|
2019-06-21 15:00:02 -06:00
|
|
|
if !diagnostics(ctx, view, pkg, reports) {
|
2019-05-01 20:46:07 -06:00
|
|
|
// If we don't have any list, parse, or type errors, run analyses.
|
2019-07-09 15:52:23 -06:00
|
|
|
if err := analyses(ctx, view, cph, disabledAnalyses, reports); err != nil {
|
|
|
|
log.Error(ctx, "failed to run analyses", err, telemetry.File.Of(f.URI()))
|
2019-05-01 20:46:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Updates to the diagnostics for this package may need to be propagated.
|
2019-09-23 18:06:15 -06:00
|
|
|
revDeps := view.GetActiveReverseDeps(ctx, f.URI())
|
|
|
|
for _, cph := range revDeps {
|
2019-09-09 17:26:26 -06:00
|
|
|
pkg, err := cph.Check(ctx)
|
|
|
|
if err != nil {
|
2019-09-06 16:25:36 -06:00
|
|
|
return nil, warningMsg, err
|
2019-09-09 17:26:26 -06:00
|
|
|
}
|
|
|
|
for _, fh := range pkg.Files() {
|
2019-08-06 16:51:17 -06:00
|
|
|
clearReports(view, reports, fh.File().Identity().URI)
|
2019-05-01 20:46:07 -06:00
|
|
|
}
|
2019-06-21 15:00:02 -06:00
|
|
|
diagnostics(ctx, view, pkg, reports)
|
2019-05-01 20:46:07 -06:00
|
|
|
}
|
2019-09-06 16:25:36 -06:00
|
|
|
return reports, warningMsg, nil
|
2019-05-01 20:46:07 -06:00
|
|
|
}
|
|
|
|
|
2019-06-14 15:16:26 -06:00
|
|
|
type diagnosticSet struct {
|
2019-08-14 18:12:18 -06:00
|
|
|
listErrors, parseErrors, typeErrors []*Diagnostic
|
2019-06-14 15:16:26 -06:00
|
|
|
}
|
|
|
|
|
2019-08-02 17:45:56 -06:00
|
|
|
func diagnostics(ctx context.Context, view View, pkg Package, reports map[span.URI][]Diagnostic) bool {
|
2019-07-09 18:16:21 -06:00
|
|
|
ctx, done := trace.StartSpan(ctx, "source.diagnostics", telemetry.Package.Of(pkg.ID()))
|
|
|
|
defer done()
|
2019-08-02 17:45:56 -06:00
|
|
|
|
2019-06-14 15:16:26 -06:00
|
|
|
diagSets := make(map[span.URI]*diagnosticSet)
|
2019-03-06 14:33:47 -07:00
|
|
|
for _, err := range pkg.GetErrors() {
|
2019-08-14 18:12:18 -06:00
|
|
|
spn := packagesErrorSpan(err)
|
|
|
|
diag := &Diagnostic{
|
|
|
|
URI: spn.URI(),
|
2019-06-14 15:16:26 -06:00
|
|
|
Message: err.Msg,
|
|
|
|
Source: "LSP",
|
2019-09-24 22:46:57 -06:00
|
|
|
Severity: protocol.SeverityError,
|
2019-06-14 15:16:26 -06:00
|
|
|
}
|
2019-08-14 18:12:18 -06:00
|
|
|
set, ok := diagSets[diag.URI]
|
2019-06-14 15:16:26 -06:00
|
|
|
if !ok {
|
|
|
|
set = &diagnosticSet{}
|
2019-08-14 18:12:18 -06:00
|
|
|
diagSets[diag.URI] = set
|
2019-06-14 15:16:26 -06:00
|
|
|
}
|
2018-11-12 12:15:47 -07:00
|
|
|
switch err.Kind {
|
|
|
|
case packages.ParseError:
|
2019-06-14 15:16:26 -06:00
|
|
|
set.parseErrors = append(set.parseErrors, diag)
|
2018-11-12 12:15:47 -07:00
|
|
|
case packages.TypeError:
|
2019-06-14 15:16:26 -06:00
|
|
|
set.typeErrors = append(set.typeErrors, diag)
|
2018-11-12 12:15:47 -07:00
|
|
|
default:
|
2019-06-14 15:16:26 -06:00
|
|
|
set.listErrors = append(set.listErrors, diag)
|
2018-11-12 12:15:47 -07:00
|
|
|
}
|
2019-08-14 18:12:18 -06:00
|
|
|
rng, err := spanToRange(ctx, view, pkg, spn, err.Kind == packages.TypeError)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(ctx, "failed to convert span to range", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
diag.Range = rng
|
2018-11-12 12:15:47 -07:00
|
|
|
}
|
2019-06-14 15:16:26 -06:00
|
|
|
var nonEmptyDiagnostics bool // track if we actually send non-empty diagnostics
|
|
|
|
for uri, set := range diagSets {
|
|
|
|
// Don't report type errors if there are parse errors or list errors.
|
|
|
|
diags := set.typeErrors
|
|
|
|
if len(set.parseErrors) > 0 {
|
|
|
|
diags = set.parseErrors
|
|
|
|
} else if len(set.listErrors) > 0 {
|
|
|
|
diags = set.listErrors
|
2018-12-20 15:31:55 -07:00
|
|
|
}
|
2019-06-14 15:16:26 -06:00
|
|
|
if len(diags) > 0 {
|
|
|
|
nonEmptyDiagnostics = true
|
2018-11-12 12:15:47 -07:00
|
|
|
}
|
2019-06-14 15:16:26 -06:00
|
|
|
for _, diag := range diags {
|
|
|
|
if _, ok := reports[uri]; ok {
|
2019-08-14 18:12:18 -06:00
|
|
|
reports[uri] = append(reports[uri], *diag)
|
2019-06-14 15:16:26 -06:00
|
|
|
}
|
2018-11-12 12:15:47 -07:00
|
|
|
}
|
|
|
|
}
|
2019-06-14 15:16:26 -06:00
|
|
|
return nonEmptyDiagnostics
|
2019-05-01 20:46:07 -06:00
|
|
|
}
|
|
|
|
|
2019-08-14 18:12:18 -06:00
|
|
|
// spanToRange converts a span.Span to a protocol.Range,
|
|
|
|
// assuming that the span belongs to the package whose diagnostics are being computed.
|
|
|
|
func spanToRange(ctx context.Context, view View, pkg Package, spn span.Span, isTypeError bool) (protocol.Range, error) {
|
2019-09-17 09:19:11 -06:00
|
|
|
ph, err := pkg.File(spn.URI())
|
|
|
|
if err != nil {
|
|
|
|
return protocol.Range{}, err
|
2019-08-14 18:12:18 -06:00
|
|
|
}
|
2019-09-17 09:19:11 -06:00
|
|
|
_, m, _, err := ph.Cached(ctx)
|
|
|
|
if err != nil {
|
2019-08-14 18:12:18 -06:00
|
|
|
return protocol.Range{}, err
|
|
|
|
}
|
2019-09-17 09:19:11 -06:00
|
|
|
data, _, err := ph.File().Read(ctx)
|
2019-08-14 18:12:18 -06:00
|
|
|
if err != nil {
|
|
|
|
return protocol.Range{}, err
|
|
|
|
}
|
|
|
|
// Try to get a range for the diagnostic.
|
|
|
|
// TODO: Don't just limit ranges to type errors.
|
|
|
|
if spn.IsPoint() && isTypeError {
|
|
|
|
if s, err := spn.WithOffset(m.Converter); err == nil {
|
|
|
|
start := s.Start()
|
|
|
|
offset := start.Offset()
|
|
|
|
if width := bytes.IndexAny(data[offset:], " \n,():;[]"); width > 0 {
|
|
|
|
spn = span.New(spn.URI(), start, span.NewPoint(start.Line(), start.Column()+width, offset+width))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m.Range(spn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func analyses(ctx context.Context, view View, cph CheckPackageHandle, disabledAnalyses map[string]struct{}, reports map[span.URI][]Diagnostic) error {
|
2019-02-06 09:24:10 -07:00
|
|
|
// Type checking and parsing succeeded. Run analyses.
|
2019-08-14 18:12:18 -06:00
|
|
|
if err := runAnalyses(ctx, view, cph, disabledAnalyses, func(a *analysis.Analyzer, diag analysis.Diagnostic) error {
|
|
|
|
diagnostic, err := toDiagnostic(ctx, view, diag, a.Name)
|
2019-03-15 11:19:43 -06:00
|
|
|
if err != nil {
|
2019-04-22 15:05:43 -06:00
|
|
|
return err
|
2019-03-15 11:19:43 -06:00
|
|
|
}
|
2019-08-14 18:12:18 -06:00
|
|
|
addReport(view, reports, diagnostic.URI, diagnostic)
|
2019-04-22 15:05:43 -06:00
|
|
|
return nil
|
2019-04-25 13:50:50 -06:00
|
|
|
}); err != nil {
|
2019-05-01 20:46:07 -06:00
|
|
|
return err
|
2019-04-25 13:50:50 -06:00
|
|
|
}
|
2019-05-01 20:46:07 -06:00
|
|
|
return nil
|
2018-11-12 12:15:47 -07:00
|
|
|
}
|
|
|
|
|
2019-08-14 18:12:18 -06:00
|
|
|
func toDiagnostic(ctx context.Context, view View, diag analysis.Diagnostic, category string) (Diagnostic, error) {
|
|
|
|
r := span.NewRange(view.Session().Cache().FileSet(), diag.Pos, diag.End)
|
|
|
|
spn, err := r.Span()
|
2019-06-20 14:57:45 -06:00
|
|
|
if err != nil {
|
|
|
|
// The diagnostic has an invalid position, so we don't have a valid span.
|
|
|
|
return Diagnostic{}, err
|
|
|
|
}
|
|
|
|
if diag.Category != "" {
|
|
|
|
category += "." + category
|
|
|
|
}
|
2019-08-14 18:12:18 -06:00
|
|
|
f, err := view.GetFile(ctx, spn.URI())
|
|
|
|
if err != nil {
|
|
|
|
return Diagnostic{}, err
|
|
|
|
}
|
|
|
|
gof, ok := f.(GoFile)
|
|
|
|
if !ok {
|
|
|
|
return Diagnostic{}, errors.Errorf("%s is not a Go file", f.URI())
|
|
|
|
}
|
|
|
|
// If the package has changed since these diagnostics were computed,
|
|
|
|
// this may be incorrect. Should the package be associated with the diagnostic?
|
2019-09-09 17:26:26 -06:00
|
|
|
cphs, err := gof.CheckPackageHandles(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return Diagnostic{}, err
|
|
|
|
}
|
|
|
|
cph := NarrowestCheckPackageHandle(cphs)
|
|
|
|
pkg, err := cph.Cached(ctx)
|
2019-08-14 18:12:18 -06:00
|
|
|
if err != nil {
|
|
|
|
return Diagnostic{}, err
|
|
|
|
}
|
2019-09-24 14:28:59 -06:00
|
|
|
rng, err := spanToRange(ctx, view, pkg, spn, false)
|
2019-09-16 16:17:51 -06:00
|
|
|
if err != nil {
|
|
|
|
return Diagnostic{}, err
|
|
|
|
}
|
2019-09-24 14:28:59 -06:00
|
|
|
fixes, err := suggestedFixes(ctx, view, pkg, diag)
|
2019-06-20 14:57:45 -06:00
|
|
|
if err != nil {
|
|
|
|
return Diagnostic{}, err
|
|
|
|
}
|
2019-09-24 14:28:59 -06:00
|
|
|
// This is a bit of a hack, but clients > 3.15 will be able to grey out unnecessary code.
|
|
|
|
// If we are deleting code as part of all of our suggested fixes, assume that this is dead code.
|
|
|
|
// TODO(golang/go/#34508): Return these codes from the diagnostics themselves.
|
|
|
|
var tags []protocol.DiagnosticTag
|
|
|
|
if onlyDeletions(fixes) {
|
|
|
|
tags = append(tags, protocol.Unnecessary)
|
|
|
|
}
|
2019-06-20 14:57:45 -06:00
|
|
|
return Diagnostic{
|
2019-08-14 18:12:18 -06:00
|
|
|
URI: spn.URI(),
|
|
|
|
Range: rng,
|
2019-06-20 14:57:45 -06:00
|
|
|
Source: category,
|
|
|
|
Message: diag.Message,
|
2019-09-24 22:46:57 -06:00
|
|
|
Severity: protocol.SeverityWarning,
|
2019-09-24 14:28:59 -06:00
|
|
|
SuggestedFixes: fixes,
|
|
|
|
Tags: tags,
|
2019-06-20 14:57:45 -06:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func clearReports(v View, reports map[span.URI][]Diagnostic, uri span.URI) {
|
2019-05-31 22:08:57 -06:00
|
|
|
if v.Ignore(uri) {
|
|
|
|
return
|
|
|
|
}
|
2019-06-20 14:57:45 -06:00
|
|
|
reports[uri] = []Diagnostic{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func addReport(v View, reports map[span.URI][]Diagnostic, uri span.URI, diagnostic Diagnostic) {
|
|
|
|
if v.Ignore(uri) {
|
|
|
|
return
|
2019-05-31 22:08:57 -06:00
|
|
|
}
|
2019-08-14 18:12:18 -06:00
|
|
|
if _, ok := reports[uri]; ok {
|
|
|
|
reports[uri] = append(reports[uri], diagnostic)
|
|
|
|
}
|
2019-05-31 22:08:57 -06:00
|
|
|
}
|
|
|
|
|
2019-06-14 15:16:26 -06:00
|
|
|
func packagesErrorSpan(err packages.Error) span.Span {
|
|
|
|
if err.Pos == "" {
|
|
|
|
return parseDiagnosticMessage(err.Msg)
|
|
|
|
}
|
|
|
|
return span.Parse(err.Pos)
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseDiagnosticMessage 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:
|
|
|
|
//
|
2019-05-17 11:45:50 -06:00
|
|
|
// attributes.go:13:1: expected 'package', found 'type'
|
2019-06-14 15:16:26 -06:00
|
|
|
//
|
2019-05-17 11:45:50 -06:00
|
|
|
func parseDiagnosticMessage(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-03-29 14:14:24 -06:00
|
|
|
func singleDiagnostic(uri span.URI, format string, a ...interface{}) map[span.URI][]Diagnostic {
|
|
|
|
return map[span.URI][]Diagnostic{
|
|
|
|
uri: []Diagnostic{{
|
|
|
|
Source: "LSP",
|
2019-08-14 18:12:18 -06:00
|
|
|
URI: uri,
|
|
|
|
Range: protocol.Range{},
|
2019-03-29 14:14:24 -06:00
|
|
|
Message: fmt.Sprintf(format, a...),
|
2019-09-24 22:46:57 -06:00
|
|
|
Severity: protocol.SeverityError,
|
2019-03-29 14:14:24 -06:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-14 18:12:18 -06:00
|
|
|
func runAnalyses(ctx context.Context, view View, cph CheckPackageHandle, disabledAnalyses map[string]struct{}, report func(a *analysis.Analyzer, diag analysis.Diagnostic) error) error {
|
2019-05-31 21:31:58 -06:00
|
|
|
var analyzers []*analysis.Analyzer
|
2019-09-18 16:19:41 -06:00
|
|
|
for _, a := range view.Analyzers() {
|
2019-05-31 21:31:58 -06:00
|
|
|
if _, ok := disabledAnalyses[a.Name]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
analyzers = append(analyzers, a)
|
2019-02-06 09:24:10 -07:00
|
|
|
}
|
2019-02-07 15:27:10 -07:00
|
|
|
|
2019-08-14 18:12:18 -06:00
|
|
|
roots, err := analyze(ctx, view, []CheckPackageHandle{cph}, analyzers)
|
2019-04-22 15:05:43 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-07 15:27:10 -07:00
|
|
|
|
|
|
|
// Report diagnostics and errors from root analyzers.
|
2019-02-06 16:47:00 -07:00
|
|
|
for _, r := range roots {
|
2019-06-20 14:57:45 -06:00
|
|
|
var sdiags []Diagnostic
|
2019-02-06 16:47:00 -07:00
|
|
|
for _, diag := range r.diagnostics {
|
|
|
|
if r.err != nil {
|
|
|
|
// TODO(matloob): This isn't quite right: we might return a failed prerequisites error,
|
|
|
|
// which isn't super useful...
|
|
|
|
return r.err
|
|
|
|
}
|
2019-04-22 15:05:43 -06:00
|
|
|
if err := report(r.Analyzer, diag); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-14 18:12:18 -06:00
|
|
|
sdiag, err := toDiagnostic(ctx, view, diag, r.Analyzer.Name)
|
2019-06-20 14:57:45 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sdiags = append(sdiags, sdiag)
|
2019-02-06 09:24:10 -07:00
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
pkg, err := cph.Check(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-09-04 11:16:09 -06:00
|
|
|
pkg.SetDiagnostics(r.Analyzer, sdiags)
|
2019-02-06 09:24:10 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|