Commit eac6fe08 authored by Russ Cox's avatar Russ Cox

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: default avatarIan Lance Taylor <iant@golang.org>
Reviewed-by: default avatarDavid Crawshaw <crawshaw@golang.org>
parent 164e1b84
...@@ -100,22 +100,41 @@ func findGOROOT() string { ...@@ -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.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment