From eac6fe082ba0e54b387eca7604811958fe62a094 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Sat, 28 Oct 2017 14:23:21 -0400 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor Reviewed-by: David Crawshaw --- src/cmd/go/internal/cfg/cfg.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/cmd/go/internal/cfg/cfg.go b/src/cmd/go/internal/cfg/cfg.go index 290757fdb4..3c7b918523 100644 --- a/src/cmd/go/internal/cfg/cfg.go +++ b/src/cmd/go/internal/cfg/cfg.go @@ -100,22 +100,41 @@ func findGOROOT() string { if env := os.Getenv("GOROOT"); env != "" { return filepath.Clean(env) } + def := filepath.Clean(runtime.GOROOT()) exe, err := os.Executable() if err == nil { exe, err = filepath.Abs(exe) if err == nil { 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 } exe, err = filepath.EvalSymlinks(exe) if err == nil { if dir := filepath.Join(exe, "../.."); isGOROOT(dir) { + if isSameDir(def, dir) { + return def + } 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.