diff options
| author | Russ Cox <rsc@golang.org> | 2018-07-30 14:53:44 -0400 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2018-08-01 00:35:28 +0000 |
| commit | b7d3f4c0b2f421c6b7bf6ce3533f1b34b470b39d (patch) | |
| tree | b77da8cd97280ea7a4907a10d350a44a495d54a3 /src/cmd/doc/pkg.go | |
| parent | b8f42d74e87aeec189f53e9fdb2a7e6026c099b1 (diff) | |
| download | go-b7d3f4c0b2f421c6b7bf6ce3533f1b34b470b39d.tar.xz | |
cmd/doc: adapt directory search for modules
Previously, cmd/doc treated GOROOT/src and GOPATH/src
as the roots of the directory trees holding packages, assuming
that the import path would be the path elements after the src directory.
With modules, each module serves as its own root of a file tree,
and the import path prefix starts with the module path before
adding the path elements after the module root.
There are ways we could make this more efficient,
but for now this is a fairly small adjustment to get 'go doc'
working OK for modules for Go 1.11.
Fixes #26635.
Change-Id: Ifdee4194601312846c7b1fc67f2fe7a4a44269cc
Reviewed-on: https://go-review.googlesource.com/126799
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Diffstat (limited to 'src/cmd/doc/pkg.go')
| -rw-r--r-- | src/cmd/doc/pkg.go | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/src/cmd/doc/pkg.go b/src/cmd/doc/pkg.go index 8ff9ff57ac..14e41b9106 100644 --- a/src/cmd/doc/pkg.go +++ b/src/cmd/doc/pkg.go @@ -425,12 +425,36 @@ func (pkg *Package) packageClause(checkUserPath bool) { return } } + importPath := pkg.build.ImportComment if importPath == "" { importPath = pkg.build.ImportPath } + + // If we're using modules, the import path derived from module code locations wins. + // If we did a file system scan, we knew the import path when we found the directory. + // But if we started with a directory name, we never knew the import path. + // Either way, we don't know it now, and it's cheap to (re)compute it. + if usingModules { + for _, root := range codeRoots() { + if pkg.build.Dir == root.dir { + importPath = root.importPath + break + } + if strings.HasPrefix(pkg.build.Dir, root.dir+string(filepath.Separator)) { + suffix := filepath.ToSlash(pkg.build.Dir[len(root.dir)+1:]) + if root.importPath == "" { + importPath = suffix + } else { + importPath = root.importPath + "/" + suffix + } + break + } + } + } + pkg.Printf("package %s // import %q\n\n", pkg.name, importPath) - if importPath != pkg.build.ImportPath { + if !usingModules && importPath != pkg.build.ImportPath { pkg.Printf("WARNING: package source is installed in %q\n", pkg.build.ImportPath) } } |
