internal/lsp/cache: set GO111MODULE=auto in 1.16

1.16 wants to set GO111MODULE=on. There are, AFAIK, two differences
between "auto" and "on". First, if you're in a directory outside of GOPATH
and with no go.mod, "on" will run in module mode with GOMOD=os.DevNull.
I don't think we care very much about that. Second, if you're in GOPATH
with no go.mod, "on" will run in module mode, breaking GOPATH mode.

Breaking GOPATH mode may be desirable for the go command generally, but
for gopls I think it will lead to an unnecessarily bad user experience.
Users will find out when they do their first build or test; there's IMO
no need to also break their editor.

Flip the default back to "auto".

Change-Id: I280e001a9f7e80d65e68c0cb94353d70a7f5425e
Reviewed-on: https://go-review.googlesource.com/c/tools/+/255781
Trust: Heschi Kreinick <heschi@google.com>
Run-TryBot: Heschi Kreinick <heschi@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
diff --git a/internal/lsp/cache/snapshot.go b/internal/lsp/cache/snapshot.go
index ee553e6..14c575d 100644
--- a/internal/lsp/cache/snapshot.go
+++ b/internal/lsp/cache/snapshot.go
@@ -138,7 +138,7 @@
 	cfg := &packages.Config{
 		Context:    ctx,
 		Dir:        dir,
-		Env:        append([]string{}, env...),
+		Env:        append(append([]string{}, env...), "GO111MODULE="+s.view.go111module),
 		BuildFlags: append([]string{}, buildFlags...),
 		Mode: packages.NeedName |
 			packages.NeedFiles |
diff --git a/internal/lsp/cache/view.go b/internal/lsp/cache/view.go
index cc83f94..9412d88 100644
--- a/internal/lsp/cache/view.go
+++ b/internal/lsp/cache/view.go
@@ -150,6 +150,9 @@
 	// `go env` variables that need to be tracked by gopls.
 	gocache, gomodcache, gopath, goprivate string
 
+	// The value of GO111MODULE we want to run with.
+	go111module string
+
 	// goEnv is the `go env` output collected when a view is created.
 	// It includes the values of the environment variables above.
 	goEnv map[string]string
@@ -492,6 +495,8 @@
 	for k, v := range v.goEnv {
 		pe.Env[k] = v
 	}
+	pe.Env["GO111MODULE"] = v.go111module
+
 	modmod, err := v.needsModEqualsMod(ctx, modFH)
 	if err != nil {
 		return cleanup, err
@@ -795,8 +800,25 @@
 	if err != nil {
 		return err
 	}
+
+	v.go111module = os.Getenv("GO111MODULE")
+	for _, kv := range options.Env {
+		split := strings.SplitN(kv, "=", 2)
+		if len(split) != 2 {
+			continue
+		}
+		if split[0] == "GO111MODULE" {
+			v.go111module = split[1]
+		}
+	}
+	// If using 1.16, change the default back to auto. The primary effect of
+	// GO111MODULE=on is to break GOPATH, which we aren't too interested in.
+	if v.goversion >= 16 && v.go111module == "" {
+		v.go111module = "auto"
+	}
+
 	// Make sure to get the `go env` before continuing with initialization.
-	modFile, err := v.setGoEnv(ctx, options.Env)
+	modFile, err := v.setGoEnv(ctx, append(options.Env, "GO111MODULE="+v.go111module))
 	if err != nil {
 		return err
 	}