Skip to content

Commit 868db45

Browse files
authored
fix(godocfx): filter out other modules, sort pkgs (#2894)
The sort keeps the TOC in a stable order.
1 parent 07df7d8 commit 868db45

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

internal/godocfx/go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
6767
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
6868
github.com/google/martian/v3 v3.0.0 h1:pMen7vLs8nvgEYhywH3KDWJIJTeEr2ULsVWHWYHQyBs=
6969
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
70+
github.com/google/pprof v0.0.0-20200905233945-acf8798be1f7 h1:k+KkMRk8mGOu1xG38StS7dQ+Z6oW1i9n3dgrAVU9Q/E=
7071
github.com/google/pprof v0.0.0-20200905233945-acf8798be1f7/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
7172
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
7273
github.com/googleapis/gax-go/v2 v2.0.5 h1:sjZBwGj9Jlw33ImPtvFviGYvseOtDM7hkSKB7+Tv3SM=

internal/godocfx/parse.go

+27-4
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func parse(glob string) (map[string]*page, tableOfContents, *packages.Module, er
122122

123123
// First, collect all of the files grouped by package, including test
124124
// packages.
125-
pkgFiles := map[string][]string{}
125+
allPkgFiles := map[string][]string{}
126126
for _, pkg := range pkgs {
127127
id := pkg.ID
128128
// See https://pkg.go.dev/golang.org/x/tools/go/packages#Config.
@@ -144,17 +144,35 @@ func parse(glob string) (map[string]*page, tableOfContents, *packages.Module, er
144144
for _, f := range pkg.Syntax {
145145
name := pkg.Fset.File(f.Pos()).Name()
146146
if strings.HasSuffix(name, ".go") {
147-
pkgFiles[id] = append(pkgFiles[id], name)
147+
allPkgFiles[id] = append(allPkgFiles[id], name)
148+
}
149+
}
150+
}
151+
152+
// Test files don't have Module set. Filter out packages in skipped modules.
153+
pkgFiles := map[string][]string{}
154+
pkgNames := []string{}
155+
for pkgPath, files := range allPkgFiles {
156+
skip := false
157+
for skipped := range skippedModules {
158+
if strings.HasPrefix(pkgPath, skipped) {
159+
skip = true
160+
break
148161
}
149162
}
163+
if !skip {
164+
pkgFiles[pkgPath] = files
165+
pkgNames = append(pkgNames, pkgPath)
166+
}
150167
}
168+
sort.Strings(pkgNames)
151169

152170
// Once the files are grouped by package, process each package
153171
// independently.
154-
for pkgPath, files := range pkgFiles {
172+
for _, pkgPath := range pkgNames {
155173
parsedFiles := []*ast.File{}
156174
fset := token.NewFileSet()
157-
for _, f := range files {
175+
for _, f := range pkgFiles[pkgPath] {
158176
pf, err := parser.ParseFile(fset, f, nil, parser.ParseComments)
159177
if err != nil {
160178
return nil, nil, nil, fmt.Errorf("ParseFile: %v", err)
@@ -168,6 +186,11 @@ func parse(glob string) (map[string]*page, tableOfContents, *packages.Module, er
168186
return nil, nil, nil, fmt.Errorf("doc.NewFromFiles: %v", err)
169187
}
170188

189+
// Extra filter in case the file filtering didn't catch everything.
190+
if !strings.HasPrefix(docPkg.ImportPath, module.Path) {
191+
continue
192+
}
193+
171194
toc = append(toc, &tocItem{
172195
UID: docPkg.ImportPath,
173196
Name: docPkg.ImportPath,

0 commit comments

Comments
 (0)