Bryan C. Mills submitted this change.
2 is the latest approved patch-set.
No files were changed between the latest approved patch-set and the submitted one.
cmd/go: proceed with GOPATH unset if the command doesn't use it
For #43938
Change-Id: I0937b9bb6de3d29d7242ee61f053d4803277dc0f
Reviewed-on: https://go-review.googlesource.com/c/go/+/351329
Trust: Bryan C. Mills <bcm...@google.com>
Run-TryBot: Bryan C. Mills <bcm...@google.com>
TryBot-Result: Go Bot <go...@golang.org>
Reviewed-by: Jay Conrod <jayc...@google.com>
---
M src/cmd/go/internal/modfetch/cache.go
M src/cmd/go/internal/modload/init.go
M src/cmd/go/testdata/script/mod_gomodcache.txt
A src/cmd/go/testdata/script/mod_no_gopath.txt
4 files changed, 37 insertions(+), 10 deletions(-)
diff --git a/src/cmd/go/internal/modfetch/cache.go b/src/cmd/go/internal/modfetch/cache.go
index b01b467..8d299e9 100644
--- a/src/cmd/go/internal/modfetch/cache.go
+++ b/src/cmd/go/internal/modfetch/cache.go
@@ -720,7 +720,7 @@
if cfg.GOMODCACHE == "" {
// modload.Init exits if GOPATH[0] is empty, and cfg.GOMODCACHE
// is set to GOPATH[0]/pkg/mod if GOMODCACHE is empty, so this should never happen.
- return fmt.Errorf("internal error: cfg.GOMODCACHE not set")
+ return fmt.Errorf("module cache not found: neither GOMODCACHE nor GOPATH is set")
}
if !filepath.IsAbs(cfg.GOMODCACHE) {
return fmt.Errorf("GOMODCACHE entry is relative; must be absolute path: %q.\n", cfg.GOMODCACHE)
diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go
index a855e6c..83414fe 100644
--- a/src/cmd/go/internal/modload/init.go
+++ b/src/cmd/go/internal/modload/init.go
@@ -246,6 +246,12 @@
func BinDir() string {
Init()
+ if cfg.GOBIN != "" {
+ return cfg.GOBIN
+ }
+ if gopath == "" {
+ return ""
+ }
return filepath.Join(gopath, "bin")
}
@@ -381,12 +387,11 @@
"verify, graph, and why. Implement support for go mod download and add test cases" +
"to ensure verify, graph, and why work properly.")
list := filepath.SplitList(cfg.BuildContext.GOPATH)
- if len(list) == 0 || list[0] == "" {
- base.Fatalf("missing $GOPATH")
- }
- gopath = list[0]
- if _, err := fsys.Stat(filepath.Join(gopath, "go.mod")); err == nil {
- base.Fatalf("$GOPATH/go.mod exists but should not")
+ if len(list) > 0 && list[0] != "" {
+ gopath = list[0]
+ if _, err := fsys.Stat(filepath.Join(gopath, "go.mod")); err == nil {
+ base.Fatalf("$GOPATH/go.mod exists but should not")
+ }
}
if inWorkspaceMode() {
diff --git a/src/cmd/go/testdata/script/mod_gomodcache.txt b/src/cmd/go/testdata/script/mod_gomodcache.txt
index 74a3c79..a9d7ab3 100644
--- a/src/cmd/go/testdata/script/mod_gomodcache.txt
+++ b/src/cmd/go/testdata/script/mod_gomodcache.txt
@@ -31,11 +31,18 @@
go env GOMODCACHE
stdout $HOME[/\\]go[/\\]pkg[/\\]mod
-# If GOMODCACHE isn't set and GOPATH starts with the path list separator, it's an error.
+# If GOMODCACHE isn't set and GOPATH starts with the path list separator,
+# GOMODCACHE is empty and any command that needs it errors out.
env GOMODCACHE=
env GOPATH=${:}$WORK/this/is/ignored
-! go env GOMODCACHE
-stderr 'missing \$GOPATH'
+
+go env GOMODCACHE
+stdout '^$'
+! stdout .
+! stderr .
+
+! go mod download rsc.io/qu...@v1.0.0
+stderr '^go: module cache not found: neither GOMODCACHE nor GOPATH is set$'
# If GOMODCACHE isn't set and GOPATH has multiple elements only the first is used.
env GOMODCACHE=
diff --git a/src/cmd/go/testdata/script/mod_no_gopath.txt b/src/cmd/go/testdata/script/mod_no_gopath.txt
new file mode 100644
index 0000000..ed91f5d
--- /dev/null
+++ b/src/cmd/go/testdata/script/mod_no_gopath.txt
@@ -0,0 +1,15 @@
+# https://golang.org/issue/43938: 'go build' should succeed
+# if GOPATH and the variables needed for its default value
+# are all unset but not relevant to the specific command.
+
+env HOME=''
+env home=''
+env GOPATH=''
+
+go list -deps main.go
+stdout '^io$'
+
+-- main.go --
+package main
+
+import _ "io"
To view, visit change 351329. To unsubscribe, or for help writing mail filters, visit settings.