From 79dbfd6c86b7839166300d4fe4ba75753900813a Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Sat, 5 Oct 2019 15:03:56 +0530 Subject: [PATCH] cmd/godoc: update findGOROOT After godoc stopped being part of the Go distribution, the location of the godoc binary and the go binary can be different. Hence the old GOROOT finding logic does not work anymore. We shell out to "go env GOROOT" to return the new GOROOT. Fixes golang/go#23445 Change-Id: I16e4c0798e3f5cda13d9f6546cf82808a10c4a0b Reviewed-on: https://go-review.googlesource.com/c/tools/+/199279 Reviewed-by: Andrew Bonventre Run-TryBot: Andrew Bonventre TryBot-Result: Gobot Gobot --- cmd/godoc/goroot.go | 54 +++++---------------------------------------- 1 file changed, 5 insertions(+), 49 deletions(-) diff --git a/cmd/godoc/goroot.go b/cmd/godoc/goroot.go index 998e869362..755069d949 100644 --- a/cmd/godoc/goroot.go +++ b/cmd/godoc/goroot.go @@ -6,14 +6,12 @@ package main import ( "os" + "os/exec" "path/filepath" "runtime" + "strings" ) -// Copies of functions from src/cmd/go/internal/cfg/cfg.go for -// finding the GOROOT. -// Keep them in sync until support is moved to a common place, if ever. - func findGOROOT() string { if env := os.Getenv("GOROOT"); env != "" { return filepath.Clean(env) @@ -24,51 +22,9 @@ func findGOROOT() string { // depend on the executable's location. return def } - 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 def -} - -// isGOROOT reports whether path looks like a GOROOT. -// -// It does this by looking for the path/pkg/tool directory, -// which is necessary for useful operation of the cmd/go tool, -// and is not typically present in a GOPATH. -func isGOROOT(path string) bool { - stat, err := os.Stat(filepath.Join(path, "pkg", "tool")) + out, err := exec.Command("go", "env", "GOROOT").Output() if err != nil { - return false + return def } - return stat.IsDir() -} - -// 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) + return strings.TrimSpace(string(out)) }