From cc63236a2e2cd2546172459361367f77e03552db Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Sun, 16 Jul 2017 21:38:57 +0100 Subject: [PATCH] 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. --- build.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/build.go b/build.go index 914f29f4..9bd12f49 100644 --- a/build.go +++ b/build.go @@ -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 "" +}