Checks if modules are enabled in GOPATH mode for go mod [graph, verify].
Added tests for GO111MODULE=[auto, off].
Fixes: #31237
Change-Id: I91efccfa10d0b2385ec2af1ea133deaa8234ba37
Reviewed-on: https://go-review.googlesource.com/c/go/+/174697
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Jay Conrod <jayconrod@google.com>
import (
"bufio"
+ "cmd/go/internal/cfg"
"os"
"sort"
if len(args) > 0 {
base.Fatalf("go mod graph: graph takes no arguments")
}
+ // Checks go mod expected behavior
+ if !modload.Enabled() {
+ if cfg.Getenv("GO111MODULE") == "off" {
+ base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'")
+ } else {
+ base.Fatalf("go: cannot find main module; see 'go help modules'")
+ }
+ }
modload.LoadBuildList()
reqs := modload.MinReqs()
import (
"bytes"
+ "cmd/go/internal/cfg"
"fmt"
"io/ioutil"
"os"
// NOTE(rsc): Could take a module pattern.
base.Fatalf("go mod verify: verify takes no arguments")
}
+ // Checks go mod expected behavior
+ if !modload.Enabled() {
+ if cfg.Getenv("GO111MODULE") == "off" {
+ base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'")
+ } else {
+ base.Fatalf("go: cannot find main module; see 'go help modules'")
+ }
+ }
ok := true
for _, mod := range modload.LoadBuildList()[1:] {
ok = verifyMod(mod) && ok
--- /dev/null
+env GO111MODULE=off
+
+# This script tests that running go mod with
+# GO111MODULE=off when outside of GOPATH will fatal
+# with an error message, even with some source code in the directory and a go.mod.
+! go mod init
+stderr 'go mod init: modules disabled by GO111MODULE=off; see ''go help modules'''
+! go mod graph
+stderr 'go: modules disabled by GO111MODULE=off; see ''go help modules'''
+! go mod verify
+stderr 'go: modules disabled by GO111MODULE=off; see ''go help modules'''
+
+# Same result in an empty directory
+mkdir z
+cd z
+! go mod init
+stderr 'go mod init: modules disabled by GO111MODULE=off; see ''go help modules'''
+! go mod graph
+stderr 'go: modules disabled by GO111MODULE=off; see ''go help modules'''
+! go mod verify
+stderr 'go: modules disabled by GO111MODULE=off; see ''go help modules'''
+
+-- sample.go --
+package sample
+
+func main() {}
+
+-- go.mod --
+module sample
+
+go 1.12