Skip to content

Commit bfe3046

Browse files
timothy-kingGo LUCI
authored and
Go LUCI
committed
go/packages: add a unit test for golang/go#70394
Adds a unit test that reproduces golang/go#70394. That issue is for 1.23 only. The test currently requires >=1.24 so it can pass until there is a backport. Updates golang/go#70394 Change-Id: I35b716d051dd3e737b4db15c3ac871d3166abfca Reviewed-on: https://go-review.googlesource.com/c/tools/+/632116 Reviewed-by: David Chase <drchase@google.com> Commit-Queue: Tim King <taking@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent df87831 commit bfe3046

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

go/packages/packages_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -3177,6 +3177,51 @@ func TestIssue69606b(t *testing.T) {
31773177
}
31783178
}
31793179

3180+
// TestIssue70394 tests materializing an alias type defined in a package (m/a)
3181+
// in another package (m/b) where the types for m/b are coming from the compiler,
3182+
// e.g. `go list -compiled=true ... m/b`.
3183+
func TestIssue70394(t *testing.T) {
3184+
// TODO(taking): backport https://go.dev/cl/604099 so that this works on 23.
3185+
testenv.NeedsGo1Point(t, 24)
3186+
testenv.NeedsTool(t, "go") // requires go list.
3187+
testenv.NeedsGoBuild(t) // requires the compiler for export data.
3188+
3189+
t.Setenv("GODEBUG", "gotypesalias=1")
3190+
3191+
dir := t.TempDir()
3192+
overlay := map[string][]byte{
3193+
filepath.Join(dir, "go.mod"): []byte("module m"), // go version of the module does not matter.
3194+
filepath.Join(dir, "a/a.go"): []byte(`package a; type A = int32`),
3195+
filepath.Join(dir, "b/b.go"): []byte(`package b; import "m/a"; var V a.A`),
3196+
}
3197+
cfg := &packages.Config{
3198+
Dir: dir,
3199+
Mode: packages.NeedTypes, // just NeedsTypes allows for loading export data.
3200+
Overlay: overlay,
3201+
Env: append(os.Environ(), "GOFLAGS=-mod=vendor", "GOWORK=off"),
3202+
}
3203+
pkgs, err := packages.Load(cfg, "m/b")
3204+
if err != nil {
3205+
t.Fatal(err)
3206+
}
3207+
if errs := packages.PrintErrors(pkgs); errs > 0 {
3208+
t.Fatalf("Got %d errors while loading packages.", errs)
3209+
}
3210+
if len(pkgs) != 1 {
3211+
t.Fatalf("Loaded %d packages. expected 1", len(pkgs))
3212+
}
3213+
3214+
pkg := pkgs[0]
3215+
scope := pkg.Types.Scope()
3216+
obj := scope.Lookup("V")
3217+
if obj == nil {
3218+
t.Fatalf("Failed to find object %q in package %q", "V", pkg)
3219+
}
3220+
if _, ok := obj.Type().(*types.Alias); !ok {
3221+
t.Errorf("Object %q has type %q. expected an alias", obj, obj.Type())
3222+
}
3223+
}
3224+
31803225
// TestNeedTypesInfoOnly tests when NeedTypesInfo was set and NeedSyntax & NeedTypes were not,
31813226
// Load should include the TypesInfo of packages properly
31823227
func TestLoadTypesInfoWithoutSyntaxOrTypes(t *testing.T) {

0 commit comments

Comments
 (0)