Background: the x/tools/go/packages.Load function returns a set of packages that are nodes in a DAG, the import graph over packages. To enumerate them, one calls Visit, passing a pair of (pre, post) functions to be called before and after the node is visited in dependencies-first order. The pre feature is almost unused: only one of the two dozen calls to Visit in x/tools uses it; I suspect the ratio is even lower in the broader ecosystem.
Proposal: As a convenience, we propose to add a Postorder function that returns a go1.23 iterator over the sequence of packages in the import graph, in dependencies first order.
package packages // golang.org/x/tools/go/packages
// Postorder returns an iterator over the the packages in
// the import graph whose roots are pkg.
// Packages are enumerated in dependencies-first order.
func Postorder(pkgs []*Package) iter.Seq[*Package]
Example before:
pkgs, err := packages.Load(...)
if err != nil { ... }
packages.Visit(pkgs, nil, func(pkg *Package) {
log.Println(pkg)
})
Example after:
for pkg := range packages.Postorder(pkgs) {
log.Println(pkg)
}
Comment From: aclements
This proposal has been added to the active column of the proposals project and will now be reviewed at the weekly proposal review meetings. — aclements for the proposal review group
Comment From: aclements
I'll note that unlike Visit, the Postorder iterator would also allow stopping early from the post-order visit.
Comment From: aclements
Based on the discussion above, this proposal seems like a likely accept. — aclements for the proposal review group
The proposal details are in https://github.com/golang/go/issues/74875#issue-3290611954
Comment From: aclements
No change in consensus, so accepted. 🎉 This issue now tracks the work of implementing the proposal. — aclements for the proposal review group
The proposal details are in https://github.com/golang/go/issues/74875#issue-3290611954
Comment From: gopherbot
Change https://go.dev/cl/698275 mentions this issue: go/packages: add Postorder
Comment From: gopherbot
Change https://go.dev/cl/698755 mentions this issue: go/packages: add Postorder iterator over import graph