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
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()
......
......@@ -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
......
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