Skip to content

x/tools/go/packages: Alias type not being parsed as types.Alias with packages.Load #73699

@shashankram

Description

@shashankram

I am not sure why an aliased type, MyInt below is not being parsed as types.Alias with golang.org/x/tools/go/packages.Load.

In example.com/project/foo/file.go:

package foo

type MyInt = int

var _ = MyInt(1)

In foo_test.go:

import (
        "go/types"

        "golang.org/x/tools/go/packages"
)

func TestLoad(t *testing.T) {
	cfg := packages.Config{
		Mode: packages.NeedName |
			packages.NeedFiles | packages.NeedImports | packages.NeedDeps |
			packages.NeedModule | packages.NeedTypes | packages.NeedSyntax,
		BuildFlags: []string{"-tags", ""},
		Tests:      false,
	}

	pkgs, err := packages.Load(&cfg, "example.com/project/foo")
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}
	if len(pkgs) != 1 {
		t.Errorf("expected 1 package, got %d", len(pkgs))
	}
	obj := pkgs[0].Types.Scope().Lookup("MyInt")
	if obj == nil {
		t.Errorf("expected type MyInt, got nil")
	}
	typ := obj.Type()
	_, ok := typ.(*types.Alias)
	if !ok {
		t.Errorf("expected type MyInt to be an alias, got %T", typ)
	}
}

Am I missing something?

I am using Go 1.24.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    ToolsThis label describes issues relating to any tools in the x/tools repository.WaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions