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 <rstambler@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
diff --git a/internal/lsp/cache/check.go b/internal/lsp/cache/check.go
index 67b9256..24933a5 100644
--- a/internal/lsp/cache/check.go
+++ b/internal/lsp/cache/check.go
@@ -15,11 +15,10 @@
 
 	"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 @@
 		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 f257360..1cf37d0 100644
--- a/internal/lsp/cache/view.go
+++ b/internal/lsp/cache/view.go
@@ -6,10 +6,12 @@
 
 import (
 	"context"
+	"fmt"
 	"go/ast"
 	"go/parser"
 	"go/types"
 	"os"
+	"path/filepath"
 	"sync"
 
 	"golang.org/x/tools/go/packages"
@@ -327,12 +329,21 @@
 	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 5cbea3c..2b4757c 100644
--- a/internal/lsp/source/diagnostics.go
+++ b/internal/lsp/source/diagnostics.go
@@ -56,9 +56,10 @@
 	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 {