go/packages: don't spam stderr

Some editors combine stdout and stderr when running tools, so
printing to stderr without failing confuses them. Stop printing go
list's warnings.

They're still useful for debugging, so users can set
GOPACKAGESPRINTGOLISTERRORS to see them. This isn't part of the API and
has no compatibility guarantees.

Change-Id: I9cf091cf59d082123149888468fa0d126dc972c7
Reviewed-on: https://go-review.googlesource.com/c/142997
Run-TryBot: Heschi Kreinick <heschi@google.com>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
diff --git a/go/packages/golist.go b/go/packages/golist.go
index ce352c7..37a6b8b 100644
--- a/go/packages/golist.go
+++ b/go/packages/golist.go
@@ -588,7 +588,7 @@
 			// Catastrophic error:
 			// - executable not found
 			// - context cancellation
-			return nil, fmt.Errorf("couldn't exec 'go list': %s %T", err, err)
+			return nil, fmt.Errorf("couldn't exec 'go %v': %s %T", args, err, err)
 		}
 
 		// Old go list?
@@ -601,20 +601,18 @@
 		// (despite the -e flag) and the Export field is blank.
 		// Do not fail in that case.
 		if !usesExportData(cfg) {
-			return nil, fmt.Errorf("go list: %s: %s", exitErr, cmd.Stderr)
+			return nil, fmt.Errorf("go %v: %s: %s", args, exitErr, cmd.Stderr)
 		}
 	}
 
-	// Print standard error output from "go list".
-	// Due to the -e flag, this should be empty.
-	// However, in -export mode it contains build errors.
-	// Should go list save build errors in the Package.Error JSON field?
-	// See https://github.com/golang/go/issues/26319.
-	// If so, then we should continue to print stderr as go list
-	// will be silent unless something unexpected happened.
-	// If not, perhaps we should suppress it to reduce noise.
-	if len(stderr.Bytes()) != 0 {
-		fmt.Fprintf(os.Stderr, "go list stderr <<%s>>\n", stderr)
+	// As of writing, go list -export prints some non-fatal compilation
+	// errors to stderr, even with -e set. We would prefer that it put
+	// them in the Package.Error JSON (see http://golang.org/issue/26319).
+	// In the meantime, there's nowhere good to put them, but they can
+	// be useful for debugging. Print them if $GOPACKAGESPRINTGOLISTERRORS
+	// is set.
+	if len(stderr.Bytes()) != 0 && os.Getenv("GOPACKAGESPRINTGOLISTERRORS") != "" {
+		fmt.Fprintf(os.Stderr, "go %v stderr: <<\n%s\n>>\n", args, stderr)
 	}
 
 	// debugging