1
0
mirror of https://github.com/golang/go synced 2024-11-23 18:50:05 -07:00

cmd/go: fix error message for go mod in GOPATH mode

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>
This commit is contained in:
Constantin Konstantinidis 2019-05-20 06:58:15 +02:00 committed by Jay Conrod
parent 220552f662
commit 7e05e97445
3 changed files with 49 additions and 0 deletions

View 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()

View 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

31
src/cmd/go/testdata/script/mod_off.txt vendored Normal file
View File

@ -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