]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: default to GO111MODULE=auto and make it trigger in GOPATH/src
authorBryan C. Mills <bcmills@google.com>
Fri, 10 May 2019 15:21:51 +0000 (11:21 -0400)
committerBryan C. Mills <bcmills@google.com>
Tue, 14 May 2019 14:28:29 +0000 (14:28 +0000)
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>
src/cmd/go/internal/cfg/cfg.go
src/cmd/go/internal/get/get.go
src/cmd/go/internal/modload/init.go
src/cmd/go/testdata/script/get_insecure_redirect.txt
src/cmd/go/testdata/script/mod_enabled.txt
src/cmd/go/testdata/script/mod_find.txt
src/cmd/go/testdata/script/mod_get_warning.txt [deleted file]
src/cmd/go/testdata/script/mod_gobuild_import.txt
src/cmd/go/testdata/script/mod_off_init.txt

index ecb0db78ac41f420c7acc9a032ae13b0a2250a6f..2d17d104a6298ad17ae6be8b9ff1fd5250d60dd3 100644 (file)
@@ -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 {
index c70013c3f5cf028a1592399819d9571dd3e29342..e4945fe14404b7c69c640d743659af07a9c7479d 100644 (file)
@@ -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()
 
index ba90756346b40fe52ce76a0282a20563b8326d2b..c55f8e3760a84d82b2bba6231af3c93eff0acbcd 100644 (file)
@@ -34,7 +34,7 @@ import (
 
 var (
        cwd            string // TODO(bcmills): Is this redundant with base.Cwd?
-       mustUseModules = true
+       mustUseModules = false
        initialized    bool
 
        modRoot     string
@@ -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
@@ -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
@@ -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
@@ -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)
index e05ced67a3b4358d5a030321c732e062bf37da8d..6d20418fb2b5c7288a114d2963ad09e066a84b02 100644 (file)
@@ -2,6 +2,7 @@
 
 [!net] skip
 
+env GO111MODULE=on
 env GOPROXY=
 
 ! go get -d vcs-test.golang.org/insecure/go/insecure
index ab5ee3d6dfa656cb36e7a311d8699fd5d85788a5..10fa103fad391364b7fcc5632b65b6e066077d81 100644 (file)
@@ -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
@@ -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
 
index e82001a24ab19bf3a944b74683f7fb76a3c4676d..7fbe9fb7fe6ed1d8a35585650bd822061c3f7200 100644 (file)
@@ -1,4 +1,4 @@
-env GO111MODULE=auto
+env GO111MODULE=on
 
 # Derive module path from import comment.
 cd $WORK/x
@@ -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$'
diff --git a/src/cmd/go/testdata/script/mod_get_warning.txt b/src/cmd/go/testdata/script/mod_get_warning.txt
deleted file mode 100644 (file)
index 36b5434..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-# go get in GO111MODULE=auto should warn when not using modules and go.mod exists
-
-env GO111MODULE=auto
-mkdir z
-cd z
-! go get # fails because no code in directory, not the warning
-stderr 'go get: warning: modules disabled by GO111MODULE=auto in GOPATH/src;\n\tignoring ..[/\\]go.mod;\n\tsee ''go help modules'''
-
--- go.mod --
-module x
index 6c87d596492424ef065592b4cc0213b976fb0310..25764b797e6f650f34cf24aa4277dbbe03ae70f6 100644 (file)
@@ -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
index f9a4e10bd4446b668a371e7b61ef51922baa2cd1..1339c8aef9679ce1ecbed258cc9ef73d7348ca78 100644 (file)
@@ -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'''