]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/cfg: improve error message when GOPATH is unset
authorsiddharth <s@ricketyspace.net>
Mon, 28 Jun 2021 23:36:53 +0000 (19:36 -0400)
committerBryan C. Mills <bcmills@google.com>
Mon, 4 Oct 2021 18:58:42 +0000 (18:58 +0000)
Add GoPathError variable. This variable gets set when GOPATH is not
set in the environment and in build.Default.GOPATH. GoPathError may
be used in "GOPATH unset" error messages to explain why GOPATH is not
set.

This CL improves upon CL 158257.

Fixes #29341

Change-Id: Ib42b42fb442c8297d58da4ca556be55e21a034e1
Reviewed-on: https://go-review.googlesource.com/c/go/+/331529
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Jay Conrod <jayconrod@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Jay Conrod <jayconrod@google.com>
Trust: Michael Knyszek <mknyszek@google.com>

src/cmd/go/internal/cfg/cfg.go
src/cmd/go/internal/modfetch/sumdb.go

index b9632ea3c9db40be04a158e4c8e54bd12e746cde..c8747d6c1109972e3364f8a942a0508f673bd733 100644 (file)
@@ -56,6 +56,10 @@ var (
 
        DebugActiongraph string // -debug-actiongraph flag (undocumented, unstable)
        DebugTrace       string // -debug-trace flag
+
+       // GoPathError is set when GOPATH is not set. it contains an
+       // explanation why GOPATH is unset.
+       GoPathError string
 )
 
 func defaultContext() build.Context {
@@ -73,7 +77,7 @@ func defaultContext() build.Context {
                build.ToolDir = filepath.Join(ctxt.GOROOT, "pkg/tool/"+runtime.GOOS+"_"+runtime.GOARCH)
        }
 
-       ctxt.GOPATH = envOr("GOPATH", ctxt.GOPATH)
+       ctxt.GOPATH = envOr("GOPATH", gopath(ctxt))
 
        // Override defaults computed in go/build with defaults
        // from go environment configuration file, if known.
@@ -402,3 +406,24 @@ func gopathDir(rel string) string {
        }
        return filepath.Join(list[0], rel)
 }
+
+func gopath(ctxt build.Context) string {
+       if len(ctxt.GOPATH) > 0 {
+               return ctxt.GOPATH
+       }
+       env := "HOME"
+       if runtime.GOOS == "windows" {
+               env = "USERPROFILE"
+       } else if runtime.GOOS == "plan9" {
+               env = "home"
+       }
+       if home := os.Getenv(env); home != "" {
+               def := filepath.Join(home, "go")
+               if filepath.Clean(def) == filepath.Clean(runtime.GOROOT()) {
+                       GoPathError = "cannot set GOROOT as GOPATH"
+               }
+               return ""
+       }
+       GoPathError = fmt.Sprintf("%s is not set", env)
+       return ""
+}
index f233cba6df1bb7e996735147834e9c40960158f4..79961b432484e2c9525a4f15ded6b314da69e759 100644 (file)
@@ -201,7 +201,8 @@ func (c *dbClient) ReadConfig(file string) (data []byte, err error) {
        }
 
        if cfg.SumdbDir == "" {
-               return nil, errors.New("could not locate sumdb file: missing $GOPATH")
+               return nil, fmt.Errorf("could not locate sumdb file: missing $GOPATH: %s",
+                       cfg.GoPathError)
        }
        targ := filepath.Join(cfg.SumdbDir, file)
        data, err = lockedfile.Read(targ)
@@ -220,7 +221,8 @@ func (*dbClient) WriteConfig(file string, old, new []byte) error {
                return fmt.Errorf("cannot write key")
        }
        if cfg.SumdbDir == "" {
-               return errors.New("could not locate sumdb file: missing $GOPATH")
+               return fmt.Errorf("could not locate sumdb file: missing $GOPATH: %s",
+                       cfg.GoPathError)
        }
        targ := filepath.Join(cfg.SumdbDir, file)
        os.MkdirAll(filepath.Dir(targ), 0777)