mirror of
https://github.com/golang/go
synced 2024-11-18 09:04:49 -07:00
cmd/doc: try better when looking for package dir
When go doc is invoked with a single package name argument (e.g. go doc pkgname) it needs to find the directory of the requested package sources in GOPATH. GOPATH might contain directories with the same name as the requested package that do no contain any *.go files. This change makes "go doc" ignore such directories when looking for possible package directories. This fixes #10882 Change-Id: Ib3d4ea69a25801c34cbe7b044de9870ba12f9aa8 Reviewed-on: https://go-review.googlesource.com/10190 Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
parent
8801bdf2b3
commit
e15f89c526
@ -276,7 +276,7 @@ func pathFor(root, pkg string) (result string) {
|
|||||||
return filepath.SkipDir
|
return filepath.SkipDir
|
||||||
}
|
}
|
||||||
// Is the tail of the path correct?
|
// Is the tail of the path correct?
|
||||||
if strings.HasSuffix(pathName, pkgString) {
|
if strings.HasSuffix(pathName, pkgString) && hasGoFiles(pathName) {
|
||||||
result = pathName
|
result = pathName
|
||||||
panic(nil)
|
panic(nil)
|
||||||
}
|
}
|
||||||
@ -287,6 +287,31 @@ func pathFor(root, pkg string) (result string) {
|
|||||||
return "" // Call to panic above sets the real value.
|
return "" // Call to panic above sets the real value.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// hasGoFiles tests whether the directory contains at least one file with ".go"
|
||||||
|
// extension
|
||||||
|
func hasGoFiles(path string) bool {
|
||||||
|
dir, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
// ignore unreadable directories
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
defer dir.Close()
|
||||||
|
|
||||||
|
names, err := dir.Readdirnames(0)
|
||||||
|
if err != nil {
|
||||||
|
// ignore unreadable directories
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, name := range names {
|
||||||
|
if strings.HasSuffix(name, ".go") {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// pwd returns the current directory.
|
// pwd returns the current directory.
|
||||||
func pwd() string {
|
func pwd() string {
|
||||||
wd, err := os.Getwd()
|
wd, err := os.Getwd()
|
||||||
|
Loading…
Reference in New Issue
Block a user