Skip to content

Commit eac6fe0

Browse files
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: David Crawshaw <[email protected]>
1 parent 164e1b8 commit eac6fe0

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/cmd/go/internal/cfg/cfg.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,41 @@ func findGOROOT() string {
100100
if env := os.Getenv("GOROOT"); env != "" {
101101
return filepath.Clean(env)
102102
}
103+
def := filepath.Clean(runtime.GOROOT())
103104
exe, err := os.Executable()
104105
if err == nil {
105106
exe, err = filepath.Abs(exe)
106107
if err == nil {
107108
if dir := filepath.Join(exe, "../.."); isGOROOT(dir) {
109+
// If def (runtime.GOROOT()) and dir are the same
110+
// directory, prefer the spelling used in def.
111+
if isSameDir(def, dir) {
112+
return def
113+
}
108114
return dir
109115
}
110116
exe, err = filepath.EvalSymlinks(exe)
111117
if err == nil {
112118
if dir := filepath.Join(exe, "../.."); isGOROOT(dir) {
119+
if isSameDir(def, dir) {
120+
return def
121+
}
113122
return dir
114123
}
115124
}
116125
}
117126
}
118-
return filepath.Clean(runtime.GOROOT())
127+
return def
128+
}
129+
130+
// isSameDir reports whether dir1 and dir2 are the same directory.
131+
func isSameDir(dir1, dir2 string) bool {
132+
if dir1 == dir2 {
133+
return true
134+
}
135+
info1, err1 := os.Stat(dir1)
136+
info2, err2 := os.Stat(dir2)
137+
return err1 == nil && err2 == nil && os.SameFile(info1, info2)
119138
}
120139

121140
// isGOROOT reports whether path looks like a GOROOT.

0 commit comments

Comments
 (0)