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"
|
2020-01-24 08:14:25 -07:00
|
|
|
"fmt"
|
2020-01-06 15:31:24 -07:00
|
|
|
"strings"
|
2019-02-07 15:27:10 -07:00
|
|
|
|
2019-02-06 16:47:00 -07:00
|
|
|
"golang.org/x/tools/go/analysis"
|
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-08-14 18:12:18 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2019-02-19 19:11:15 -07:00
|
|
|
"golang.org/x/tools/internal/span"
|
2019-11-21 13:14:48 -07: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
|
|
|
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
|
|
|
|
2020-04-29 15:49:22 -06:00
|
|
|
Related []RelatedInformation
|
2019-10-11 03:39:09 -06:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-07-14 22:19:10 -06:00
|
|
|
func Diagnostics(ctx context.Context, snapshot Snapshot, ph PackageHandle, withAnalysis bool) (map[FileIdentity][]*Diagnostic, bool, error) {
|
2020-06-12 19:23:21 -06:00
|
|
|
onlyIgnoredFiles := true
|
|
|
|
for _, pgh := range ph.CompiledGoFiles() {
|
2020-06-22 12:38:32 -06:00
|
|
|
onlyIgnoredFiles = onlyIgnoredFiles && snapshot.View().IgnoredFile(pgh.File().URI())
|
2020-06-12 19:23:21 -06:00
|
|
|
}
|
|
|
|
if onlyIgnoredFiles {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
|
|
|
|
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.
|
2020-01-23 23:36:46 -07:00
|
|
|
var warn bool
|
2019-11-29 23:17:57 -07:00
|
|
|
if len(ph.MissingDependencies()) > 0 {
|
2020-01-11 21:59:57 -07:00
|
|
|
warn = true
|
2020-01-07 16:00:43 -07:00
|
|
|
}
|
2019-11-29 23:17:57 -07:00
|
|
|
pkg, err := ph.Check(ctx)
|
2019-07-09 15:52:23 -06:00
|
|
|
if err != nil {
|
2020-01-23 23:36:46 -07:00
|
|
|
return nil, false, err
|
2019-03-13 17:31:41 -06:00
|
|
|
}
|
2020-01-06 15:31:24 -07:00
|
|
|
// If we have a package with a single file and errors about "undeclared" symbols,
|
|
|
|
// we may have an ad-hoc package with multiple files. Show a warning message.
|
|
|
|
// TODO(golang/go#36416): Remove this when golang.org/cl/202277 is merged.
|
2020-01-07 16:00:43 -07:00
|
|
|
if len(pkg.CompiledGoFiles()) == 1 && hasUndeclaredErrors(pkg) {
|
2020-01-11 21:59:57 -07:00
|
|
|
warn = true
|
|
|
|
}
|
2019-05-29 12:59:35 -06:00
|
|
|
// Prepare the reports we will send for the files in this package.
|
2020-03-31 21:53:42 -06:00
|
|
|
reports := make(map[FileIdentity][]*Diagnostic)
|
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
|
|
|
for _, pgh := range pkg.CompiledGoFiles() {
|
|
|
|
clearReports(ctx, snapshot, reports, pgh.File().URI())
|
2018-11-12 12:15:47 -07: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() {
|
2019-12-04 10:56:52 -07:00
|
|
|
// We only need to handle lower-level errors.
|
2019-12-19 08:01:26 -07:00
|
|
|
if e.Kind != ListError {
|
2019-06-14 12:23:47 -06:00
|
|
|
continue
|
|
|
|
}
|
2020-01-07 16:00:43 -07:00
|
|
|
// If no file is associated with the error, pick an open file from the package.
|
2020-01-13 17:43:43 -07:00
|
|
|
if e.URI.Filename() == "" {
|
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
|
|
|
for _, pgh := range pkg.CompiledGoFiles() {
|
|
|
|
if snapshot.IsOpen(pgh.File().URI()) {
|
|
|
|
e.URI = pgh.File().URI()
|
2020-01-07 16:00:43 -07:00
|
|
|
}
|
|
|
|
}
|
2019-12-04 10:56:52 -07:00
|
|
|
}
|
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
|
|
|
clearReports(ctx, snapshot, reports, e.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.
|
2020-03-10 08:14:56 -06:00
|
|
|
hadDiagnostics, hadTypeErrors, err := diagnostics(ctx, snapshot, reports, pkg, len(ph.MissingDependencies()) > 0)
|
2020-01-13 17:43:43 -07:00
|
|
|
if err != nil {
|
2020-01-23 23:36:46 -07:00
|
|
|
return nil, warn, err
|
2020-01-13 17:43:43 -07:00
|
|
|
}
|
2020-03-10 08:14:56 -06:00
|
|
|
if hadDiagnostics || !withAnalysis {
|
|
|
|
return reports, warn, nil
|
|
|
|
}
|
|
|
|
// Exit early if the context has been canceled. This also protects us
|
|
|
|
// from a race on Options, see golang/go#36699.
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
return nil, warn, ctx.Err()
|
|
|
|
}
|
|
|
|
// If we don't have any list or parse errors, run analyses.
|
2020-06-12 15:10:06 -06:00
|
|
|
analyzers := pickAnalyzers(snapshot, hadTypeErrors)
|
2020-03-10 08:14:56 -06:00
|
|
|
if err := analyses(ctx, snapshot, reports, ph, analyzers); err != nil {
|
2020-03-30 10:45:15 -06:00
|
|
|
event.Error(ctx, "analyses failed", err, tag.Snapshot.Of(snapshot.ID()), tag.Package.Of(ph.ID()))
|
2020-01-29 12:56:25 -07:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
return nil, warn, ctx.Err()
|
|
|
|
}
|
2019-05-01 20:46:07 -06:00
|
|
|
}
|
2020-01-23 23:36:46 -07:00
|
|
|
return reports, warn, nil
|
2020-01-11 21:59:57 -07:00
|
|
|
}
|
|
|
|
|
2020-06-12 15:10:06 -06:00
|
|
|
func pickAnalyzers(snapshot Snapshot, hadTypeErrors bool) map[string]Analyzer {
|
|
|
|
analyzers := make(map[string]Analyzer)
|
|
|
|
|
|
|
|
// Always run convenience analyzers.
|
|
|
|
for k, v := range snapshot.View().Options().ConvenienceAnalyzers {
|
|
|
|
analyzers[k] = v
|
|
|
|
}
|
|
|
|
// If we had type errors, only run type error analyzers.
|
|
|
|
if hadTypeErrors {
|
|
|
|
for k, v := range snapshot.View().Options().TypeErrorAnalyzers {
|
|
|
|
analyzers[k] = v
|
|
|
|
}
|
|
|
|
return analyzers
|
|
|
|
}
|
|
|
|
for k, v := range snapshot.View().Options().DefaultAnalyzers {
|
|
|
|
analyzers[k] = v
|
|
|
|
}
|
|
|
|
return analyzers
|
|
|
|
}
|
|
|
|
|
2020-03-31 21:53:42 -06:00
|
|
|
func FileDiagnostics(ctx context.Context, snapshot Snapshot, uri span.URI) (FileIdentity, []*Diagnostic, error) {
|
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
|
|
|
fh, err := snapshot.GetFile(ctx, uri)
|
2019-12-19 12:31:39 -07:00
|
|
|
if err != nil {
|
2020-01-11 21:59:57 -07:00
|
|
|
return FileIdentity{}, nil, err
|
2019-12-19 12:31:39 -07:00
|
|
|
}
|
2020-01-11 21:59:57 -07:00
|
|
|
phs, err := snapshot.PackageHandles(ctx, fh)
|
|
|
|
if err != nil {
|
|
|
|
return FileIdentity{}, nil, err
|
2019-05-01 20:46:07 -06:00
|
|
|
}
|
2020-01-11 21:59:57 -07:00
|
|
|
ph, err := NarrowestPackageHandle(phs)
|
|
|
|
if err != nil {
|
|
|
|
return FileIdentity{}, nil, err
|
|
|
|
}
|
2020-07-14 22:19:10 -06:00
|
|
|
reports, _, err := Diagnostics(ctx, snapshot, ph, true)
|
2020-01-11 21:59:57 -07:00
|
|
|
if err != nil {
|
|
|
|
return FileIdentity{}, nil, err
|
|
|
|
}
|
|
|
|
diagnostics, ok := reports[fh.Identity()]
|
|
|
|
if !ok {
|
|
|
|
return FileIdentity{}, nil, errors.Errorf("no diagnostics for %s", uri)
|
|
|
|
}
|
|
|
|
return fh.Identity(), diagnostics, 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
|
|
|
}
|
|
|
|
|
2020-03-31 21:53:42 -06:00
|
|
|
func diagnostics(ctx context.Context, snapshot Snapshot, reports map[FileIdentity][]*Diagnostic, pkg Package, hasMissingDeps bool) (bool, bool, error) {
|
2020-04-20 10:14:12 -06:00
|
|
|
ctx, done := event.Start(ctx, "source.diagnostics", tag.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
|
|
|
|
2020-01-21 12:45:11 -07:00
|
|
|
diagSets := make(map[span.URI]*diagnosticSet)
|
2019-11-20 22:08:58 -07:00
|
|
|
for _, e := range pkg.GetErrors() {
|
2019-08-14 18:12:18 -06:00
|
|
|
diag := &Diagnostic{
|
2019-11-20 22:08:58 -07:00
|
|
|
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
|
|
|
}
|
2020-01-21 12:45:11 -07:00
|
|
|
set, ok := diagSets[e.URI]
|
2019-06-14 15:16:26 -06:00
|
|
|
if !ok {
|
|
|
|
set = &diagnosticSet{}
|
2020-01-21 12:45:11 -07:00
|
|
|
diagSets[e.URI] = 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-12-19 08:01:26 -07: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
|
|
|
}
|
|
|
|
}
|
2020-03-10 08:14:56 -06:00
|
|
|
var nonEmptyDiagnostics, hasTypeErrors bool // track if we actually send non-empty diagnostics
|
2020-01-21 12:45:11 -07:00
|
|
|
for uri, 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 {
|
2020-03-10 08:14:56 -06:00
|
|
|
diags, nonEmptyDiagnostics = set.parseErrors, true
|
2019-06-14 15:16:26 -06:00
|
|
|
} else if len(set.listErrors) > 0 {
|
2020-01-25 17:58:28 -07:00
|
|
|
// Only show list errors if the package has missing dependencies.
|
|
|
|
if hasMissingDeps {
|
2020-03-10 08:14:56 -06:00
|
|
|
diags, nonEmptyDiagnostics = set.listErrors, true
|
2020-01-25 17:58:28 -07:00
|
|
|
}
|
2020-03-10 08:14:56 -06:00
|
|
|
} else if len(set.typeErrors) > 0 {
|
|
|
|
hasTypeErrors = true
|
2018-11-12 12:15:47 -07:00
|
|
|
}
|
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
|
|
|
if err := addReports(ctx, snapshot, reports, uri, diags...); err != nil {
|
2020-03-10 08:14:56 -06:00
|
|
|
return false, false, err
|
2020-01-21 12:45:11 -07:00
|
|
|
}
|
2018-11-12 12:15:47 -07:00
|
|
|
}
|
2020-03-10 08:14:56 -06:00
|
|
|
return nonEmptyDiagnostics, hasTypeErrors, nil
|
2019-05-01 20:46:07 -06:00
|
|
|
}
|
|
|
|
|
2020-03-31 21:53:42 -06:00
|
|
|
func analyses(ctx context.Context, snapshot Snapshot, reports map[FileIdentity][]*Diagnostic, ph PackageHandle, analyses map[string]Analyzer) error {
|
2019-10-21 15:25:09 -06:00
|
|
|
var analyzers []*analysis.Analyzer
|
2020-04-01 21:22:23 -06:00
|
|
|
for _, a := range analyses {
|
|
|
|
if !a.Enabled(snapshot) {
|
2019-10-21 15:25:09 -06:00
|
|
|
continue
|
2019-03-15 11:19:43 -06:00
|
|
|
}
|
2020-03-16 09:49:00 -06:00
|
|
|
analyzers = append(analyzers, a.Analyzer)
|
2019-10-11 03:39:09 -06:00
|
|
|
}
|
2020-03-31 21:53:42 -06:00
|
|
|
analysisErrors, err := snapshot.Analyze(ctx, ph.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.
|
2020-03-10 08:14:56 -06:00
|
|
|
for _, e := range analysisErrors {
|
2020-06-12 15:10:06 -06:00
|
|
|
// If the diagnostic comes from a "convenience" analyzer, it is not
|
|
|
|
// meant to provide diagnostics, but rather only suggested fixes.
|
|
|
|
// Skip these types of errors in diagnostics; we will use their
|
|
|
|
// suggested fixes when providing code actions.
|
|
|
|
if isConvenienceAnalyzer(e.Category) {
|
|
|
|
continue
|
|
|
|
}
|
2019-10-24 16:15:23 -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.
|
2020-03-31 21:53:42 -06:00
|
|
|
// TODO(golang/go#34508): Return these codes from the diagnostics themselves.
|
2019-10-24 16:15:23 -06:00
|
|
|
var tags []protocol.DiagnosticTag
|
|
|
|
if onlyDeletions(e.SuggestedFixes) {
|
|
|
|
tags = append(tags, protocol.Unnecessary)
|
2019-10-21 13:10:19 -06:00
|
|
|
}
|
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
|
|
|
if err := addReports(ctx, snapshot, reports, e.URI, &Diagnostic{
|
2020-04-29 15:49:22 -06:00
|
|
|
Range: e.Range,
|
|
|
|
Message: e.Message,
|
|
|
|
Source: e.Category,
|
|
|
|
Severity: protocol.SeverityWarning,
|
|
|
|
Tags: tags,
|
|
|
|
Related: e.Related,
|
2020-01-21 12:45:11 -07:00
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
func clearReports(ctx context.Context, snapshot Snapshot, reports map[FileIdentity][]*Diagnostic, uri span.URI) {
|
2020-04-23 21:24:24 -06:00
|
|
|
fh := snapshot.FindFile(uri)
|
|
|
|
if fh == nil {
|
|
|
|
return
|
2020-01-21 12:45:11 -07:00
|
|
|
}
|
2020-03-31 21:53:42 -06:00
|
|
|
reports[fh.Identity()] = []*Diagnostic{}
|
2019-06-20 14:57:45 -06:00
|
|
|
}
|
|
|
|
|
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
|
|
|
func addReports(ctx context.Context, snapshot Snapshot, reports map[FileIdentity][]*Diagnostic, uri span.URI, diagnostics ...*Diagnostic) error {
|
2020-04-23 21:24:24 -06:00
|
|
|
fh := snapshot.FindFile(uri)
|
|
|
|
if fh == nil {
|
|
|
|
return nil
|
2020-01-21 12:45:11 -07:00
|
|
|
}
|
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
|
|
|
existingDiagnostics, ok := reports[fh.Identity()]
|
2020-03-31 21:53:42 -06:00
|
|
|
if !ok {
|
2020-04-23 21:24:24 -06:00
|
|
|
return fmt.Errorf("diagnostics for unexpected file %s", uri)
|
2019-11-21 13:14:48 -07:00
|
|
|
}
|
2020-03-31 21:53:42 -06:00
|
|
|
if len(diagnostics) == 1 {
|
|
|
|
d1 := diagnostics[0]
|
|
|
|
if _, ok := snapshot.View().Options().TypeErrorAnalyzers[d1.Source]; ok {
|
|
|
|
for i, d2 := range existingDiagnostics {
|
|
|
|
if r := protocol.CompareRange(d1.Range, d2.Range); r != 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if d1.Message != d2.Message {
|
|
|
|
continue
|
|
|
|
}
|
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
|
|
|
reports[fh.Identity()][i].Tags = append(reports[fh.Identity()][i].Tags, d1.Tags...)
|
2020-03-10 08:14:56 -06:00
|
|
|
}
|
2020-03-31 21:53:42 -06:00
|
|
|
return nil
|
2020-03-10 08:14:56 -06:00
|
|
|
}
|
2020-03-31 21:53:42 -06:00
|
|
|
}
|
2020-04-14 17:32:43 -06:00
|
|
|
reports[fh.Identity()] = append(reports[fh.Identity()], diagnostics...)
|
2019-11-20 22:08:58 -07:00
|
|
|
return nil
|
2019-05-31 22:08:57 -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
|
|
|
}
|
2020-02-26 13:27:56 -07:00
|
|
|
return len(fixes) > 0
|
2019-02-06 09:24:10 -07:00
|
|
|
}
|
2020-01-06 15:31:24 -07:00
|
|
|
|
|
|
|
// hasUndeclaredErrors returns true if a package has a type error
|
|
|
|
// about an undeclared symbol.
|
|
|
|
func hasUndeclaredErrors(pkg Package) bool {
|
|
|
|
for _, err := range pkg.GetErrors() {
|
|
|
|
if err.Kind != TypeError {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.Contains(err.Message, "undeclared name:") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2020-06-12 15:10:06 -06:00
|
|
|
|
|
|
|
func isConvenienceAnalyzer(category string) bool {
|
|
|
|
for _, a := range DefaultOptions().ConvenienceAnalyzers {
|
|
|
|
if category == a.Analyzer.Name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|