-
Notifications
You must be signed in to change notification settings - Fork 18k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
x/tools/go/packages: types.Package for all main packages has Path()="main" when Mode=LoadTypes #70742
Comments
It looks like cmd/compile emits path="main" into the export data for main packages, because that's the prefix for linker symbols in that package; so when you request NeedTypes, that's what you get. But that's not how go/types interprets the Package.Path, which is as a disambiguating mechanism that keeps various main packages' symbols from colliding. The relevant types.NewPackage call is in xtools/internal/gcimporter/goreader.doPkg. This seems to be an effective workaround: diff --git a/internal/gcimporter/ureader_yes.go b/internal/gcimporter/ureader_yes.go
index 1db408613..be6dc1082 100644
--- a/internal/gcimporter/ureader_yes.go
+++ b/internal/gcimporter/ureader_yes.go
@@ -266,7 +266,7 @@ func (pr *pkgReader) pkgIdx(idx pkgbits.Index) *types.Package {
func (r *reader) doPkg() *types.Package {
path := r.String()
switch path {
- case "":
+ case "main", "":
path = r.p.PkgPath()
case "builtin":
return nil // universe @timothy-king any thoughts? |
Interesting. I'd like to confirm where the "main" path string is coming from. I suspect Fix looks reasonable. What is the output of the modified command |
The types.Packages have the correct Path (not "main"). |
Looks like the actual source of this is Tech debt wise, the patch should be safe for the unified IR reader in noder as "main" is not importable. All LGTM. |
Do you mean that the analogous patch to cmd/compile would be safe? The patch above is to x/tools' ureader. |
Yes. I believe the analogous change to cmd/compile would be safe. Applying the change to both would help minimize the maintenance burden. |
Thanks. CL on its way. |
Change https://go.dev/cl/634922 mentions this issue: |
The patch below causes the go/packages/gopackages command to additionally log the values of
types.Package.{Name,Path}
fields. These are not the same as thego/packages.Package.{Name,PkgPath}
fields, which are ordinarily printed!Observe that although packages.Package.Path has the correct value,
types.Package.Path
is "main" for gomodindex, and every other main package:It should be an invariant that the types.Package fields (Name and Path) match the packages.Package fields.
The text was updated successfully, but these errors were encountered: