From 38d8bcfa38af6494a92557425458dc19a8e204d4 Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Thu, 23 May 2019 12:08:20 -0400 Subject: [PATCH] internal/lsp: don't show diagnostics for mod files The Go file changes didn't actually check the file extensions for the files the Go extension is receiving. This error was not noticed because the VSCode extension doesn't yet actually send mod files to gopls yet, but it will in its next release. Fixes golang/go#32178 Change-Id: Ia04d0a92ce7df13fcf4c601421bc000d69855f6d Reviewed-on: https://go-review.googlesource.com/c/tools/+/178679 Run-TryBot: Rebecca Stambler Reviewed-by: Ian Cottrell --- internal/lsp/cache/check.go | 8 +------- internal/lsp/cache/view.go | 21 ++++++++++++++++----- internal/lsp/source/diagnostics.go | 3 ++- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/internal/lsp/cache/check.go b/internal/lsp/cache/check.go index 67b92566..24933a5b 100644 --- a/internal/lsp/cache/check.go +++ b/internal/lsp/cache/check.go @@ -15,11 +15,10 @@ import ( "golang.org/x/tools/go/analysis" "golang.org/x/tools/go/packages" - "golang.org/x/tools/internal/lsp/source" "golang.org/x/tools/internal/span" ) -func (v *view) parse(ctx context.Context, file source.File) ([]packages.Error, error) { +func (v *view) parse(ctx context.Context, f *goFile) ([]packages.Error, error) { v.mcache.mu.Lock() defer v.mcache.mu.Unlock() @@ -28,11 +27,6 @@ func (v *view) parse(ctx context.Context, file source.File) ([]packages.Error, e return nil, err } - f, ok := file.(*goFile) - if !ok { - return nil, fmt.Errorf("not a go file: %v", file.URI()) - } - // If the package for the file has not been invalidated by the application // of the pending changes, there is no need to continue. if f.isPopulated() { diff --git a/internal/lsp/cache/view.go b/internal/lsp/cache/view.go index f2573605..1cf37d0f 100644 --- a/internal/lsp/cache/view.go +++ b/internal/lsp/cache/view.go @@ -6,10 +6,12 @@ package cache import ( "context" + "fmt" "go/ast" "go/parser" "go/types" "os" + "path/filepath" "sync" "golang.org/x/tools/go/packages" @@ -327,12 +329,21 @@ func (v *view) getFile(uri span.URI) (viewFile, error) { if err != nil { return nil, err } - f := &goFile{ - fileBase: fileBase{ - view: v, - fname: filename, - }, + var f *goFile + switch ext := filepath.Ext(filename); ext { + case ".go": + f = &goFile{ + fileBase: fileBase{ + view: v, + fname: filename, + }, + } + case ".mod": + return nil, fmt.Errorf("mod files are not yet supported") + default: + return nil, fmt.Errorf("unsupported file extension: %s", ext) } + v.mapFile(uri, f) return f, nil } diff --git a/internal/lsp/source/diagnostics.go b/internal/lsp/source/diagnostics.go index 5cbea3cc..2b4757ca 100644 --- a/internal/lsp/source/diagnostics.go +++ b/internal/lsp/source/diagnostics.go @@ -56,9 +56,10 @@ func Diagnostics(ctx context.Context, v View, uri span.URI) (map[span.URI][]Diag if err != nil { return singleDiagnostic(uri, "no file found for %s", uri), nil } + // For non-Go files, don't return any diagnostics. gof, ok := f.(GoFile) if !ok { - return singleDiagnostic(uri, "%s is not a go file", uri), nil + return nil, nil } pkg := gof.GetPackage(ctx) if pkg == nil {