Skip to content

Commit

Permalink
internal/lsp/cache: set GO111MODULE=auto in 1.16
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
heschi committed Sep 17, 2020
1 parent 3791637 commit d56e4e4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion internal/lsp/cache/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (s *snapshot) configWithDir(ctx context.Context, dir string) *packages.Conf
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 |
Expand Down
24 changes: 23 additions & 1 deletion internal/lsp/cache/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ type View struct {
// `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
Expand Down Expand Up @@ -492,6 +495,8 @@ func (v *View) populateProcessEnv(ctx context.Context, modFH, sumFH source.FileH
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
Expand Down Expand Up @@ -795,8 +800,25 @@ func (v *View) setBuildInformation(ctx context.Context, options *source.Options)
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
}
Expand Down

0 comments on commit d56e4e4

Please sign in to comment.