Repository navigation
Conversation
importer.Default() only resolved stdlib imports, and none at all when revive was built with -trimpath, so typed rules silently missed most issues. Resolve imports with importer.ForCompiler and a lookup backed by one lazy, cached `go list -export` batch per module. Results no longer depend on GOROOT, GOPATH or -trimpath. Fixes revive-lint#1277
|
Trying this based on @ccoVeille's suggestion to use
Results with and without Build cache grows a bit more (~2x on fiber) if not already filled. And it does run the Reproductions against upstream fiber since the fork in #1277 (comment) doesn't seem to exist anymore. |
|
I’m happy you worked on this, the approach is interesting. But, I was surprised to see the I asked Copilot for feedback, and it basically confirmed my concerns, but also explain the possible solutions |
|
Further queries to Copilot suggested something like this Alternative Approach: Using Thanks for implementing this fix! I understand the concern about depending on a local The Proposal Replace the direct
Implementation Sketch
type PackagesImporter struct {
mu sync.RWMutex
pkgs map[string]*types.Package
}
func NewPackagesImporter(rootDir string) (*PackagesImporter, error) {
cfg := &packages.Config{
Mode: packages.NeedName | packages.NeedTypes | packages.NeedTypesInfo | packages.NeedDeps,
Dir: rootDir,
}
loaded, err := packages.Load(cfg, "./...")
if err != nil {
return nil, err
}
imp := &PackagesImporter{pkgs: make(map[string]*types.Package)}
packages.Visit(loaded, nil, func(p *packages.Package) {
if p.Types != nil {
imp.pkgs[p.PkgPath] = p.Types
}
})
return imp, nil
}
func (i *PackagesImporter) Import(path string) (*types.Package, error) {
i.mu.RLock()
defer i.mu.RUnlock()
if pkg, ok := i.pkgs[path]; ok {
return pkg, nil
}
return nil, fmt.Errorf("package %q not found", path)
}
Benefits
Trade-offs
Feel free to adjust the tone or details! |
|
@alexandear what do think about the PR, the concerns I raised, the possible solutions that popped out of AI ? |
|
I'm guessing from earlier workflow approval on #1894 that it's preferred over this PR, closing this one. |
importer.Default() only resolved stdlib imports, and none at all when revive was built with -trimpath, so typed rules silently missed most issues.
Resolve imports with importer.ForCompiler and a lookup backed by one lazy, cached
go list -exportbatch per module. Results no longer depend on GOROOT, GOPATH or -trimpath.Closes #1277