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-11-20 22:08:58 -07:00
|
|
|
File FileIdentity
|
2019-08-14 18:12:18 -06:00
|
|
|
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-11-20 22:08:58 -07:00
|
|
|
func Diagnostics(ctx context.Context, snapshot Snapshot, f File, disabledAnalyses map[string]struct{}) (map[FileIdentity][]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-11-20 22:08:58 -07:00
|
|
|
fh := snapshot.Handle(ctx, f)
|
2019-11-20 14:38:43 -07:00
|
|
|
cphs, err := snapshot.PackageHandles(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 {
|
2019-11-20 14:38:43 -07:00
|
|
|
warningMsg, err = checkCommonErrors(ctx, snapshot.View(), f.URI())
|
2019-09-23 18:19:50 -06:00
|
|
|
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-11-20 22:08:58 -07:00
|
|
|
return singleDiagnostic(fh.Identity(), "%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-11-20 22:08:58 -07:00
|
|
|
reports := make(map[FileIdentity][]Diagnostic)
|
2019-11-20 14:15:00 -07:00
|
|
|
for _, fh := range pkg.CompiledGoFiles() {
|
2019-11-20 22:08:58 -07:00
|
|
|
clearReports(snapshot, reports, fh.File().Identity())
|
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-11-20 22:08:58 -07:00
|
|
|
for _, e := range pkg.GetErrors() {
|
|
|
|
if e.Kind != ListError {
|
2019-06-14 12:23:47 -06:00
|
|
|
continue
|
|
|
|
}
|
2019-11-20 22:08:58 -07:00
|
|
|
clearReports(snapshot, reports, e.File)
|
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-11-20 14:38:43 -07:00
|
|
|
if !diagnostics(ctx, 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-11-20 14:38:43 -07:00
|
|
|
revDeps := snapshot.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
|
|
|
}
|
2019-11-20 14:15:00 -07:00
|
|
|
for _, fh := range pkg.CompiledGoFiles() {
|
2019-11-20 22:08:58 -07:00
|
|
|
clearReports(snapshot, reports, fh.File().Identity())
|
2019-05-01 20:46:07 -06:00
|
|
|
}
|
2019-11-20 14:38:43 -07:00
|
|
|
diagnostics(ctx, 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-11-20 22:08:58 -07:00
|
|
|
func diagnostics(ctx context.Context, pkg Package, reports map[FileIdentity][]Diagnostic) bool {
|
2019-07-09 18:16:21 -06:00
|
|
|
ctx, done := trace.StartSpan(ctx, "source.diagnostics", telemetry.Package.Of(pkg.ID()))
|
2019-11-12 15:58:37 -07:00
|
|
|
_ = ctx // circumvent SA4006
|
2019-07-09 18:16:21 -06:00
|
|
|
defer done()
|
2019-08-02 17:45:56 -06:00
|
|
|
|
2019-11-20 22:08:58 -07:00
|
|
|
diagSets := make(map[FileIdentity]*diagnosticSet)
|
|
|
|
for _, e := range pkg.GetErrors() {
|
2019-08-14 18:12:18 -06:00
|
|
|
diag := &Diagnostic{
|
2019-11-20 22:08:58 -07:00
|
|
|
File: e.File,
|
|
|
|
Message: e.Message,
|
|
|
|
Range: e.Range,
|
2019-09-24 22:46:57 -06:00
|
|
|
Severity: protocol.SeverityError,
|
2019-06-14 15:16:26 -06:00
|
|
|
}
|
2019-11-20 22:08:58 -07:00
|
|
|
set, ok := diagSets[e.File]
|
2019-06-14 15:16:26 -06:00
|
|
|
if !ok {
|
|
|
|
set = &diagnosticSet{}
|
2019-11-20 22:08:58 -07:00
|
|
|
diagSets[e.File] = set
|
2019-06-14 15:16:26 -06:00
|
|
|
}
|
2019-11-20 22:08:58 -07:00
|
|
|
switch e.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
|
2019-11-20 22:08:58 -07:00
|
|
|
for fileID, set := range diagSets {
|
2019-06-14 15:16:26 -06:00
|
|
|
// 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 {
|
2019-11-20 22:08:58 -07:00
|
|
|
if _, ok := reports[fileID]; ok {
|
|
|
|
reports[fileID] = append(reports[fileID], *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-11-20 22:08:58 -07:00
|
|
|
func analyses(ctx context.Context, snapshot Snapshot, cph CheckPackageHandle, disabledAnalyses map[string]struct{}, reports map[FileIdentity][]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-11-20 22:08:58 -07:00
|
|
|
addReport(ctx, snapshot, reports, Diagnostic{
|
|
|
|
File: e.File,
|
2019-10-24 16:15:23 -06:00
|
|
|
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-11-20 22:08:58 -07:00
|
|
|
func clearReports(snapshot Snapshot, reports map[FileIdentity][]Diagnostic, fileID FileIdentity) {
|
|
|
|
if snapshot.View().Ignore(fileID.URI) {
|
2019-05-31 22:08:57 -06:00
|
|
|
return
|
|
|
|
}
|
2019-11-20 22:08:58 -07:00
|
|
|
reports[fileID] = []Diagnostic{}
|
2019-06-20 14:57:45 -06:00
|
|
|
}
|
|
|
|
|
2019-11-20 22:08:58 -07:00
|
|
|
func addReport(ctx context.Context, snapshot Snapshot, reports map[FileIdentity][]Diagnostic, diagnostic Diagnostic) error {
|
|
|
|
if snapshot.View().Ignore(diagnostic.File.URI) {
|
|
|
|
return nil
|
2019-05-31 22:08:57 -06:00
|
|
|
}
|
2019-11-20 22:08:58 -07:00
|
|
|
if _, ok := reports[diagnostic.File]; ok {
|
|
|
|
reports[diagnostic.File] = append(reports[diagnostic.File], diagnostic)
|
2019-08-14 18:12:18 -06:00
|
|
|
}
|
2019-11-20 22:08:58 -07:00
|
|
|
return nil
|
2019-05-31 22:08:57 -06:00
|
|
|
}
|
|
|
|
|
2019-11-20 22:08:58 -07:00
|
|
|
func singleDiagnostic(fileID FileIdentity, format string, a ...interface{}) map[FileIdentity][]Diagnostic {
|
|
|
|
return map[FileIdentity][]Diagnostic{
|
|
|
|
fileID: []Diagnostic{{
|
|
|
|
File: fileID,
|
|
|
|
Source: "gopls",
|
2019-08-14 18:12:18 -06:00
|
|
|
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
|
|
|
}
|