]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: adjust default GOROOT to prefer runtime.GOROOT() spelling
authorRuss Cox <rsc@golang.org>
Sat, 28 Oct 2017 18:23:21 +0000 (14:23 -0400)
committerRuss Cox <rsc@golang.org>
Sun, 29 Oct 2017 23:37:34 +0000 (23:37 +0000)
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>
src/cmd/go/internal/cfg/cfg.go

index 290757fdb476d2744d48370d100477556fe5f343..3c7b918523506fdea315b52b6444e84e3ce6e19d 100644 (file)
@@ -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.