Skip to content

Commit

Permalink
cmd/go: change the default value of GO111MODULE to 'on'
Browse files Browse the repository at this point in the history
Fixes #30228

Change-Id: Ie45ba6483849b843eb6605272f686b9deffe5e48
Reviewed-on: https://go-review.googlesource.com/c/go/+/162698
Reviewed-by: Jay Conrod <[email protected]>
  • Loading branch information
Bryan C. Mills committed Mar 11, 2019
1 parent c5cf662 commit cf46916
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 232 deletions.
331 changes: 161 additions & 170 deletions src/cmd/go/alldocs.go

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/cmd/go/help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ import (
"testing"

"cmd/go/internal/help"
"cmd/go/internal/modload"
)

func TestDocsUpToDate(t *testing.T) {
if !modload.Enabled() {
t.Skipf("help.Help in GOPATH mode is configured by main.main")
}

buf := new(bytes.Buffer)
// Match the command in mkalldocs.sh that generates alldocs.go.
help.Help(buf, []string{"documentation"})
Expand Down
7 changes: 5 additions & 2 deletions src/cmd/go/internal/help/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"unicode/utf8"

"cmd/go/internal/base"
"cmd/go/internal/modload"
)

// Help implements the 'help' command.
Expand All @@ -35,8 +36,10 @@ func Help(w io.Writer, args []string) {
usage := &base.Command{Long: buf.String()}
cmds := []*base.Command{usage}
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
}
cmds = append(cmds, cmd)
Expand Down
39 changes: 15 additions & 24 deletions src/cmd/go/internal/modload/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,25 @@ including recording and resolving dependencies on other modules.
Modules replace the old GOPATH-based approach to specifying
which source files are used in a given build.
Preliminary module support
Module support
Go 1.11 includes preliminary support for Go modules,
including a new module-aware 'go get' command.
We intend to keep revising this support, while preserving compatibility,
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.
Go 1.13 includes official support for Go modules,
including a module-aware 'go get' command.
Module-aware mode is active by default.
The quickest way to take advantage of the new Go 1.11 module support
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
For more fine-grained control, Go 1.13 continues to respect
a temporary environment variable, GO111MODULE, which can be set to one
of three string values: off, on, or auto (the default).
If GO111MODULE=off, then the go command never uses the
new module support. Instead it looks in vendor directories and GOPATH
of three string values: off, auto, or on (the default).
If GO111MODULE=on or is unset, 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, 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."
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
during a build, but it still stores downloaded dependencies (in GOPATH/pkg/mod)
Expand Down
35 changes: 12 additions & 23 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 = mustUseModules()
mustUseModules = true
initialized bool

modRoot string
Expand Down Expand Up @@ -70,16 +70,6 @@ func BinDir() string {
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

// Init determines whether module mode is enabled, locates the root of the
Expand All @@ -96,14 +86,13 @@ func Init() {
switch env {
default:
base.Fatalf("go: unknown environment setting GO111MODULE=%s", env)
case "", "auto":
// leave MustUseModules alone
case "on":
MustUseModules = true
case "auto":
mustUseModules = false
case "on", "":
mustUseModules = true
case "off":
if !MustUseModules {
return
}
mustUseModules = false
return
}

// Disable any prompting for passwords by Git.
Expand Down Expand Up @@ -150,7 +139,7 @@ func Init() {
}
}

if inGOPATH && !MustUseModules {
if inGOPATH && !mustUseModules {
if CmdModInit {
die() // Don't init a module that we're just going to ignore.
}
Expand All @@ -167,8 +156,8 @@ func Init() {
} else {
modRoot = findModuleRoot(cwd)
if modRoot == "" {
if !MustUseModules {
// GO111MODULE is 'auto' (or unset), and we can't find a module root.
if !mustUseModules {
// GO111MODULE is 'auto', and we can't find a module root.
// Stay in GOPATH mode.
return
}
Expand Down Expand Up @@ -267,7 +256,7 @@ func init() {
// (usually through MustModRoot).
func Enabled() bool {
Init()
return modRoot != "" || MustUseModules
return modRoot != "" || mustUseModules
}

// ModRoot returns the root of the main module.
Expand Down Expand Up @@ -300,7 +289,7 @@ func die() {
if os.Getenv("GO111MODULE") == "off" {
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'")
}
if cwd != "" {
Expand Down
15 changes: 4 additions & 11 deletions src/cmd/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func init() {
fix.CmdFix,
fmtcmd.CmdFmt,
generate.CmdGenerate,
get.CmdGet,
modget.CmdGet,
work.CmdInstall,
list.CmdList,
modcmd.CmdMod,
Expand Down Expand Up @@ -89,17 +89,10 @@ func main() {
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" {
// Replace get with module-aware get if appropriate.
// Note that if MustUseModules is true, this happened already above,
// but no harm in doing it again.
if modload.Init(); modload.Enabled() {
*get.CmdGet = *modget.CmdGet
if modload.Init(); !modload.Enabled() {
// Replace module-aware get with GOPATH get if appropriate.
*modget.CmdGet = *get.CmdGet
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/go/mkalldocs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ set -e
go build -o go.latest
# If the command used to generate alldocs.go changes, update TestDocsUpToDate in
# help_test.go.
./go.latest help documentation >alldocs.go
GO111MODULE='' ./go.latest help documentation >alldocs.go
gofmt -w alldocs.go
rm go.latest
2 changes: 1 addition & 1 deletion src/cmd/go/testdata/script/mod_find.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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=on
env GO111MODULE=

# Derive module path from location inside GOPATH.
cd $GOPATH/src/example.com/x/y
Expand Down
6 changes: 6 additions & 0 deletions src/cmd/go/testdata/script/mod_gobuild_import.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@ exec $WORK/testimport.exe other/x/y/z/w .
stdout w2.go

# GO111MODULE=on outside GOPATH/src
env GO111MODULE=
exec $WORK/testimport.exe other/x/y/z/w .
stdout w2.go
env GO111MODULE=on
exec $WORK/testimport.exe other/x/y/z/w .
stdout w2.go

# GO111MODULE=on in GOPATH/src
cd $GOPATH/src
env GO111MODULE=
exec $WORK/testimport.exe x/y/z/w .
stdout w1.go
env GO111MODULE=on
exec $WORK/testimport.exe x/y/z/w .
stdout w1.go
Expand Down

0 comments on commit cf46916

Please sign in to comment.