1
0
mirror of https://github.com/golang/go synced 2024-11-18 10:54:40 -07:00

internal/lsp/cache: hide diagnostics for ignored dirs

The go command ignores go files in specially-named directories like
testdata. We probably still want to try to offer services like
autocomplete and jump-to-definition there, but diagnostics are less
likely to be helpful. As a compromise, just squash diagnostics.

Note that the rules we use are slightly wrong; see the comment on
ignoreFiles for details.

Fixes golang/go#39563.

Change-Id: I0bc00639e68bd71ea55d867af36e07ef4ec780a1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/237638
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Heschi Kreinick 2020-06-12 21:23:21 -04:00
parent 54c614fe05
commit 1725ffee6d
2 changed files with 48 additions and 0 deletions

View File

@ -803,3 +803,24 @@ func TestHello(t *testing.T) {
)
})
}
func TestIgnoredFiles(t *testing.T) {
const ws = `
-- go.mod --
module mod.com
go 1.15
-- _foo/x.go --
package x
var _ = foo.Bar
`
runner.Run(t, ws, func(t *testing.T, env *Env) {
env.OpenFile("_foo/x.go")
env.Await(
OnceMet(
CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidOpen), 1),
NoDiagnostics("_foo/x.go"),
))
})
}

View File

@ -8,6 +8,7 @@ import (
"context"
"fmt"
"go/ast"
"path/filepath"
"strconv"
"strings"
@ -42,6 +43,14 @@ type RelatedInformation struct {
}
func Diagnostics(ctx context.Context, snapshot Snapshot, ph PackageHandle, missingModules map[string]*modfile.Require, withAnalysis bool) (map[FileIdentity][]*Diagnostic, bool, error) {
onlyIgnoredFiles := true
for _, pgh := range ph.CompiledGoFiles() {
onlyIgnoredFiles = onlyIgnoredFiles && ignoreFile(pgh.File().URI().Filename())
}
if onlyIgnoredFiles {
return nil, false, nil
}
// If we are missing dependencies, it may because the user's workspace is
// not correctly configured. Report errors, if possible.
var warn bool
@ -134,6 +143,24 @@ func Diagnostics(ctx context.Context, snapshot Snapshot, ph PackageHandle, missi
return reports, warn, nil
}
// ignoreFile is an approximation of go list's exclusion rules. go help list:
// Directory and file names that begin with "." or "_" are ignored
// by the go tool, as are directories named "testdata".
// Those rules only apply if the directory in question is beneath GOPATH
// or the module root, but we don't really have that information, so we just
// hope that nobody's messing with us.
func ignoreFile(path string) bool {
for _, component := range strings.Split(path, string(filepath.Separator)) {
if len(component) == 0 {
continue
}
if component[0] == '.' || component[0] == '_' || component == "testdata" {
return true
}
}
return false
}
func FileDiagnostics(ctx context.Context, snapshot Snapshot, uri span.URI) (FileIdentity, []*Diagnostic, error) {
fh, err := snapshot.GetFile(ctx, uri)
if err != nil {