gopls/internal/regtest: add a test for switching from modules to GOPATH

Fixes golang/go#40487

Change-Id: I79457a8de559da2a9a3ffabdb315f4d35345c8a0
Reviewed-on: https://go-review.googlesource.com/c/tools/+/261738
Trust: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
diff --git a/gopls/internal/regtest/watch_test.go b/gopls/internal/regtest/watch_test.go
index 1462bb3..3b5b69d 100644
--- a/gopls/internal/regtest/watch_test.go
+++ b/gopls/internal/regtest/watch_test.go
@@ -622,6 +622,46 @@
 	})
 }
 
+// Reproduces golang/go#40487.
+func TestSwitchFromModulesToGOPATH(t *testing.T) {
+	testenv.NeedsGo1Point(t, 13)
+
+	const files = `
+-- foo/go.mod --
+module mod.com
+
+go 1.14
+-- foo/blah/blah.go --
+package blah
+
+const Name = ""
+-- foo/main.go --
+package main
+
+import "mod.com/blah"
+
+func main() {
+	_ = blah.Name
+}
+`
+	withOptions(
+		InGOPATH(),
+	).run(t, files, func(t *testing.T, env *Env) {
+		env.OpenFile("foo/main.go")
+		env.RemoveWorkspaceFile("foo/go.mod")
+		env.Await(
+			OnceMet(
+				CompletedWork(lsp.DiagnosticWorkTitle(lsp.FromDidChangeWatchedFiles), 1),
+				env.DiagnosticAtRegexp("foo/main.go", `"mod.com/blah"`),
+			),
+		)
+		env.RegexpReplace("foo/main.go", `"mod.com/blah"`, `"foo/blah"`)
+		env.Await(
+			EmptyDiagnostics("foo/main.go"),
+		)
+	})
+}
+
 func TestNewSymbolInTestVariant(t *testing.T) {
 	const files = `
 -- go.mod --