build: Use Go's default GOPATH if it's valid

As of Go 1.8 it's valid to not have a GOPATH set. We ask the Go tool
what the GOPATH seems to be, and if we find ourselves (or a valid copy
of ourselves...) in that location we do not fiddle with the GOPATH.
This commit is contained in:
Jakob Borg 2017-07-16 21:38:57 +01:00
parent 6623657ef3
commit cc63236a2e

View File

@ -201,7 +201,7 @@ func main() {
}()
}
if os.Getenv("GOPATH") == "" {
if gopath() == "" {
gopath, err := temporaryBuildDir()
if err != nil {
log.Fatal(err)
@ -1143,3 +1143,26 @@ func buildGOPATH(gopath string) error {
return nil
}
func gopath() string {
if gopath := os.Getenv("GOPATH"); gopath != "" {
// The env var is set, use that.
return gopath
}
// Ask Go what it thinks.
bs, err := runError("go", "env", "GOPATH")
if err != nil {
return ""
}
// We got something. Check if we are in fact available in that location.
gopath := string(bs)
if _, err := os.Stat(filepath.Join(gopath, "src/github.com/syncthing/syncthing/build.go")); err == nil {
// That seems to be the gopath.
return gopath
}
// The gopath is not valid.
return ""
}