From c265c893de73727b3a54511acf0e3f60593c1fa6 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Fri, 10 Aug 2018 20:22:21 -0400 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- src/cmd/go/internal/modload/init.go | 16 ++++++++++++++-- src/cmd/go/testdata/script/mod_enabled.txt | 9 +++++++++ 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 -- -- 2.48.1