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>
This commit is contained in:
Russ Cox 2018-07-30 14:53:44 -04:00
parent b8f42d74e8
commit b7d3f4c0b2
5 changed files with 180 additions and 31 deletions

View file

@ -18,6 +18,7 @@ import (
func TestMain(m *testing.M) {
// Clear GOPATH so we don't access the user's own packages in the test.
buildCtx.GOPATH = ""
testGOPATH = true // force GOPATH mode; module test is in cmd/go/testdata/script/mod_doc.txt
// Add $GOROOT/src/cmd/doc/testdata explicitly so we can access its contents in the test.
// Normally testdata directories are ignored, but sending it to dirs.scan directly is
@ -26,7 +27,7 @@ func TestMain(m *testing.M) {
if err != nil {
panic(err)
}
dirsInit(testdataDir, filepath.Join(testdataDir, "nested"), filepath.Join(testdataDir, "nested", "nested"))
dirsInit(Dir{"testdata", testdataDir}, Dir{"testdata/nested", filepath.Join(testdataDir, "nested")}, Dir{"testdata/nested/nested", filepath.Join(testdataDir, "nested", "nested")})
os.Exit(m.Run())
}
@ -537,7 +538,7 @@ func TestDoc(t *testing.T) {
var flagSet flag.FlagSet
err := do(&b, &flagSet, test.args)
if err != nil {
t.Fatalf("%s: %s\n", test.name, err)
t.Fatalf("%s %v: %s\n", test.name, test.args, err)
}
output := b.Bytes()
failed := false