Skip to content

go/packages: ensure TypesInfo is set when NeedTypesInfo is enabled #536

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions go/packages/golist.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func goListDriver(cfg *Config, patterns ...string) (_ *DriverResponse, err error
}

// Fill in response.Sizes asynchronously if necessary.
if cfg.Mode&NeedTypesSizes != 0 || cfg.Mode&NeedTypes != 0 {
if cfg.Mode&NeedTypesSizes != 0 || cfg.Mode&(NeedTypes|NeedTypesInfo) != 0 {
errCh := make(chan error)
go func() {
compiler, arch, err := getSizesForArgs(ctx, state.cfgInvocation(), cfg.gocmdRunner)
Expand Down Expand Up @@ -751,15 +751,15 @@ func jsonFlag(cfg *Config, goVersion int) string {
}
}
addFields("Name", "ImportPath", "Error") // These fields are always needed
if cfg.Mode&NeedFiles != 0 || cfg.Mode&NeedTypes != 0 {
if cfg.Mode&NeedFiles != 0 || cfg.Mode&(NeedTypes|NeedTypesInfo) != 0 {
addFields("Dir", "GoFiles", "IgnoredGoFiles", "IgnoredOtherFiles", "CFiles",
"CgoFiles", "CXXFiles", "MFiles", "HFiles", "FFiles", "SFiles",
"SwigFiles", "SwigCXXFiles", "SysoFiles")
if cfg.Tests {
addFields("TestGoFiles", "XTestGoFiles")
}
}
if cfg.Mode&NeedTypes != 0 {
if cfg.Mode&(NeedTypes|NeedTypesInfo) != 0 {
// CompiledGoFiles seems to be required for the test case TestCgoNoSyntax,
// even when -compiled isn't passed in.
// TODO(#52435): Should we make the test ask for -compiled, or automatically
Expand Down
6 changes: 3 additions & 3 deletions go/packages/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ func newLoader(cfg *Config) *loader {
ld.requestedMode = ld.Mode
ld.Mode = impliedLoadMode(ld.Mode)

if ld.Mode&NeedTypes != 0 || ld.Mode&NeedSyntax != 0 {
if ld.Mode&(NeedTypes|NeedSyntax|NeedTypesInfo) != 0 {
if ld.Fset == nil {
ld.Fset = token.NewFileSet()
}
Expand Down Expand Up @@ -920,7 +920,7 @@ func (ld *loader) refine(response *DriverResponse) ([]*Package, error) {

// Load type data and syntax if needed, starting at
// the initial packages (roots of the import DAG).
if ld.Mode&NeedTypes != 0 || ld.Mode&NeedSyntax != 0 {
if ld.Mode&(NeedTypes|NeedSyntax|NeedTypesInfo) != 0 {
var wg sync.WaitGroup
for _, lpkg := range initial {
wg.Add(1)
Expand Down Expand Up @@ -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
}

Expand Down
33 changes: 33 additions & 0 deletions go/packages/packages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3176,3 +3176,36 @@ func TestIssue69606b(t *testing.T) {
t.Fatal("Expected to get an error because missing unsafe package but got a nil error")
}
}

// TestNeedTypesInfoOnly tests when NeedTypesInfo was set and NeedSyntax & NeedTypes were not,
// Load should include the TypesInfo of packages properly
func TestLoadTypesInfoWithoutSyntaxOrTypes(t *testing.T) {
testAllOrModulesParallel(t, testLoadTypesInfoWithoutSyntaxOrTypes)
}

func testLoadTypesInfoWithoutSyntaxOrTypes(t *testing.T, exporter packagestest.Exporter) {
exported := packagestest.Export(t, exporter, []packagestest.Module{{
Name: "golang.org/fake",
Files: map[string]interface{}{
"a/a.go": `package a;

func foo() int {
i := 0
s := "abc"
return i + len(s)
}
`,
}}})
defer exported.Cleanup()
exported.Config.Mode = packages.NeedTypesInfo

pkgs, err := packages.Load(exported.Config, "golang.org/fake/a")
if err != nil {
t.Fatal(err)
}

// check if types info is present
if pkgs[0].TypesInfo == nil {
t.Errorf("expected types info to be present but got nil")
}
}