godoc: set the GOROOT path properly in cmdline mode

- Setting the GOROOT build path to the value passed from the command line.

- Clarified the return values to named parameters for extra clarity.

- And while here, added some missed out error handling. Just logging the error
to preserve original behavior.

Fixes golang/go#13296

Change-Id: I91427eee790928a3cfb51ae207747e9a17bd5496
Reviewed-on: https://go-review.googlesource.com/110275
Run-TryBot: Andrew Bonventre <andybons@golang.org>
Reviewed-by: Andrew Bonventre <andybons@golang.org>
This commit is contained in:
Agniva De Sarker 2018-04-11 13:29:15 +05:30 committed by Andrew Bonventre
parent 5c8013c561
commit a25dedfa53
2 changed files with 11 additions and 3 deletions

View File

@ -356,6 +356,7 @@ func main() {
return return
} }
build.Default.GOROOT = *goroot
if err := godoc.CommandLine(os.Stdout, fs, pres, flag.Args()); err != nil { if err := godoc.CommandLine(os.Stdout, fs, pres, flag.Args()); err != nil {
log.Print(err) log.Print(err)
} }

View File

@ -134,18 +134,25 @@ func CommandLine(w io.Writer, fs vfs.NameSpace, pres *Presentation, args []strin
// for this. That is, if we get passed a directory like the above, we map that // for this. That is, if we get passed a directory like the above, we map that
// directory so that getPageInfo sees it as /target. // directory so that getPageInfo sees it as /target.
// Returns the absolute and relative paths. // Returns the absolute and relative paths.
func paths(fs vfs.NameSpace, pres *Presentation, path string) (string, string) { func paths(fs vfs.NameSpace, pres *Presentation, path string) (abspath, relpath string) {
if filepath.IsAbs(path) { if filepath.IsAbs(path) {
fs.Bind(target, vfs.OS(path), "/", vfs.BindReplace) fs.Bind(target, vfs.OS(path), "/", vfs.BindReplace)
return target, target return target, target
} }
if build.IsLocalImport(path) { if build.IsLocalImport(path) {
cwd, _ := os.Getwd() // ignore errors cwd, err := os.Getwd()
if err != nil {
log.Printf("error while getting working directory: %v", err)
}
path = filepath.Join(cwd, path) path = filepath.Join(cwd, path)
fs.Bind(target, vfs.OS(path), "/", vfs.BindReplace) fs.Bind(target, vfs.OS(path), "/", vfs.BindReplace)
return target, target return target, target
} }
if bp, _ := build.Import(path, "", build.FindOnly); bp.Dir != "" && bp.ImportPath != "" { bp, err := build.Import(path, "", build.FindOnly)
if err != nil {
log.Printf("error while importing build package: %v", err)
}
if bp.Dir != "" && bp.ImportPath != "" {
fs.Bind(target, vfs.OS(bp.Dir), "/", vfs.BindReplace) fs.Bind(target, vfs.OS(bp.Dir), "/", vfs.BindReplace)
return target, bp.ImportPath return target, bp.ImportPath
} }