diff --git a/internal/lsp/cache/check.go b/internal/lsp/cache/check.go index 67b9256682..24933a5b51 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 f2573605a4..1cf37d0f09 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 5cbea3ccbd..2b4757ca5c 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 {