1
0
mirror of https://github.com/golang/go synced 2024-11-19 18:54:41 -07:00

cmd/go: adjust default GOROOT to prefer runtime.GOROOT() spelling

If runtime.GOROOT() and the os.Executable method for finding GOROOT
find the same directory but with different spellings, prefer the spelling
returned by runtime.GOROOT().

This avoids an inconsistency if "pwd" returns one spelling but a
different spelling is used in $PATH (and therefore in os.Executable()).
make.bash runs with GOROOT=$(cd .. && pwd); the goal is to allow
the resulting toolchain to use that default setting (unless moved)
even if the directory spelling is different in $PATH.

Change-Id: If96b28b9e8697f4888f153a400b40bbf58a9128b
Reviewed-on: https://go-review.googlesource.com/74250
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: David Crawshaw <crawshaw@golang.org>
This commit is contained in:
Russ Cox 2017-10-28 14:23:21 -04:00
parent 164e1b8477
commit eac6fe082b

View File

@ -100,22 +100,41 @@ func findGOROOT() string {
if env := os.Getenv("GOROOT"); env != "" { if env := os.Getenv("GOROOT"); env != "" {
return filepath.Clean(env) return filepath.Clean(env)
} }
def := filepath.Clean(runtime.GOROOT())
exe, err := os.Executable() exe, err := os.Executable()
if err == nil { if err == nil {
exe, err = filepath.Abs(exe) exe, err = filepath.Abs(exe)
if err == nil { if err == nil {
if dir := filepath.Join(exe, "../.."); isGOROOT(dir) { if dir := filepath.Join(exe, "../.."); isGOROOT(dir) {
// If def (runtime.GOROOT()) and dir are the same
// directory, prefer the spelling used in def.
if isSameDir(def, dir) {
return def
}
return dir return dir
} }
exe, err = filepath.EvalSymlinks(exe) exe, err = filepath.EvalSymlinks(exe)
if err == nil { if err == nil {
if dir := filepath.Join(exe, "../.."); isGOROOT(dir) { if dir := filepath.Join(exe, "../.."); isGOROOT(dir) {
if isSameDir(def, dir) {
return def
}
return dir return dir
} }
} }
} }
} }
return filepath.Clean(runtime.GOROOT()) return def
}
// isSameDir reports whether dir1 and dir2 are the same directory.
func isSameDir(dir1, dir2 string) bool {
if dir1 == dir2 {
return true
}
info1, err1 := os.Stat(dir1)
info2, err2 := os.Stat(dir2)
return err1 == nil && err2 == nil && os.SameFile(info1, info2)
} }
// isGOROOT reports whether path looks like a GOROOT. // isGOROOT reports whether path looks like a GOROOT.