Commit c265c893 authored by Russ Cox's avatar Russ Cox

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: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
parent dea36a6f
......@@ -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
}
}
}
......
......@@ -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 --
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment