Skip to content

Commit

Permalink
cmd/go: default to GO111MODULE=auto and make it trigger in GOPATH/src
Browse files Browse the repository at this point in the history
Fixes #31857

Change-Id: Ib0b791376acb7ee1cdc163f808b8ecf77dbdaf06
Reviewed-on: https://go-review.googlesource.com/c/go/+/176580
Run-TryBot: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Jay Conrod <jayconrod@google.com>
  • Loading branch information
Bryan C. Mills committed May 14, 2019
1 parent 45d74aa commit 5b4ea62
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 68 deletions.
5 changes: 0 additions & 5 deletions src/cmd/go/internal/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,6 @@ var (
// in module-aware mode (as opposed to GOPATH mode).
// It is equal to modload.Enabled, but not all packages can import modload.
ModulesEnabled bool

// GoModInGOPATH records whether we've found a go.mod in GOPATH/src
// in GO111MODULE=auto mode. In that case, we don't use modules
// but people might expect us to, so 'go get' warns.
GoModInGOPATH string
)

func exeSuffix() string {
Expand Down
5 changes: 0 additions & 5 deletions src/cmd/go/internal/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,6 @@ func runGet(cmd *base.Command, args []string) {
// Should not happen: main.go should install the separate module-enabled get code.
base.Fatalf("go get: modules not implemented")
}
if cfg.GoModInGOPATH != "" {
// Warn about not using modules with GO111MODULE=auto when go.mod exists.
// To silence the warning, users can set GO111MODULE=off.
fmt.Fprintf(os.Stderr, "go get: warning: modules disabled by GO111MODULE=auto in GOPATH/src;\n\tignoring %s;\n\tsee 'go help modules'\n", base.ShortPath(cfg.GoModInGOPATH))
}

work.BuildInit()

Expand Down
33 changes: 3 additions & 30 deletions src/cmd/go/internal/modload/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (

var (
cwd string // TODO(bcmills): Is this redundant with base.Cwd?
mustUseModules = true
mustUseModules = false
initialized bool

modRoot string
Expand Down Expand Up @@ -79,8 +79,6 @@ func BinDir() string {
return filepath.Join(gopath, "bin")
}

var inGOPATH bool // running in GOPATH/src

// Init determines whether module mode is enabled, locates the root of the
// current module (if any), sets environment variables for Git subprocesses, and
// configures the cfg, codehost, load, modfetch, and search packages for use
Expand All @@ -95,9 +93,9 @@ func Init() {
switch env {
default:
base.Fatalf("go: unknown environment setting GO111MODULE=%s", env)
case "auto":
case "auto", "":
mustUseModules = false
case "on", "":
case "on":
mustUseModules = true
case "off":
mustUseModules = false
Expand Down Expand Up @@ -137,28 +135,6 @@ func Init() {
base.Fatalf("go: %v", err)
}

inGOPATH = false
for _, gopath := range filepath.SplitList(cfg.BuildContext.GOPATH) {
if gopath == "" {
continue
}
if search.InDir(cwd, filepath.Join(gopath, "src")) != "" {
inGOPATH = true
break
}
}

if inGOPATH && !mustUseModules {
if CmdModInit {
die() // Don't init a module that we're just going to ignore.
}
// No automatic enabling in GOPATH.
if root := findModuleRoot(cwd); root != "" {
cfg.GoModInGOPATH = filepath.Join(root, "go.mod")
}
return
}

if CmdModInit {
// Running 'go mod init': go.mod will be created in current directory.
modRoot = cwd
Expand Down Expand Up @@ -300,9 +276,6 @@ func die() {
if cfg.Getenv("GO111MODULE") == "off" {
base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'")
}
if inGOPATH && !mustUseModules {
base.Fatalf("go: modules disabled inside GOPATH/src by GO111MODULE=auto; see 'go help modules'")
}
if cwd != "" {
if dir, name := findAltConfig(cwd); dir != "" {
rel, err := filepath.Rel(cwd, dir)
Expand Down
1 change: 1 addition & 0 deletions src/cmd/go/testdata/script/get_insecure_redirect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[!net] skip

env GO111MODULE=on
env GOPROXY=

! go get -d vcs-test.golang.org/insecure/go/insecure
Expand Down
21 changes: 16 additions & 5 deletions src/cmd/go/testdata/script/mod_enabled.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# GO111MODULE=auto should only trigger outside GOPATH/src
# GO111MODULE=auto should trigger any time a go.mod exists in a parent directory.
env GO111MODULE=auto

cd $GOPATH/src/x/y/z
go env GOMOD
! stdout . # no non-empty lines
! go list -m -f {{.GoMod}}
stderr 'not using modules'
stdout $GOPATH[/\\]src[/\\]x[/\\]y[/\\]z[/\\]go.mod
go list -m -f {{.GoMod}}
stdout $GOPATH[/\\]src[/\\]x[/\\]y[/\\]z[/\\]go.mod

cd $GOPATH/src/x/y/z/w
go env GOMOD
! stdout .
stdout $GOPATH[/\\]src[/\\]x[/\\]y[/\\]z[/\\]go.mod

cd $GOPATH/src/x/y
go env GOMOD
Expand All @@ -25,6 +25,17 @@ cd $GOPATH/foo/bar/baz
go env GOMOD
stdout foo[/\\]go.mod

# GO111MODULE unset should be equivalent to auto.
env GO111MODULE=

cd $GOPATH/src/x/y/z
go env GOMOD
stdout $GOPATH[/\\]src[/\\]x[/\\]y[/\\]z[/\\]go.mod

cd $GOPATH/src/x/y
go env GOMOD
! stdout .

# GO111MODULE=on should trigger everywhere
env GO111MODULE=on

Expand Down
10 changes: 2 additions & 8 deletions src/cmd/go/testdata/script/mod_find.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
env GO111MODULE=auto
env GO111MODULE=on

# Derive module path from import comment.
cd $WORK/x
Expand All @@ -12,14 +12,8 @@ addcrlf x.go
go mod init
stderr 'module x'

# go mod should die in GOPATH if modules are not enabled for GOPATH
cd $GOPATH/src/example.com/x/y
! go mod init
stderr 'go: modules disabled inside GOPATH/src by GO111MODULE=auto; see ''go help modules'''

env GO111MODULE=

# Derive module path from location inside GOPATH.
# 'go mod init' should succeed if modules are not explicitly disabled.
cd $GOPATH/src/example.com/x/y
go mod init
stderr 'module example.com/x/y$'
Expand Down
10 changes: 0 additions & 10 deletions src/cmd/go/testdata/script/mod_get_warning.txt

This file was deleted.

2 changes: 1 addition & 1 deletion src/cmd/go/testdata/script/mod_gobuild_import.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ env GO111MODULE=off

# GO111MODULE=auto in GOPATH/src
env GO111MODULE=auto
! exec $WORK/testimport.exe x/y/z/w .
exec $WORK/testimport.exe x/y/z/w .

# GO111MODULE=auto outside GOPATH/src
cd $GOPATH/other
Expand Down
6 changes: 2 additions & 4 deletions src/cmd/go/testdata/script/mod_off_init.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# 'go mod init' should refuse to initialize a module if it will be
# ignored anyway due to GO111MODULE=off.
env GO111MODULE=off

# This script tests that running go mod init with
# GO111MODULE=off when outside of GOPATH will fatal
# with an error message.
! go mod init
stderr 'go mod init: modules disabled by GO111MODULE=off; see ''go help modules'''

0 comments on commit 5b4ea62

Please sign in to comment.