// 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 {
// 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()
var (
cwd string // TODO(bcmills): Is this redundant with base.Cwd?
- mustUseModules = true
+ mustUseModules = false
initialized bool
modRoot 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
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
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
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)
[!net] skip
+env GO111MODULE=on
env GOPROXY=
! go get -d vcs-test.golang.org/insecure/go/insecure
-# 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
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
-env GO111MODULE=auto
+env GO111MODULE=on
# Derive module path from import comment.
cd $WORK/x
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$'
+++ /dev/null
-# 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
# 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
+# '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'''