internal/lsp: watch go.{mod,sum} files, as well as Go files

Not sure how this hasn't come up earlier. We aren't noticing file
changes on-disk for go.mod/go.sum files.

Fixes golang/vscode-go#297

Change-Id: I4532a252f330404515efec244a9c266a765e6bcb
Reviewed-on: https://go-review.googlesource.com/c/tools/+/242160
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
diff --git a/internal/lsp/general.go b/internal/lsp/general.go
index 83a3bb5..8d38c57 100644
--- a/internal/lsp/general.go
+++ b/internal/lsp/general.go
@@ -171,7 +171,7 @@
 					Method: "workspace/didChangeWatchedFiles",
 					RegisterOptions: protocol.DidChangeWatchedFilesRegistrationOptions{
 						Watchers: []protocol.FileSystemWatcher{{
-							GlobPattern: fmt.Sprintf("%s/**.go", dir),
+							GlobPattern: fmt.Sprintf("%s/**.{go,mod,sum}", dir),
 							Kind:        float64(protocol.WatchChange + protocol.WatchDelete + protocol.WatchCreate),
 						}},
 					},
@@ -199,7 +199,6 @@
 				wg.Wait()
 				work.End(ctx, "Done.")
 			}()
-
 		}()
 	}
 	for _, folder := range folders {
diff --git a/internal/lsp/regtest/modfile_test.go b/internal/lsp/regtest/modfile_test.go
index 5caf875..692a9f3 100644
--- a/internal/lsp/regtest/modfile_test.go
+++ b/internal/lsp/regtest/modfile_test.go
@@ -7,6 +7,7 @@
 import (
 	"testing"
 
+	"golang.org/x/tools/internal/lsp"
 	"golang.org/x/tools/internal/lsp/protocol"
 	"golang.org/x/tools/internal/lsp/tests"
 	"golang.org/x/tools/internal/testenv"
@@ -170,3 +171,38 @@
 		}
 	}, WithProxy(proxy))
 }
+
+// TODO: For this test to be effective, the sandbox's file watcher must respect
+// the file watching GlobPattern in the capability registration. See
+// golang/go#39384.
+func TestModuleChangesOnDisk(t *testing.T) {
+	testenv.NeedsGo1Point(t, 14)
+
+	const mod = `
+-- go.mod --
+module mod.com
+
+go 1.12
+
+require example.com v1.2.3
+-- main.go --
+package main
+
+func main() {
+	fmt.Println(blah.Name)
+`
+	const want = `module mod.com
+
+go 1.12
+`
+	runner.Run(t, mod, func(t *testing.T, env *Env) {
+		env.Await(
+			CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromInitialWorkspaceLoad), 1),
+			env.DiagnosticAtRegexp("go.mod", "require"),
+		)
+		env.Sandbox.RunGoCommand(env.Ctx, "mod", "tidy")
+		env.Await(
+			EmptyDiagnostics("go.mod"),
+		)
+	}, WithProxy(proxy))
+}