From ba93f9405e5422070378f3fd678025dfceec0863 Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Thu, 23 Aug 2018 10:54:26 +0530 Subject: [PATCH] godoc: update to use new goroot finding logic The logic to determine whether a filesystem root was in GOROOT or GOPATH still relied on runtime.GOROOT(), whereas cmd/godoc was updated to copy the goroot finding logic from standard library. Hence, godoc is unable to determine if a filesystem is in GOROOT or not when the binary is outside runtime.GOROOT(). So we expose a new variable and set that from cmd/godoc to avoid copying the logic again for the 3rd time. Fixes golang/go#27162 Change-Id: I160dcdbdd262e671f09f7bf01c329be5eac280ad Reviewed-on: https://go-review.googlesource.com/130796 Reviewed-by: Brad Fitzpatrick Run-TryBot: Brad Fitzpatrick --- cmd/godoc/main.go | 3 +++ godoc/vfs/os.go | 10 +++++++++- godoc/vfs/zipfs/zipfs.go | 3 +-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/cmd/godoc/main.go b/cmd/godoc/main.go index 66beabdbf5..89e9bba8f6 100644 --- a/cmd/godoc/main.go +++ b/cmd/godoc/main.go @@ -183,6 +183,9 @@ func main() { usage() } + // Setting the resolved goroot. + vfs.GOROOT = *goroot + var fsGate chan bool fsGate = make(chan bool, 20) diff --git a/godoc/vfs/os.go b/godoc/vfs/os.go index 8cc9ed1990..78719b46ad 100644 --- a/godoc/vfs/os.go +++ b/godoc/vfs/os.go @@ -14,6 +14,14 @@ import ( "runtime" ) +// GOROOT returns the GOROOT path under which the godoc binary is running. +// It is needed to check whether a filesystem root is under GOROOT or not. +// This is set from cmd/godoc/main.go + +// We expose a new variable because otherwise we need to copy the findGOROOT logic again +// from cmd/godoc which is already copied twice from the standard library. +var GOROOT = runtime.GOROOT() + // OS returns an implementation of FileSystem reading from the // tree rooted at root. Recording a root is convenient everywhere // but necessary on Windows, because the slash-separated path @@ -22,7 +30,7 @@ import ( func OS(root string) FileSystem { var t RootType switch { - case root == runtime.GOROOT(): + case root == GOROOT: t = RootTypeGoRoot case isGoPath(root): t = RootTypeGoPath diff --git a/godoc/vfs/zipfs/zipfs.go b/godoc/vfs/zipfs/zipfs.go index 6d27f63624..b216f55827 100644 --- a/godoc/vfs/zipfs/zipfs.go +++ b/godoc/vfs/zipfs/zipfs.go @@ -25,7 +25,6 @@ import ( "os" "path" "path/filepath" - "runtime" "sort" "strings" "time" @@ -87,7 +86,7 @@ func (fs *zipFS) String() string { func (fs *zipFS) RootType(abspath string) vfs.RootType { var t vfs.RootType switch { - case abspath == runtime.GOROOT(): + case abspath == vfs.GOROOT: t = vfs.RootTypeGoRoot case isGoPath(abspath): t = vfs.RootTypeGoPath