]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/doc: use 'go env' to identify GOROOT if it isn't otherwise known
authorBryan C. Mills <bcmills@google.com>
Thu, 10 Mar 2022 19:58:02 +0000 (14:58 -0500)
committerBryan Mills <bcmills@google.com>
Thu, 17 Mar 2022 20:09:18 +0000 (20:09 +0000)
Updates #51483.

Change-Id: I13d8e58b30639d8a5ed3c9e8b72c8bbaa6a6f1cc
Reviewed-on: https://go-review.googlesource.com/c/go/+/391813
Trust: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/doc/dirs.go
src/cmd/doc/pkg.go

index f27af1d27befbe10f4fcf61fc21eed960f6e9867..cb4d45ac6ced8b9a76b418cd2ca78a1285fdfdb5 100644 (file)
@@ -41,6 +41,17 @@ var dirs Dirs
 // dirsInit starts the scanning of package directories in GOROOT and GOPATH. Any
 // extra paths passed to it are included in the channel.
 func dirsInit(extra ...Dir) {
+       if buildCtx.GOROOT == "" {
+               stdout, err := exec.Command("go", "env", "GOROOT").Output()
+               if err != nil {
+                       if ee, ok := err.(*exec.ExitError); ok && len(ee.Stderr) > 0 {
+                               log.Fatalf("failed to determine GOROOT: $GOROOT is not set and 'go env GOROOT' failed:\n%s", ee.Stderr)
+                       }
+                       log.Fatalf("failed to determine GOROOT: $GOROOT is not set and could not run 'go env GOROOT':\n\t%s", err)
+               }
+               buildCtx.GOROOT = string(bytes.TrimSpace(stdout))
+       }
+
        dirs.hist = make([]Dir, 0, 1000)
        dirs.hist = append(dirs.hist, extra...)
        dirs.scan = make(chan Dir)
@@ -174,7 +185,7 @@ func findCodeRoots() []Dir {
                gomod := string(bytes.TrimSpace(stdout))
 
                usingModules = len(gomod) > 0
-               if usingModules {
+               if usingModules && buildCtx.GOROOT != "" {
                        list = append(list,
                                Dir{dir: filepath.Join(buildCtx.GOROOT, "src"), inModule: true},
                                Dir{importPath: "cmd", dir: filepath.Join(buildCtx.GOROOT, "src", "cmd"), inModule: true})
@@ -190,7 +201,9 @@ func findCodeRoots() []Dir {
        }
 
        if !usingModules {
-               list = append(list, Dir{dir: filepath.Join(buildCtx.GOROOT, "src")})
+               if buildCtx.GOROOT != "" {
+                       list = append(list, Dir{dir: filepath.Join(buildCtx.GOROOT, "src")})
+               }
                for _, root := range splitGopath() {
                        list = append(list, Dir{dir: filepath.Join(root, "src")})
                }
index 02666007300f8f24302812d15635fe80b9349f47..49b68873b6f9ceb26e3206a1ff9620b898c36ba4 100644 (file)
@@ -89,9 +89,11 @@ func (pkg *Package) prettyPath() string {
        // Also convert everything to slash-separated paths for uniform handling.
        path = filepath.Clean(filepath.ToSlash(pkg.build.Dir))
        // Can we find a decent prefix?
-       goroot := filepath.Join(buildCtx.GOROOT, "src")
-       if p, ok := trim(path, filepath.ToSlash(goroot)); ok {
-               return p
+       if buildCtx.GOROOT != "" {
+               goroot := filepath.Join(buildCtx.GOROOT, "src")
+               if p, ok := trim(path, filepath.ToSlash(goroot)); ok {
+                       return p
+               }
        }
        for _, gopath := range splitGopath() {
                if p, ok := trim(path, filepath.ToSlash(gopath)); ok {