]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: ignore /tmp/go.mod
authorRuss Cox <rsc@golang.org>
Sat, 11 Aug 2018 00:22:21 +0000 (20:22 -0400)
committerRuss Cox <rsc@golang.org>
Fri, 17 Aug 2018 14:35:32 +0000 (14:35 +0000)
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>
src/cmd/go/internal/modload/init.go
src/cmd/go/testdata/script/mod_enabled.txt

index 5e9db0f9ea3d1125765017bb874098b5a64821c1..169bb5fdb603334434862cdd5f9ae5b93113c3da 100644 (file)
@@ -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
+                       }
                }
        }
 
index 4901b9c5e6f61c6dc360a966617acee9af36020f..8eef870b02b35362134bb92da551fceef3120802 100644 (file)
@@ -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 --