Skip to content

Commit 3791637

Browse files
author
Jay Conrod
committed
all: fix tests in preparation for GO111MODULE=on by default
This CL does not fix failures in ./gopls/internal/regtest, which will be fixed separately. In refactor/rename.TestDiff, add a go.mod file. In internal/imports.ProcessEnv.buildContext, set an I/O hook if GO111MODULE=off in ProcessEnv but not in the current process's environment. Context allows the user to set GOPATH, GOOS, GOARCH, and a few other environment variables, but not GO111MODULE. Context.Import may return different results than packages.Load if the latter is invoked with a GO111MODULE value that differs from the caller's environment. Setting an I/O hook forces Import to run in GOPATH mode, not invoking 'go list'. This is undocumented, but it should be stable while GOPATH is supported. For golang/go#41330 Change-Id: I5679e8941e32dc95b05c234cb2e3fec5cabebced Reviewed-on: https://go-review.googlesource.com/c/tools/+/255398 Run-TryBot: Jay Conrod <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Trust: Jay Conrod <[email protected]>
1 parent d148ae1 commit 3791637

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

internal/imports/fix.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,17 @@ func (e *ProcessEnv) buildContext() (*build.Context, error) {
928928
dir.SetString(e.WorkingDir)
929929
}
930930

931+
// Since Go 1.11, go/build.Context.Import may invoke 'go list' depending on
932+
// the value in GO111MODULE in the process's environment. We always want to
933+
// run in GOPATH mode when calling Import, so we need to prevent this from
934+
// happening. In Go 1.16, GO111MODULE defaults to "on", so this problem comes
935+
// up more frequently.
936+
//
937+
// HACK: setting any of the Context I/O hooks prevents Import from invoking
938+
// 'go list', regardless of GO111MODULE. This is undocumented, but it's
939+
// unlikely to change before GOPATH support is removed.
940+
ctx.ReadDir = ioutil.ReadDir
941+
931942
return &ctx, nil
932943
}
933944

refactor/rename/rename_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,14 @@ func TestDiff(t *testing.T) {
13251325
t.Fatal(err)
13261326
}
13271327

1328+
const modFile = `module example.com/rename
1329+
1330+
go 1.15
1331+
`
1332+
if err := ioutil.WriteFile(filepath.Join(pkgDir, "go.mod"), []byte(modFile), 0644); err != nil {
1333+
t.Fatal(err)
1334+
}
1335+
13281336
const goFile = `package rename
13291337
13301338
func justHereForTestingDiff() {

0 commit comments

Comments
 (0)