Skip to content

Commit dd4bf11

Browse files
committed
internal/gcimporter: set correct Package.Path for main packages
The compiler emits Path="main" for main packages because that's the linker symbol prefix that it uses; but go/packages and x/tools in general wants types.Package.Path to represent the path as reported by go list. This CL works around the bug by decoding path="main" as the correct path for the package being decoded. + a test in go/packages Fixes golang/go#70742 Change-Id: I90f9234976cbfbe2a39c4da4852bc4a134fffd88 Reviewed-on: https://go-review.googlesource.com/c/tools/+/634922 Reviewed-by: Tim King <[email protected]> Commit-Queue: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Alan Donovan <[email protected]>
1 parent aca81ce commit dd4bf11

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

go/packages/packages_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3339,6 +3339,31 @@ func Foo() int { return a.Foo() }
33393339
t.Logf("Packages: %+v", pkgs)
33403340
}
33413341

3342+
// TestMainPackagePathInModeTypes tests (*types.Package).Path() for
3343+
// main packages in mode NeedTypes, a regression test for #70742, a
3344+
// bug in cmd/compile's export data that caused them to appear as
3345+
// "main". (The PkgPath field was always correct.)
3346+
func TestMainPackagePathInModeTypes(t *testing.T) {
3347+
testenv.NeedsGoPackages(t)
3348+
3349+
cfg := &packages.Config{Mode: packages.NeedName | packages.NeedTypes}
3350+
pkgs, err := packages.Load(cfg, "cmd/go")
3351+
if err != nil {
3352+
t.Fatal(err)
3353+
}
3354+
p := pkgs[0]
3355+
if p.PkgPath != "cmd/go" ||
3356+
p.Name != "main" ||
3357+
p.Types.Path() != "cmd/go" ||
3358+
p.Types.Name() != "main" {
3359+
t.Errorf("PkgPath=%q Name=%q Types.Path=%q Types.Name=%q; want (cmd/go, main) both times)",
3360+
p.PkgPath,
3361+
p.Name,
3362+
p.Types.Name(),
3363+
p.Types.Path())
3364+
}
3365+
}
3366+
33423367
func writeTree(t *testing.T, archive string) string {
33433368
root := t.TempDir()
33443369

internal/gcimporter/ureader_yes.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,12 @@ func (pr *pkgReader) pkgIdx(idx pkgbits.Index) *types.Package {
264264
func (r *reader) doPkg() *types.Package {
265265
path := r.String()
266266
switch path {
267-
case "":
267+
// cmd/compile emits path="main" for main packages because
268+
// that's the linker symbol prefix it used; but we need
269+
// the package's path as it would be reported by go list,
270+
// hence "main" below.
271+
// See test at go/packages.TestMainPackagePathInModeTypes.
272+
case "", "main":
268273
path = r.p.PkgPath()
269274
case "builtin":
270275
return nil // universe

0 commit comments

Comments
 (0)