Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #771: Handling default GOPATH for Go 1.8 #798

Merged
merged 1 commit into from
Apr 10, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 26 additions & 2 deletions path/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"

Expand Down Expand Up @@ -38,6 +39,24 @@ var GlideFile = DefaultGlideFile
// LockFile is the default name for the lock file.
const LockFile = "glide.lock"

func init() {

// As of Go 1.8 the GOPATH is no longer required to be set. Instead there
// is a default value. If there is no GOPATH check for the default value.
// Note, checking the GOPATH first to avoid invoking the go toolchain if
// possible.
if gopaths = os.Getenv("GOPATH"); len(gopaths) == 0 {
goExecutable := os.Getenv("GLIDE_GO_EXECUTABLE")
if len(goExecutable) <= 0 {
goExecutable = "go"
}
out, err := exec.Command(goExecutable, "env", "GOPATH").Output()
if err == nil {
gopaths = strings.TrimSpace(string(out))
}
}
}

// Home returns the Glide home directory ($GLIDE_HOME or ~/.glide, typically).
//
// This normalizes to an absolute path, and passes through os.ExpandEnv.
Expand Down Expand Up @@ -145,6 +164,12 @@ func GlideWD(dir string) (string, error) {
return GlideWD(base)
}

// Stores the gopaths so they do not get repeatedly looked up. This is especially
// true when the default value needs to be retrieved from `go env GOPATH`.
// TODO(mattfarina): Instead of a singleton an application context would be a
// better place to store things like this.
var gopaths string

// Gopath gets GOPATH from environment and return the most relevant path.
//
// A GOPATH can contain a colon-separated list of paths. This retrieves the
Expand All @@ -163,8 +188,7 @@ func Gopath() string {
// Gopaths retrieves the Gopath as a list when there is more than one path
// listed in the Gopath.
func Gopaths() []string {
p := os.Getenv("GOPATH")
p = strings.Trim(p, string(filepath.ListSeparator))
p := strings.Trim(gopaths, string(filepath.ListSeparator))
return filepath.SplitList(p)
}

Expand Down