Commit eca5c83a authored by Bryan C. Mills's avatar Bryan C. Mills

cmd/go: change the default value of GO111MODULE to 'on'

This reverts CL 166985, restoring CL 162698.

The bootstrap failure from CL 162698 was fixed in
CL 167077 and CL 167078.

Fixes #30228

Change-Id: I5a4e3081018c51b74b67185e64f20a9c824a564e
Reviewed-on: https://go-review.googlesource.com/c/go/+/167087
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarJay Conrod <jayconrod@google.com>
parent 259de393
This diff is collapsed.
...@@ -12,9 +12,14 @@ import ( ...@@ -12,9 +12,14 @@ import (
"testing" "testing"
"cmd/go/internal/help" "cmd/go/internal/help"
"cmd/go/internal/modload"
) )
func TestDocsUpToDate(t *testing.T) { func TestDocsUpToDate(t *testing.T) {
if !modload.Enabled() {
t.Skipf("help.Help in GOPATH mode is configured by main.main")
}
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
// Match the command in mkalldocs.sh that generates alldocs.go. // Match the command in mkalldocs.sh that generates alldocs.go.
help.Help(buf, []string{"documentation"}) help.Help(buf, []string{"documentation"})
......
...@@ -17,6 +17,7 @@ import ( ...@@ -17,6 +17,7 @@ import (
"unicode/utf8" "unicode/utf8"
"cmd/go/internal/base" "cmd/go/internal/base"
"cmd/go/internal/modload"
) )
// Help implements the 'help' command. // Help implements the 'help' command.
...@@ -35,8 +36,10 @@ func Help(w io.Writer, args []string) { ...@@ -35,8 +36,10 @@ func Help(w io.Writer, args []string) {
usage := &base.Command{Long: buf.String()} usage := &base.Command{Long: buf.String()}
cmds := []*base.Command{usage} cmds := []*base.Command{usage}
for _, cmd := range base.Go.Commands { for _, cmd := range base.Go.Commands {
if cmd.UsageLine == "gopath-get" {
// Avoid duplication of the "get" documentation. // Avoid duplication of the "get" documentation.
if cmd.UsageLine == "module-get" && modload.Enabled() {
continue
} else if cmd.UsageLine == "gopath-get" && !modload.Enabled() {
continue continue
} }
cmds = append(cmds, cmd) cmds = append(cmds, cmd)
......
...@@ -19,34 +19,25 @@ including recording and resolving dependencies on other modules. ...@@ -19,34 +19,25 @@ including recording and resolving dependencies on other modules.
Modules replace the old GOPATH-based approach to specifying Modules replace the old GOPATH-based approach to specifying
which source files are used in a given build. which source files are used in a given build.
Preliminary module support Module support
Go 1.11 includes preliminary support for Go modules, Go 1.13 includes official support for Go modules,
including a new module-aware 'go get' command. including a module-aware 'go get' command.
We intend to keep revising this support, while preserving compatibility, Module-aware mode is active by default.
until it can be declared official (no longer preliminary),
and then at a later point we may remove support for work
in GOPATH and the old 'go get' command.
The quickest way to take advantage of the new Go 1.11 module support For more fine-grained control, Go 1.13 continues to respect
is to check out your repository into a directory outside GOPATH/src,
create a go.mod file (described in the next section) there, and run
go commands from within that file tree.
For more fine-grained control, the module support in Go 1.11 respects
a temporary environment variable, GO111MODULE, which can be set to one a temporary environment variable, GO111MODULE, which can be set to one
of three string values: off, on, or auto (the default). of three string values: off, auto, or on (the default).
If GO111MODULE=off, then the go command never uses the If GO111MODULE=on or is unset, then the go command requires the use of
new module support. Instead it looks in vendor directories and GOPATH modules, never consulting GOPATH. We refer to this as the command
being module-aware or running in "module-aware mode".
If GO111MODULE=auto, then the go command enables or disables module
support based on the current directory. Module support is enabled only
when the current directory is outside GOPATH/src and itself contains a
go.mod file or is below a directory containing a go.mod file.
If GO111MODULE=off, then the go command never uses
module support. Instead it looks in vendor directories and GOPATH
to find dependencies; we now refer to this as "GOPATH mode." to find dependencies; we now refer to this as "GOPATH mode."
If GO111MODULE=on, then the go command requires the use of modules,
never consulting GOPATH. We refer to this as the command being
module-aware or running in "module-aware mode".
If GO111MODULE=auto or is unset, then the go command enables or
disables module support based on the current directory.
Module support is enabled only when the current directory is outside
GOPATH/src and itself contains a go.mod file or is below a directory
containing a go.mod file.
In module-aware mode, GOPATH no longer defines the meaning of imports In module-aware mode, GOPATH no longer defines the meaning of imports
during a build, but it still stores downloaded dependencies (in GOPATH/pkg/mod) during a build, but it still stores downloaded dependencies (in GOPATH/pkg/mod)
......
...@@ -33,7 +33,7 @@ import ( ...@@ -33,7 +33,7 @@ import (
var ( var (
cwd string // TODO(bcmills): Is this redundant with base.Cwd? cwd string // TODO(bcmills): Is this redundant with base.Cwd?
MustUseModules = mustUseModules() mustUseModules = true
initialized bool initialized bool
modRoot string modRoot string
...@@ -78,16 +78,6 @@ func BinDir() string { ...@@ -78,16 +78,6 @@ func BinDir() string {
return filepath.Join(gopath, "bin") return filepath.Join(gopath, "bin")
} }
// mustUseModules reports whether we are invoked as vgo
// (as opposed to go).
// If so, we only support builds with go.mod files.
func mustUseModules() bool {
name := os.Args[0]
name = name[strings.LastIndex(name, "/")+1:]
name = name[strings.LastIndex(name, `\`)+1:]
return strings.HasPrefix(name, "vgo")
}
var inGOPATH bool // running in GOPATH/src var inGOPATH bool // running in GOPATH/src
// Init determines whether module mode is enabled, locates the root of the // Init determines whether module mode is enabled, locates the root of the
...@@ -104,15 +94,14 @@ func Init() { ...@@ -104,15 +94,14 @@ func Init() {
switch env { switch env {
default: default:
base.Fatalf("go: unknown environment setting GO111MODULE=%s", env) base.Fatalf("go: unknown environment setting GO111MODULE=%s", env)
case "", "auto": case "auto":
// leave MustUseModules alone mustUseModules = false
case "on": case "on", "":
MustUseModules = true mustUseModules = true
case "off": case "off":
if !MustUseModules { mustUseModules = false
return return
} }
}
// Disable any prompting for passwords by Git. // Disable any prompting for passwords by Git.
// Only has an effect for 2.3.0 or later, but avoiding // Only has an effect for 2.3.0 or later, but avoiding
...@@ -158,7 +147,7 @@ func Init() { ...@@ -158,7 +147,7 @@ func Init() {
} }
} }
if inGOPATH && !MustUseModules { if inGOPATH && !mustUseModules {
if CmdModInit { if CmdModInit {
die() // Don't init a module that we're just going to ignore. die() // Don't init a module that we're just going to ignore.
} }
...@@ -175,8 +164,8 @@ func Init() { ...@@ -175,8 +164,8 @@ func Init() {
} else { } else {
modRoot = findModuleRoot(cwd) modRoot = findModuleRoot(cwd)
if modRoot == "" { if modRoot == "" {
if !MustUseModules { if !mustUseModules {
// GO111MODULE is 'auto' (or unset), and we can't find a module root. // GO111MODULE is 'auto', and we can't find a module root.
// Stay in GOPATH mode. // Stay in GOPATH mode.
return return
} }
...@@ -275,7 +264,7 @@ func init() { ...@@ -275,7 +264,7 @@ func init() {
// (usually through MustModRoot). // (usually through MustModRoot).
func Enabled() bool { func Enabled() bool {
Init() Init()
return modRoot != "" || MustUseModules return modRoot != "" || mustUseModules
} }
// ModRoot returns the root of the main module. // ModRoot returns the root of the main module.
...@@ -308,7 +297,7 @@ func die() { ...@@ -308,7 +297,7 @@ func die() {
if os.Getenv("GO111MODULE") == "off" { if os.Getenv("GO111MODULE") == "off" {
base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'") base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'")
} }
if inGOPATH && !MustUseModules { if inGOPATH && !mustUseModules {
base.Fatalf("go: modules disabled inside GOPATH/src by GO111MODULE=auto; see 'go help modules'") base.Fatalf("go: modules disabled inside GOPATH/src by GO111MODULE=auto; see 'go help modules'")
} }
if cwd != "" { if cwd != "" {
......
...@@ -49,7 +49,7 @@ func init() { ...@@ -49,7 +49,7 @@ func init() {
fix.CmdFix, fix.CmdFix,
fmtcmd.CmdFmt, fmtcmd.CmdFmt,
generate.CmdGenerate, generate.CmdGenerate,
get.CmdGet, modget.CmdGet,
work.CmdInstall, work.CmdInstall,
list.CmdList, list.CmdList,
modcmd.CmdMod, modcmd.CmdMod,
...@@ -89,17 +89,10 @@ func main() { ...@@ -89,17 +89,10 @@ func main() {
base.Usage() base.Usage()
} }
if modload.MustUseModules {
// If running with modules force-enabled, change get now to change help message.
*get.CmdGet = *modget.CmdGet
}
if args[0] == "get" || args[0] == "help" { if args[0] == "get" || args[0] == "help" {
// Replace get with module-aware get if appropriate. if modload.Init(); !modload.Enabled() {
// Note that if MustUseModules is true, this happened already above, // Replace module-aware get with GOPATH get if appropriate.
// but no harm in doing it again. *modget.CmdGet = *get.CmdGet
if modload.Init(); modload.Enabled() {
*get.CmdGet = *modget.CmdGet
} }
} }
......
...@@ -8,6 +8,6 @@ set -e ...@@ -8,6 +8,6 @@ set -e
go build -o go.latest go build -o go.latest
# If the command used to generate alldocs.go changes, update TestDocsUpToDate in # If the command used to generate alldocs.go changes, update TestDocsUpToDate in
# help_test.go. # help_test.go.
./go.latest help documentation >alldocs.go GO111MODULE='' ./go.latest help documentation >alldocs.go
gofmt -w alldocs.go gofmt -w alldocs.go
rm go.latest rm go.latest
...@@ -17,7 +17,7 @@ cd $GOPATH/src/example.com/x/y ...@@ -17,7 +17,7 @@ cd $GOPATH/src/example.com/x/y
! go mod init ! go mod init
stderr 'go: modules disabled inside GOPATH/src by GO111MODULE=auto; see ''go help modules''' stderr 'go: modules disabled inside GOPATH/src by GO111MODULE=auto; see ''go help modules'''
env GO111MODULE=on env GO111MODULE=
# Derive module path from location inside GOPATH. # Derive module path from location inside GOPATH.
cd $GOPATH/src/example.com/x/y cd $GOPATH/src/example.com/x/y
......
...@@ -24,12 +24,18 @@ exec $WORK/testimport.exe other/x/y/z/w . ...@@ -24,12 +24,18 @@ exec $WORK/testimport.exe other/x/y/z/w .
stdout w2.go stdout w2.go
# GO111MODULE=on outside GOPATH/src # GO111MODULE=on outside GOPATH/src
env GO111MODULE=
exec $WORK/testimport.exe other/x/y/z/w .
stdout w2.go
env GO111MODULE=on env GO111MODULE=on
exec $WORK/testimport.exe other/x/y/z/w . exec $WORK/testimport.exe other/x/y/z/w .
stdout w2.go stdout w2.go
# GO111MODULE=on in GOPATH/src # GO111MODULE=on in GOPATH/src
cd $GOPATH/src cd $GOPATH/src
env GO111MODULE=
exec $WORK/testimport.exe x/y/z/w .
stdout w1.go
env GO111MODULE=on env GO111MODULE=on
exec $WORK/testimport.exe x/y/z/w . exec $WORK/testimport.exe x/y/z/w .
stdout w1.go stdout w1.go
......
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