2018-11-05 12:48:08 -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.
|
|
|
|
|
2018-10-19 14:03:29 -06:00
|
|
|
package lsp
|
|
|
|
|
|
|
|
import (
|
2018-12-05 15:00:36 -07:00
|
|
|
"context"
|
2019-05-17 11:45:50 -06:00
|
|
|
"strings"
|
2018-11-13 09:13:53 -07:00
|
|
|
|
2019-12-17 14:13:33 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/mod"
|
2018-10-19 14:03:29 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2018-11-02 14:15:31 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2019-07-14 21:08:10 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/telemetry"
|
2019-08-13 13:07:39 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry/log"
|
2019-09-17 12:36:39 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry/trace"
|
2018-10-19 14:03:29 -06:00
|
|
|
)
|
|
|
|
|
2019-12-19 12:31:39 -07:00
|
|
|
func (s *Server) diagnoseSnapshot(ctx context.Context, snapshot source.Snapshot) {
|
2019-11-20 12:26:02 -07:00
|
|
|
ctx, done := trace.StartSpan(ctx, "lsp:background-worker")
|
|
|
|
defer done()
|
|
|
|
|
2019-12-19 12:31:39 -07:00
|
|
|
wsPackages, err := snapshot.View().WorkspacePackageIDs(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(ctx, "diagnoseSnapshot: no workspace packages", err, telemetry.Directory.Of(snapshot.View().Folder))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, id := range wsPackages {
|
2020-01-07 16:00:43 -07:00
|
|
|
go func(id string) {
|
|
|
|
ph, err := snapshot.PackageHandle(ctx, id)
|
2019-11-20 12:26:02 -07:00
|
|
|
if err != nil {
|
2020-01-07 16:00:43 -07:00
|
|
|
log.Error(ctx, "diagnoseSnapshot: no PackageHandle for workspace package", err, telemetry.Package.Of(id))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
reports, _, err := source.PackageDiagnostics(ctx, snapshot, ph, false, snapshot.View().Options().DisabledAnalyses)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(ctx, "diagnoseSnapshot: no diagnostics", err, telemetry.Package.Of(ph.ID()))
|
2019-11-20 12:26:02 -07:00
|
|
|
return
|
2019-11-21 16:55:49 -07:00
|
|
|
}
|
2019-11-20 12:26:02 -07:00
|
|
|
// Don't publish empty diagnostics.
|
|
|
|
s.publishReports(ctx, reports, false)
|
2020-01-07 16:00:43 -07:00
|
|
|
}(id)
|
2019-11-15 12:47:29 -07:00
|
|
|
}
|
2019-12-17 14:13:33 -07:00
|
|
|
// Run diagnostics on the go.mod file.
|
|
|
|
s.diagnoseModfile(snapshot)
|
2019-11-15 12:47:29 -07:00
|
|
|
}
|
|
|
|
|
2019-12-17 16:57:54 -07:00
|
|
|
func (s *Server) diagnoseFile(snapshot source.Snapshot, fh source.FileHandle) {
|
2019-11-20 12:26:02 -07:00
|
|
|
ctx := snapshot.View().BackgroundContext()
|
2019-09-17 12:36:39 -06:00
|
|
|
ctx, done := trace.StartSpan(ctx, "lsp:background-worker")
|
|
|
|
defer done()
|
|
|
|
|
2019-12-17 16:57:54 -07:00
|
|
|
ctx = telemetry.File.With(ctx, fh.Identity().URI)
|
2019-09-17 09:19:11 -06:00
|
|
|
|
2020-01-07 16:00:43 -07:00
|
|
|
reports, warningMsg, err := source.FileDiagnostics(ctx, snapshot, fh, true, snapshot.View().Options().DisabledAnalyses)
|
2019-12-05 14:37:47 -07:00
|
|
|
// Check the warning message first.
|
2019-09-06 16:25:36 -06:00
|
|
|
if warningMsg != "" {
|
|
|
|
s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
|
|
|
|
Type: protocol.Info,
|
|
|
|
Message: warningMsg,
|
|
|
|
})
|
|
|
|
}
|
2019-12-05 14:37:47 -07:00
|
|
|
if err != nil {
|
|
|
|
if err != context.Canceled {
|
|
|
|
log.Error(ctx, "diagnoseFile: could not generate diagnostics", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2019-11-20 12:26:02 -07:00
|
|
|
// Publish empty diagnostics for files.
|
|
|
|
s.publishReports(ctx, reports, true)
|
|
|
|
}
|
2019-03-14 15:19:01 -06:00
|
|
|
|
2019-12-17 14:13:33 -07:00
|
|
|
func (s *Server) diagnoseModfile(snapshot source.Snapshot) {
|
|
|
|
ctx := snapshot.View().BackgroundContext()
|
|
|
|
ctx, done := trace.StartSpan(ctx, "lsp:background-worker")
|
|
|
|
defer done()
|
|
|
|
|
|
|
|
f, diags, err := mod.Diagnostics(ctx, snapshot)
|
|
|
|
if err != nil {
|
|
|
|
if err != context.Canceled {
|
|
|
|
log.Error(ctx, "diagnoseModfile: could not generate diagnostics", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
reports := map[source.FileIdentity][]source.Diagnostic{f: diags}
|
|
|
|
// Publish empty diagnostics for files.
|
|
|
|
s.publishReports(ctx, reports, true)
|
|
|
|
}
|
|
|
|
|
2019-11-20 12:26:02 -07:00
|
|
|
func (s *Server) publishReports(ctx context.Context, reports map[source.FileIdentity][]source.Diagnostic, publishEmpty bool) {
|
2019-12-05 14:37:47 -07:00
|
|
|
// Check for context cancellation before publishing diagnostics.
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-20 23:24:43 -07:00
|
|
|
s.deliveredMu.Lock()
|
|
|
|
defer s.deliveredMu.Unlock()
|
|
|
|
|
2019-11-20 22:08:58 -07:00
|
|
|
for fileID, diagnostics := range reports {
|
2019-12-05 14:37:47 -07:00
|
|
|
// Don't deliver diagnostics if the context has already been canceled.
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
break
|
|
|
|
}
|
2019-11-20 23:24:43 -07:00
|
|
|
// Pre-sort diagnostics to avoid extra work when we compare them.
|
|
|
|
source.SortDiagnostics(diagnostics)
|
|
|
|
toSend := sentDiagnostics{
|
|
|
|
version: fileID.Version,
|
|
|
|
identifier: fileID.Identifier,
|
|
|
|
sorted: diagnostics,
|
|
|
|
}
|
2020-01-02 10:45:15 -07:00
|
|
|
delivered, ok := s.delivered[fileID.URI]
|
2020-01-08 11:53:50 -07:00
|
|
|
// If diagnostics are empty and not previously delivered,
|
|
|
|
// only send them if we are publishing empty diagnostics.
|
|
|
|
if !ok && len(diagnostics) == 0 && !publishEmpty {
|
|
|
|
// Update the delivered map to cache the diagnostics.
|
|
|
|
s.delivered[fileID.URI] = toSend
|
|
|
|
continue
|
|
|
|
}
|
2020-01-02 10:45:15 -07:00
|
|
|
// Reuse equivalent cached diagnostics for subsequent file versions (if known),
|
|
|
|
// or identical files (if versions are not known).
|
|
|
|
if ok {
|
2020-01-08 11:53:50 -07:00
|
|
|
// If the file is open, and we've already delivered diagnostics for
|
|
|
|
// a later version, do nothing. This only works for open files,
|
|
|
|
// since their contents in the editor are the source of truth.
|
|
|
|
if s.session.IsOpen(fileID.URI) && fileID.Version < delivered.version {
|
|
|
|
continue
|
|
|
|
}
|
2020-01-02 10:45:15 -07:00
|
|
|
geqVersion := fileID.Version >= delivered.version && delivered.version > 0
|
2019-11-20 23:24:43 -07:00
|
|
|
noVersions := (fileID.Version == 0 && delivered.version == 0) && delivered.identifier == fileID.Identifier
|
2020-01-02 10:45:15 -07:00
|
|
|
if (geqVersion || noVersions) && equalDiagnostics(delivered.sorted, diagnostics) {
|
2019-11-20 23:24:43 -07:00
|
|
|
// Update the delivered map even if we reuse cached diagnostics.
|
|
|
|
s.delivered[fileID.URI] = toSend
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2020-01-08 11:53:50 -07:00
|
|
|
|
2019-12-05 14:37:47 -07:00
|
|
|
if err := s.client.PublishDiagnostics(ctx, &protocol.PublishDiagnosticsParams{
|
|
|
|
Diagnostics: toProtocolDiagnostics(ctx, diagnostics),
|
|
|
|
URI: protocol.NewURI(fileID.URI),
|
|
|
|
Version: fileID.Version,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error(ctx, "failed to deliver diagnostic", err, telemetry.File)
|
2019-05-01 20:46:07 -06:00
|
|
|
continue
|
2019-03-14 15:19:01 -06:00
|
|
|
}
|
2019-11-20 23:24:43 -07:00
|
|
|
// Update the delivered map.
|
|
|
|
s.delivered[fileID.URI] = toSend
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// equalDiagnostics returns true if the 2 lists of diagnostics are equal.
|
|
|
|
// It assumes that both a and b are already sorted.
|
|
|
|
func equalDiagnostics(a, b []source.Diagnostic) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := 0; i < len(a); i++ {
|
|
|
|
if source.CompareDiagnostic(a[i], b[i]) != 0 {
|
|
|
|
return false
|
|
|
|
}
|
2019-05-01 20:46:07 -06:00
|
|
|
}
|
2019-11-20 23:24:43 -07:00
|
|
|
return true
|
2018-12-18 14:18:03 -07:00
|
|
|
}
|
|
|
|
|
2019-09-24 14:28:59 -06:00
|
|
|
func toProtocolDiagnostics(ctx context.Context, diagnostics []source.Diagnostic) []protocol.Diagnostic {
|
2018-11-12 12:15:47 -07:00
|
|
|
reports := []protocol.Diagnostic{}
|
|
|
|
for _, diag := range diagnostics {
|
2019-10-11 03:39:09 -06:00
|
|
|
related := make([]protocol.DiagnosticRelatedInformation, 0, len(diag.Related))
|
|
|
|
for _, rel := range diag.Related {
|
|
|
|
related = append(related, protocol.DiagnosticRelatedInformation{
|
|
|
|
Location: protocol.Location{
|
|
|
|
URI: protocol.NewURI(rel.URI),
|
|
|
|
Range: rel.Range,
|
|
|
|
},
|
|
|
|
Message: rel.Message,
|
|
|
|
})
|
|
|
|
}
|
2019-09-24 22:46:57 -06:00
|
|
|
reports = append(reports, protocol.Diagnostic{
|
2019-10-11 03:39:09 -06:00
|
|
|
Message: strings.TrimSpace(diag.Message), // go list returns errors prefixed by newline
|
|
|
|
Range: diag.Range,
|
|
|
|
Severity: diag.Severity,
|
|
|
|
Source: diag.Source,
|
|
|
|
Tags: diag.Tags,
|
|
|
|
RelatedInformation: related,
|
2019-09-24 22:46:57 -06:00
|
|
|
})
|
2018-11-12 12:15:47 -07:00
|
|
|
}
|
2019-09-24 14:28:59 -06:00
|
|
|
return reports
|
2018-10-29 16:12:41 -06:00
|
|
|
}
|