diff options
| author | Michael Matloob <matloob@golang.org> | 2025-05-30 14:52:32 -0400 |
|---|---|---|
| committer | Michael Matloob <matloob@golang.org> | 2025-05-30 14:29:13 -0700 |
| commit | a4f2384de7e18f683603694ee1e12b93dbc8adeb (patch) | |
| tree | 149299553cbf15da9492978a5550a03c836ad288 | |
| parent | 274f41854e537a9443cc9ca83cd631cde79fe343 (diff) | |
| download | go-x-pkgsite-a4f2384de7e18f683603694ee1e12b93dbc8adeb.tar.xz | |
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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
kokoro-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Michael Matloob <matloob@google.com>
| -rw-r--r-- | internal/godoc/dochtml/dochtml_test.go | 25 | ||||
| -rw-r--r-- | internal/godoc/render.go | 25 |
2 files changed, 50 insertions, 0 deletions
diff --git a/internal/godoc/dochtml/dochtml_test.go b/internal/godoc/dochtml/dochtml_test.go index 880b87f4..12f85365 100644 --- a/internal/godoc/dochtml/dochtml_test.go +++ b/internal/godoc/dochtml/dochtml_test.go @@ -477,6 +477,14 @@ func mustLoadPackage(path string) (*token.FileSet, *doc.Package) { astFile, _ := parser.ParseFile(fset, srcName, code, parser.ParseComments) files := []*ast.File{astFile} + filesMap := map[string]*ast.File{} + if !strings.HasSuffix(srcName, "_test.go") { + filesMap[srcName] = astFile + } + + //lint:ignore SA1019 We had a preexisting dependency on ast.Object. + ast.NewPackage(fset, filesMap, simpleImporter, nil) + astPackage, err := doc.NewFromFiles(fset, files, path, doc.AllDecls) if err != nil { panic(err) @@ -484,3 +492,20 @@ func mustLoadPackage(path string) (*token.FileSet, *doc.Package) { return fset, astPackage } + +// simpleImporter returns a (dummy) package object named by the last path +// component of the provided package path (as is the convention for packages). +// This is sufficient to resolve package identifiers without doing an actual +// import. It never returns an error. +// +//lint:ignore SA1019 We had a preexisting dependency on ast.Object. +func simpleImporter(imports map[string]*ast.Object, path string) (*ast.Object, error) { + pkg := imports[path] + if pkg == nil { + // note that strings.LastIndex returns -1 if there is no "/" + pkg = ast.NewObj(ast.Pkg, path[strings.LastIndex(path, "/")+1:]) + pkg.Data = ast.NewScope(nil) // required by ast.NewPackage for dot-import + imports[path] = pkg + } + return pkg, nil +} diff --git a/internal/godoc/render.go b/internal/godoc/render.go index 1c780750..4bd8cdea 100644 --- a/internal/godoc/render.go +++ b/internal/godoc/render.go @@ -115,9 +115,17 @@ func (p *Package) DocPackage(innerPath string, modInfo *ModuleInfo) (_ *doc.Pack m |= doc.AllDecls } var allGoFiles []*ast.File + nonTestFiles := map[string]*ast.File{} for _, f := range p.Files { allGoFiles = append(allGoFiles, f.AST) + if !strings.HasSuffix(f.Name, "_test.go") { + nonTestFiles[f.Name] = f.AST + } } + // Call ast.NewPackage for side-effects to populate objects. In Go 1.25+ + // doc.NewFromFiles will not cause the objects to be populated. + //lint:ignore SA1019 We had a preexisting dependency on ast.Object. + ast.NewPackage(p.Fset, nonTestFiles, simpleImporter, nil) d, err := doc.NewFromFiles(p.Fset, allGoFiles, importPath, m) if err != nil { return nil, fmt.Errorf("doc.NewFromFiles: %v", err) @@ -142,6 +150,23 @@ func (p *Package) DocPackage(innerPath string, modInfo *ModuleInfo) (_ *doc.Pack return d, nil } +// simpleImporter returns a (dummy) package object named by the last path +// component of the provided package path (as is the convention for packages). +// This is sufficient to resolve package identifiers without doing an actual +// import. It never returns an error. +// +//lint:ignore SA1019 We had a preexisting dependency on ast.Object. +func simpleImporter(imports map[string]*ast.Object, path string) (*ast.Object, error) { + pkg := imports[path] + if pkg == nil { + // note that strings.LastIndex returns -1 if there is no "/" + pkg = ast.NewObj(ast.Pkg, path[strings.LastIndex(path, "/")+1:]) + pkg.Data = ast.NewScope(nil) // required by ast.NewPackage for dot-import + imports[path] = pkg + } + return pkg, nil +} + // renderOptions returns a RenderOptions for p. func (p *Package) renderOptions(innerPath string, sourceInfo *source.Info, modInfo *ModuleInfo, nameToVersion map[string]string, bc internal.BuildContext) dochtml.RenderOptions { |
