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.

Change-Id: If6d78f01eb59d406e44ab6746f2da9e797bbf8e2
GitHub-Last-Rev: e81ddd0903b1322f2d642349c340d60310cf00f0
GitHub-Pull-Request: golang/tools#189
Reviewed-on: https://go-review.googlesource.com/c/tools/+/208597
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
diff --git a/go/packages/packages.go b/go/packages/packages.go
index 050cca4..12ab1e5 100644
--- a/go/packages/packages.go
+++ b/go/packages/packages.go
@@ -713,7 +713,7 @@
 	// which would then require that such created packages be explicitly
 	// inserted back into the Import graph as a final step after export data loading.
 	// The Diamond test exercises this case.
-	if !lpkg.needtypes {
+	if !lpkg.needtypes && !lpkg.needsrc {
 		return
 	}
 	if !lpkg.needsrc {
diff --git a/go/packages/packages_test.go b/go/packages/packages_test.go
index 5d286e9..94bff03 100644
--- a/go/packages/packages_test.go
+++ b/go/packages/packages_test.go
@@ -2470,13 +2470,15 @@
 	if len(pkgs) != 1 {
 		t.Fatalf("Expected 1 package, got %v", pkgs)
 	}
-	pkg := pkgs[0]
-	if len(pkg.Errors) > 0 {
-		t.Fatalf("Expected no errors in package, got %v", pkg.Errors)
-	}
-	if len(pkg.Syntax) == 0 {
-		t.Fatalf("Expected syntax on package, got none.")
-	}
+	packages.Visit(pkgs, func(pkg *packages.Package) bool {
+		if len(pkg.Errors) > 0 {
+			t.Errorf("Expected no errors in package %q, got %v", pkg.ID, pkg.Errors)
+		}
+		if len(pkg.Syntax) == 0 && pkg.ID != "unsafe" {
+			t.Errorf("Expected syntax on package %q, got none.", pkg.ID)
+		}
+		return true
+	}, nil)
 }
 
 func TestLoadModeStrings(t *testing.T) {