Skip to content

Commit 79dbfd6

Browse files
committed
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 <[email protected]> Run-TryBot: Andrew Bonventre <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 944452d commit 79dbfd6

File tree

1 file changed

+5
-49
lines changed

1 file changed

+5
-49
lines changed

cmd/godoc/goroot.go

+5-49
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ package main
66

77
import (
88
"os"
9+
"os/exec"
910
"path/filepath"
1011
"runtime"
12+
"strings"
1113
)
1214

13-
// Copies of functions from src/cmd/go/internal/cfg/cfg.go for
14-
// finding the GOROOT.
15-
// Keep them in sync until support is moved to a common place, if ever.
16-
1715
func findGOROOT() string {
1816
if env := os.Getenv("GOROOT"); env != "" {
1917
return filepath.Clean(env)
@@ -24,51 +22,9 @@ func findGOROOT() string {
2422
// depend on the executable's location.
2523
return def
2624
}
27-
exe, err := os.Executable()
28-
if err == nil {
29-
exe, err = filepath.Abs(exe)
30-
if err == nil {
31-
if dir := filepath.Join(exe, "../.."); isGOROOT(dir) {
32-
// If def (runtime.GOROOT()) and dir are the same
33-
// directory, prefer the spelling used in def.
34-
if isSameDir(def, dir) {
35-
return def
36-
}
37-
return dir
38-
}
39-
exe, err = filepath.EvalSymlinks(exe)
40-
if err == nil {
41-
if dir := filepath.Join(exe, "../.."); isGOROOT(dir) {
42-
if isSameDir(def, dir) {
43-
return def
44-
}
45-
return dir
46-
}
47-
}
48-
}
49-
}
50-
return def
51-
}
52-
53-
// isGOROOT reports whether path looks like a GOROOT.
54-
//
55-
// It does this by looking for the path/pkg/tool directory,
56-
// which is necessary for useful operation of the cmd/go tool,
57-
// and is not typically present in a GOPATH.
58-
func isGOROOT(path string) bool {
59-
stat, err := os.Stat(filepath.Join(path, "pkg", "tool"))
25+
out, err := exec.Command("go", "env", "GOROOT").Output()
6026
if err != nil {
61-
return false
62-
}
63-
return stat.IsDir()
64-
}
65-
66-
// isSameDir reports whether dir1 and dir2 are the same directory.
67-
func isSameDir(dir1, dir2 string) bool {
68-
if dir1 == dir2 {
69-
return true
27+
return def
7028
}
71-
info1, err1 := os.Stat(dir1)
72-
info2, err2 := os.Stat(dir2)
73-
return err1 == nil && err2 == nil && os.SameFile(info1, info2)
29+
return strings.TrimSpace(string(out))
7430
}

0 commit comments

Comments
 (0)