Open xrxtzz opened 4 days ago
Related Issues and Documentation
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
types.Info
is, fundamentally, the set of type-checker annotations on the syntax tree. You can neither compute it nor meaningfully use it without syntax trees. Indeed, the keys of the types.Info.Scopes mapping is a superset of the list of ast.Files in Packages.Syntax, so if you have Packages.TypesInfo you could derive Packages.Syntax from it. How did you plan to use TypesInfo without Syntax?
While the Need
interface of go/packages is complicated and certainly has bugs, in this case I think it is working as intended. Perhaps the documentation could be improved.
Indeed, the keys of the types.Info.Scopes mapping is a superset of the list of ast.Files in Packages.Syntax, so if you have Packages.TypesInfo you could derive Packages.Syntax from it. How did you plan to use TypesInfo without Syntax?
Agree on that, but the behaviors of different combinations of LoadModes still make no sense, for example:
NeedSyntax & NeedTypesInfo
won't actually give TypesInfo, though we do have Syntax here and can use TypesInfo well.NeedTypes & NeedTypesInfo
can output TypesInfo, and in this case no Syntax was obtained.Reason for above two cases is probably because *loader.loadPackage
would simply return on tools/go/packages/packages.go:1162 unless NeedTypes
was set. When I look into it deeper I found that indeed the LoadMode controlling here is pretty complicated and buggy (as indicated in other related issues)...
Possibly the right fix here is just:
--- a/go/packages/packages.go
+++ b/go/packages/packages.go
@@ -1158,7 +1158,7 @@ func (ld *loader) loadPackage(lpkg *loaderPackage) {
}
lpkg.Syntax = files
- if ld.Config.Mode&NeedTypes == 0 {
+ if ld.Config.Mode&(NeedTypes|NeedTypesInfo) == 0 {
return
}
It's a little weird to ask for only TypesInfo but not Syntax or Types, but I don't see any reason we can't honor the request.
@matloob
Go version
go1.22.7 darwin/arm64
Output of
go env
in your module/workspace:What did you do?
The
NeedTypesInfo
option indicates thatpackages.Load
would addTypesInfo
field to result packages. But it turns out that whenNeedTypesInfo
was used separately fromNeedSyntax
orNeedTypes
, theTypesInfo
fields would simply benil
.What did you see happen?
When
NeedTypesInfo
was used separately fromNeedSyntax
orNeedTypes
, in LoadMode of packages.Load, theTypesInfo
fields would simply benil
.What did you expect to see?
The
TypesInfo
were produced properly.