Commit 7e05e974 authored by Constantin Konstantinidis's avatar Constantin Konstantinidis Committed by Jay Conrod

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: default avatarBryan C. Mills <bcmills@google.com>
Reviewed-by: default avatarJay Conrod <jayconrod@google.com>
parent 220552f6
...@@ -8,6 +8,7 @@ package modcmd ...@@ -8,6 +8,7 @@ package modcmd
import ( import (
"bufio" "bufio"
"cmd/go/internal/cfg"
"os" "os"
"sort" "sort"
...@@ -33,6 +34,14 @@ func runGraph(cmd *base.Command, args []string) { ...@@ -33,6 +34,14 @@ func runGraph(cmd *base.Command, args []string) {
if len(args) > 0 { if len(args) > 0 {
base.Fatalf("go mod graph: graph takes no arguments") 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() modload.LoadBuildList()
reqs := modload.MinReqs() reqs := modload.MinReqs()
......
...@@ -6,6 +6,7 @@ package modcmd ...@@ -6,6 +6,7 @@ package modcmd
import ( import (
"bytes" "bytes"
"cmd/go/internal/cfg"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
...@@ -36,6 +37,14 @@ func runVerify(cmd *base.Command, args []string) { ...@@ -36,6 +37,14 @@ func runVerify(cmd *base.Command, args []string) {
// NOTE(rsc): Could take a module pattern. // NOTE(rsc): Could take a module pattern.
base.Fatalf("go mod verify: verify takes no arguments") 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 ok := true
for _, mod := range modload.LoadBuildList()[1:] { for _, mod := range modload.LoadBuildList()[1:] {
ok = verifyMod(mod) && ok ok = verifyMod(mod) && ok
......
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
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