aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2018-08-10 20:22:21 -0400
committerRuss Cox <rsc@golang.org>2018-08-17 14:35:32 +0000
commitc265c893de73727b3a54511acf0e3f60593c1fa6 (patch)
treefc134acedf527352c2f2ecd3aaaeb96c460984a4
parentdea36a6f751783c3c511e2eb0e1de3696daec1f7 (diff)
downloadgo-c265c893de73727b3a54511acf0e3f60593c1fa6.tar.gz
go-c265c893de73727b3a54511acf0e3f60593c1fa6.zip
cmd/go: ignore /tmp/go.mod
Two different people have created /tmp/go.mod for experimentation and then had other tests that create fresh work directories below /tmp fail unexpectedly because the go command finds /tmp/go.mod. Refuse to use /tmp/go.mod. /tmp/anything/go.mod is fine. Fixes #26708. Change-Id: I2a4f61ea63099cff59fbf9e8798e5dcefefd5557 Reviewed-on: https://go-review.googlesource.com/129063 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
-rw-r--r--src/cmd/go/internal/modload/init.go16
-rw-r--r--src/cmd/go/testdata/script/mod_enabled.txt9
2 files changed, 23 insertions, 2 deletions
diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go
index 5e9db0f9ea..169bb5fdb6 100644
--- a/src/cmd/go/internal/modload/init.go
+++ b/src/cmd/go/internal/modload/init.go
@@ -150,8 +150,20 @@ func Init() {
ModRoot = cwd
} else {
ModRoot, _ = FindModuleRoot(cwd, "", MustUseModules)
- if ModRoot == "" && !MustUseModules {
- return
+ if !MustUseModules {
+ if ModRoot == "" {
+ return
+ }
+ if search.InDir(ModRoot, os.TempDir()) == "." {
+ // If you create /tmp/go.mod for experimenting,
+ // then any tests that create work directories under /tmp
+ // will find it and get modules when they're not expecting them.
+ // It's a bit of a peculiar thing to disallow but quite mysterious
+ // when it happens. See golang.org/issue/26708.
+ ModRoot = ""
+ fmt.Fprintf(os.Stderr, "go: warning: ignoring go.mod in system temp root %v\n", os.TempDir())
+ return
+ }
}
}
diff --git a/src/cmd/go/testdata/script/mod_enabled.txt b/src/cmd/go/testdata/script/mod_enabled.txt
index 4901b9c5e6..8eef870b02 100644
--- a/src/cmd/go/testdata/script/mod_enabled.txt
+++ b/src/cmd/go/testdata/script/mod_enabled.txt
@@ -65,6 +65,15 @@ cd $GOPATH/foo/bar/baz
go env GOMOD
! stdout .+
+# GO111MODULE=auto should ignore and warn about /tmp/go.mod
+env GO111MODULE=auto
+cp $GOPATH/src/x/y/z/go.mod $WORK/tmp/go.mod
+mkdir $WORK/tmp/mydir
+cd $WORK/tmp/mydir
+go env GOMOD
+! stdout .+
+stderr '^go: warning: ignoring go.mod in system temp root '
+
-- $GOPATH/src/x/y/z/go.mod --
module x/y/z
-- $GOPATH/src/x/y/z/w/w.txt --