]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: fix error message for go mod in GOPATH mode
authorConstantin Konstantinidis <constantinkonstantinidis@gmail.com>
Mon, 20 May 2019 04:58:15 +0000 (06:58 +0200)
committerJay Conrod <jayconrod@google.com>
Thu, 30 May 2019 19:02:47 +0000 (19:02 +0000)
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>
src/cmd/go/internal/modcmd/graph.go
src/cmd/go/internal/modcmd/verify.go
src/cmd/go/testdata/script/mod_off.txt [new file with mode: 0644]

index 5825c6d8ca8e0b5478e8db2337278dd1f0811a96..8fcb84f2801e658a7441e186b207a72d5c9685a6 100644 (file)
@@ -8,6 +8,7 @@ package modcmd
 
 import (
        "bufio"
+       "cmd/go/internal/cfg"
        "os"
        "sort"
 
@@ -33,6 +34,14 @@ func runGraph(cmd *base.Command, args []string) {
        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()
index 381c18d58f1f607590599271160ffa0b42a37409..81fc44dc97ae609cffc8e43b56ddaa1d9435f9e4 100644 (file)
@@ -6,6 +6,7 @@ package modcmd
 
 import (
        "bytes"
+       "cmd/go/internal/cfg"
        "fmt"
        "io/ioutil"
        "os"
@@ -36,6 +37,14 @@ func runVerify(cmd *base.Command, args []string) {
                // 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
diff --git a/src/cmd/go/testdata/script/mod_off.txt b/src/cmd/go/testdata/script/mod_off.txt
new file mode 100644 (file)
index 0000000..bc0a786
--- /dev/null
@@ -0,0 +1,31 @@
+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