go/packages: handle variation an an error message

go/packages has a special case for cmd/go errors that result from
errors running the 'pkg-config' tool. When modules are missing from
the module cache cmd/go can download modules, and log a message for
each downloaded module, so add a special case to the special case
to check for that message.

Also remove a usage of the x/xerrors package now that it's been folded
into the stdlib for a while.

Fixes golang/go#36770

Change-Id: If1cf8c5d83ac84a51b8bafc4930a0869674d216a
Reviewed-on: https://go-review.googlesource.com/c/tools/+/261502
Trust: Michael Matloob <matloob@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
diff --git a/go/packages/golist.go b/go/packages/golist.go
index 3c6e6c0..c83ca09 100644
--- a/go/packages/golist.go
+++ b/go/packages/golist.go
@@ -902,8 +902,13 @@
 			return unicode.IsOneOf([]*unicode.RangeTable{unicode.L, unicode.M, unicode.N, unicode.P, unicode.S}, r) &&
 				!strings.ContainsRune("!\"#$%&'()*,:;<=>?[\\]^`{|}\uFFFD", r)
 		}
+		// golang/go#36770: Handle case where cmd/go prints module download messages before the error.
+		msg := stderr.String()
+		for strings.HasPrefix(msg, "go: downloading") {
+			msg = msg[strings.IndexRune(msg, '\n')+1:]
+		}
 		if len(stderr.String()) > 0 && strings.HasPrefix(stderr.String(), "# ") {
-			msg := stderr.String()[len("# "):]
+			msg := msg[len("# "):]
 			if strings.HasPrefix(strings.TrimLeftFunc(msg, isPkgPathRune), "\n") {
 				return stdout, nil
 			}