Skip to content

Commit 56eba13

Browse files
committed
go/packages: Fix loading syntax for imports when types not requested.
http://golang.org/cl/205160 fixed an issue when package syntax wasn't loaded unless NeedTypes was specified, but syntax of imported packages wasn't loaded even with NeedDeps specified. This change corrects this error. Fixes issue #35331.
1 parent 2a6ccf2 commit 56eba13

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

go/packages/packages.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ func (ld *loader) loadPackage(lpkg *loaderPackage) {
713713
// which would then require that such created packages be explicitly
714714
// inserted back into the Import graph as a final step after export data loading.
715715
// The Diamond test exercises this case.
716-
if !lpkg.needtypes {
716+
if !lpkg.needtypes && !lpkg.needsrc {
717717
return
718718
}
719719
if !lpkg.needsrc {

go/packages/packages_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2517,13 +2517,15 @@ func testIssue35331(t *testing.T, exporter packagestest.Exporter) {
25172517
if len(pkgs) != 1 {
25182518
t.Fatalf("Expected 1 package, got %v", pkgs)
25192519
}
2520-
pkg := pkgs[0]
2521-
if len(pkg.Errors) > 0 {
2522-
t.Fatalf("Expected no errors in package, got %v", pkg.Errors)
2523-
}
2524-
if len(pkg.Syntax) == 0 {
2525-
t.Fatalf("Expected syntax on package, got none.")
2526-
}
2520+
packages.Visit(pkgs, func(pkg *packages.Package) bool {
2521+
if len(pkg.Errors) > 0 {
2522+
t.Errorf("Expected no errors in package %q, got %v", pkg.ID, pkg.Errors)
2523+
}
2524+
if len(pkg.Syntax) == 0 && pkg.ID != "unsafe" {
2525+
t.Errorf("Expected syntax on package %q, got none.", pkg.ID)
2526+
}
2527+
return true
2528+
}, nil)
25272529
}
25282530

25292531
func TestLoadModeStrings(t *testing.T) {

0 commit comments

Comments
 (0)