]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: proceed with GOPATH unset if the command doesn't use it
authorBryan C. Mills <bcmills@google.com>
Tue, 21 Sep 2021 20:05:57 +0000 (16:05 -0400)
committerBryan C. Mills <bcmills@google.com>
Wed, 22 Sep 2021 15:46:33 +0000 (15:46 +0000)
For #43938

Change-Id: I0937b9bb6de3d29d7242ee61f053d4803277dc0f
Reviewed-on: https://go-review.googlesource.com/c/go/+/351329
Trust: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
src/cmd/go/internal/modfetch/cache.go
src/cmd/go/internal/modload/init.go
src/cmd/go/testdata/script/mod_gomodcache.txt
src/cmd/go/testdata/script/mod_no_gopath.txt [new file with mode: 0644]

index b01b4674131e7af76436c5b5513b6dbc1c155cbe..8d299e931af8b126b4cbdb18d725e5f4f7852eef 100644 (file)
@@ -720,7 +720,7 @@ func checkCacheDir() error {
        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)
index a855e6c85179f2f81f28d99adbcafd8c51530026..83414feb3c5134176fd7fe7fd6105e02d1e9ce14 100644 (file)
@@ -246,6 +246,12 @@ func ModFile() *modfile.File {
 
 func BinDir() string {
        Init()
+       if cfg.GOBIN != "" {
+               return cfg.GOBIN
+       }
+       if gopath == "" {
+               return ""
+       }
        return filepath.Join(gopath, "bin")
 }
 
@@ -381,12 +387,11 @@ func Init() {
                "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() {
index 74a3c79622f1f5ff916edd64cb39facd386edee4..a9d7ab3f042b355114ef3123c940851943816548 100644 (file)
@@ -31,11 +31,18 @@ env GOPATH=
 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/quote@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 (file)
index 0000000..ed91f5d
--- /dev/null
@@ -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"