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 (
|
|
|
|
"context"
|
2018-12-18 13:46:14 -07:00
|
|
|
"fmt"
|
2019-02-07 15:27:10 -07:00
|
|
|
|
2019-02-06 16:47:00 -07:00
|
|
|
"golang.org/x/tools/go/analysis"
|
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"
|
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-10-11 03:39:09 -06:00
|
|
|
Related []RelatedInformation
|
|
|
|
}
|
|
|
|
|
2019-10-21 15:25:09 -06:00
|
|
|
type SuggestedFix struct {
|
|
|
|
Title string
|
|
|
|
Edits map[span.URI][]protocol.TextEdit
|
|
|
|
}
|
|
|
|
|
2019-10-11 03:39:09 -06:00
|
|
|
type RelatedInformation struct {
|
|
|
|
URI span.URI
|
|
|
|
Range protocol.Range
|
|
|
|
Message string
|
2019-06-20 14:57:45 -06:00
|
|
|
}
|
|
|
|
|
2019-09-27 11:17:59 -06:00
|
|
|
func Diagnostics(ctx context.Context, view View, f File, 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-10-04 15:18:43 -06:00
|
|
|
snapshot, cphs, err := view.CheckPackageHandles(ctx, f)
|
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-10-10 13:22:30 -06:00
|
|
|
cph, err := WidestCheckPackageHandle(cphs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
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() {
|
2019-10-21 15:25:09 -06:00
|
|
|
if err.Kind != ListError {
|
2019-06-14 12:23:47 -06:00
|
|
|
continue
|
|
|
|
}
|
2019-10-20 17:57:03 -06:00
|
|
|
clearReports(view, reports, 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-10-04 15:18:43 -06:00
|
|
|
if err := analyses(ctx, snapshot, cph, disabledAnalyses, reports); err != nil {
|
2019-07-09 15:52:23 -06:00
|
|
|
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-27 11:17:59 -06:00
|
|
|
revDeps := view.GetActiveReverseDeps(ctx, f)
|
2019-09-23 18:06:15 -06:00
|
|
|
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
|
|
|
diag := &Diagnostic{
|
2019-10-20 17:57:03 -06:00
|
|
|
URI: err.URI,
|
2019-10-21 15:25:09 -06:00
|
|
|
Message: err.Message,
|
2019-10-20 17:57:03 -06:00
|
|
|
Range: err.Range,
|
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 {
|
2019-10-21 15:25:09 -06:00
|
|
|
case ParseError:
|
2019-06-14 15:16:26 -06:00
|
|
|
set.parseErrors = append(set.parseErrors, diag)
|
2019-10-20 17:57:03 -06:00
|
|
|
diag.Source = "syntax"
|
2019-10-21 15:25:09 -06:00
|
|
|
case TypeError:
|
2019-06-14 15:16:26 -06:00
|
|
|
set.typeErrors = append(set.typeErrors, diag)
|
2019-10-20 17:57:03 -06:00
|
|
|
diag.Source = "compiler"
|
2019-10-21 15:25:09 -06:00
|
|
|
case ListError:
|
2019-06-14 15:16:26 -06:00
|
|
|
set.listErrors = append(set.listErrors, diag)
|
2019-10-20 17:57:03 -06:00
|
|
|
diag.Source = "go list"
|
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-10-04 15:18:43 -06:00
|
|
|
func analyses(ctx context.Context, snapshot Snapshot, cph CheckPackageHandle, disabledAnalyses map[string]struct{}, reports map[span.URI][]Diagnostic) error {
|
2019-10-21 15:25:09 -06:00
|
|
|
var analyzers []*analysis.Analyzer
|
|
|
|
for _, a := range snapshot.View().Options().Analyzers {
|
|
|
|
if _, ok := disabledAnalyses[a.Name]; ok {
|
|
|
|
continue
|
2019-03-15 11:19:43 -06:00
|
|
|
}
|
2019-10-21 15:25:09 -06:00
|
|
|
analyzers = append(analyzers, a)
|
2019-10-11 03:39:09 -06:00
|
|
|
}
|
|
|
|
|
2019-10-21 15:25:09 -06:00
|
|
|
diagnostics, err := snapshot.Analyze(ctx, cph.ID(), analyzers)
|
2019-06-20 14:57:45 -06:00
|
|
|
if err != nil {
|
2019-10-21 15:25:09 -06:00
|
|
|
return err
|
2019-06-20 14:57:45 -06:00
|
|
|
}
|
2019-10-11 03:39:09 -06:00
|
|
|
|
2019-10-21 15:25:09 -06:00
|
|
|
// Report diagnostics and errors from root analyzers.
|
2019-10-24 16:15:23 -06:00
|
|
|
for _, e := range diagnostics {
|
|
|
|
// 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(e.SuggestedFixes) {
|
|
|
|
tags = append(tags, protocol.Unnecessary)
|
2019-10-21 13:10:19 -06:00
|
|
|
}
|
2019-10-24 16:15:23 -06:00
|
|
|
addReport(snapshot.View(), reports, Diagnostic{
|
|
|
|
URI: e.URI,
|
|
|
|
Range: e.Range,
|
|
|
|
Message: e.Message,
|
|
|
|
Source: e.Category,
|
|
|
|
Severity: protocol.SeverityWarning,
|
|
|
|
Tags: tags,
|
|
|
|
SuggestedFixes: e.SuggestedFixes,
|
|
|
|
Related: e.Related,
|
|
|
|
})
|
2019-10-21 13:10:19 -06:00
|
|
|
}
|
2019-10-21 15:25:09 -06:00
|
|
|
return nil
|
2019-10-21 13:10:19 -06:00
|
|
|
}
|
|
|
|
|
2019-06-20 14:57:45 -06:00
|
|
|
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{}
|
|
|
|
}
|
|
|
|
|
2019-10-21 15:25:09 -06:00
|
|
|
func addReport(v View, reports map[span.URI][]Diagnostic, diagnostic Diagnostic) {
|
|
|
|
if v.Ignore(diagnostic.URI) {
|
2019-06-20 14:57:45 -06:00
|
|
|
return
|
2019-05-31 22:08:57 -06:00
|
|
|
}
|
2019-10-21 15:25:09 -06:00
|
|
|
if _, ok := reports[diagnostic.URI]; ok {
|
|
|
|
reports[diagnostic.URI] = append(reports[diagnostic.URI], diagnostic)
|
2019-08-14 18:12:18 -06:00
|
|
|
}
|
2019-05-31 22:08:57 -06:00
|
|
|
}
|
|
|
|
|
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-10-21 15:25:09 -06:00
|
|
|
// onlyDeletions returns true if all of the suggested fixes are deletions.
|
|
|
|
func onlyDeletions(fixes []SuggestedFix) bool {
|
|
|
|
for _, fix := range fixes {
|
|
|
|
for _, edits := range fix.Edits {
|
|
|
|
for _, edit := range edits {
|
|
|
|
if edit.NewText != "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if protocol.ComparePosition(edit.Range.Start, edit.Range.End) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
2019-06-20 14:57:45 -06:00
|
|
|
}
|
2019-07-09 15:52:23 -06:00
|
|
|
}
|
2019-02-06 09:24:10 -07:00
|
|
|
}
|
2019-10-21 15:25:09 -06:00
|
|
|
return true
|
2019-02-06 09:24:10 -07:00
|
|
|
}
|