Skip to content

Commit a4f2384

Browse files
committed
internal/godoc: fix incompatibility with Go 1.25 go/doc package
In Go 1.25, the go/doc package will not call the ast.NewPackage function on the asts, which has the side effect of populating the objects. Instead, call it ourselves for the side effect. This will fix the build breakage. Change-Id: I657fb92619b1db724a80bb1c8f0df998c2fce2e0 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/677596 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]> kokoro-CI: kokoro <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent 274f418 commit a4f2384

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

internal/godoc/dochtml/dochtml_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,10 +477,35 @@ func mustLoadPackage(path string) (*token.FileSet, *doc.Package) {
477477
astFile, _ := parser.ParseFile(fset, srcName, code, parser.ParseComments)
478478
files := []*ast.File{astFile}
479479

480+
filesMap := map[string]*ast.File{}
481+
if !strings.HasSuffix(srcName, "_test.go") {
482+
filesMap[srcName] = astFile
483+
}
484+
485+
//lint:ignore SA1019 We had a preexisting dependency on ast.Object.
486+
ast.NewPackage(fset, filesMap, simpleImporter, nil)
487+
480488
astPackage, err := doc.NewFromFiles(fset, files, path, doc.AllDecls)
481489
if err != nil {
482490
panic(err)
483491
}
484492

485493
return fset, astPackage
486494
}
495+
496+
// simpleImporter returns a (dummy) package object named by the last path
497+
// component of the provided package path (as is the convention for packages).
498+
// This is sufficient to resolve package identifiers without doing an actual
499+
// import. It never returns an error.
500+
//
501+
//lint:ignore SA1019 We had a preexisting dependency on ast.Object.
502+
func simpleImporter(imports map[string]*ast.Object, path string) (*ast.Object, error) {
503+
pkg := imports[path]
504+
if pkg == nil {
505+
// note that strings.LastIndex returns -1 if there is no "/"
506+
pkg = ast.NewObj(ast.Pkg, path[strings.LastIndex(path, "/")+1:])
507+
pkg.Data = ast.NewScope(nil) // required by ast.NewPackage for dot-import
508+
imports[path] = pkg
509+
}
510+
return pkg, nil
511+
}

internal/godoc/render.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,17 @@ func (p *Package) DocPackage(innerPath string, modInfo *ModuleInfo) (_ *doc.Pack
115115
m |= doc.AllDecls
116116
}
117117
var allGoFiles []*ast.File
118+
nonTestFiles := map[string]*ast.File{}
118119
for _, f := range p.Files {
119120
allGoFiles = append(allGoFiles, f.AST)
121+
if !strings.HasSuffix(f.Name, "_test.go") {
122+
nonTestFiles[f.Name] = f.AST
123+
}
120124
}
125+
// Call ast.NewPackage for side-effects to populate objects. In Go 1.25+
126+
// doc.NewFromFiles will not cause the objects to be populated.
127+
//lint:ignore SA1019 We had a preexisting dependency on ast.Object.
128+
ast.NewPackage(p.Fset, nonTestFiles, simpleImporter, nil)
121129
d, err := doc.NewFromFiles(p.Fset, allGoFiles, importPath, m)
122130
if err != nil {
123131
return nil, fmt.Errorf("doc.NewFromFiles: %v", err)
@@ -142,6 +150,23 @@ func (p *Package) DocPackage(innerPath string, modInfo *ModuleInfo) (_ *doc.Pack
142150
return d, nil
143151
}
144152

153+
// simpleImporter returns a (dummy) package object named by the last path
154+
// component of the provided package path (as is the convention for packages).
155+
// This is sufficient to resolve package identifiers without doing an actual
156+
// import. It never returns an error.
157+
//
158+
//lint:ignore SA1019 We had a preexisting dependency on ast.Object.
159+
func simpleImporter(imports map[string]*ast.Object, path string) (*ast.Object, error) {
160+
pkg := imports[path]
161+
if pkg == nil {
162+
// note that strings.LastIndex returns -1 if there is no "/"
163+
pkg = ast.NewObj(ast.Pkg, path[strings.LastIndex(path, "/")+1:])
164+
pkg.Data = ast.NewScope(nil) // required by ast.NewPackage for dot-import
165+
imports[path] = pkg
166+
}
167+
return pkg, nil
168+
}
169+
145170
// renderOptions returns a RenderOptions for p.
146171
func (p *Package) renderOptions(innerPath string, sourceInfo *source.Info, modInfo *ModuleInfo,
147172
nameToVersion map[string]string, bc internal.BuildContext) dochtml.RenderOptions {

0 commit comments

Comments
 (0)