go/packages: work around another case where go list doesn't respect -e

When there's an error running the C compiler for cgo, go list will produce
output, but will also complain in stderr, and return a nonzero exit status.
But it sholudn't return a non-zero exit status in that case when run with
-e. Check for and suppress this case.

Fixes golang/go#33462

Change-Id: Idae0970000a0cb134386f5e66d2b63b348ec8576
Reviewed-on: https://go-review.googlesource.com/c/tools/+/190339
Run-TryBot: Michael Matloob <matloob@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
diff --git a/go/packages/golist.go b/go/packages/golist.go
index 8f678a7..511bc28 100644
--- a/go/packages/golist.go
+++ b/go/packages/golist.go
@@ -884,6 +884,12 @@
 			return nil, fmt.Errorf("%s", stderr.String())
 		}
 
+		// Is there an error running the C compiler in cgo? This will be reported in the "Error" field
+		// and should be suppressed by go list -e.
+		if len(stderr.String()) > 0 && strings.HasPrefix(stderr.String(), "# runtime/cgo\n") && strings.Count(stderr.String(), "\n") == 2 {
+			return stdout, nil
+		}
+
 		// This error only appears in stderr. See golang.org/cl/166398 for a fix in go list to show
 		// the error in the Err section of stdout in case -e option is provided.
 		// This fix is provided for backwards compatibility.
diff --git a/go/packages/packages_test.go b/go/packages/packages_test.go
index 42cd4f3..5f42e6a 100644
--- a/go/packages/packages_test.go
+++ b/go/packages/packages_test.go
@@ -2107,9 +2107,6 @@
 
 func TestNoCcompiler(t *testing.T) { packagestest.TestAll(t, testNoCcompiler) }
 func testNoCcompiler(t *testing.T, exporter packagestest.Exporter) {
-	// Enable this test after golang/go#33462 is resolved.
-	t.Skip()
-
 	exported := packagestest.Export(t, exporter, []packagestest.Module{{
 		Name: "golang.org/fake",
 		Files: map[string]interface{}{