Skip to content

Commit

Permalink
cmd/go: proceed with GOPATH unset if the command doesn't use it
Browse files Browse the repository at this point in the history
For #43938

Change-Id: I0937b9bb6de3d29d7242ee61f053d4803277dc0f
Reviewed-on: https://go-review.googlesource.com/c/go/+/351329
Trust: Bryan C. Mills <[email protected]>
Run-TryBot: Bryan C. Mills <[email protected]>
TryBot-Result: Go Bot <[email protected]>
Reviewed-by: Jay Conrod <[email protected]>
  • Loading branch information
Bryan C. Mills committed Sep 22, 2021
1 parent ccf140f commit 5ee32ff
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/cmd/go/internal/modfetch/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 11 additions & 6 deletions src/cmd/go/internal/modload/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down Expand Up @@ -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() {
Expand Down
13 changes: 10 additions & 3 deletions src/cmd/go/testdata/script/mod_gomodcache.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
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=
Expand Down
15 changes: 15 additions & 0 deletions src/cmd/go/testdata/script/mod_no_gopath.txt
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 5ee32ff

Please sign in to comment.