go/packages: handle potential nil pointer error

Change-Id: I2b889e1d5ec07d9271292c58b896f6c0c7155cf3
Reviewed-on: https://go-review.googlesource.com/c/tools/+/179220
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
diff --git a/go/packages/golist.go b/go/packages/golist.go
index 5389849..72c0c5d 100644
--- a/go/packages/golist.go
+++ b/go/packages/golist.go
@@ -172,7 +172,19 @@
 	// Check candidate packages for containFiles.
 	if len(containFiles) > 0 {
 		for _, id := range containsCandidates {
-			pkg := response.seenPackages[id]
+			pkg, ok := response.seenPackages[id]
+			if !ok {
+				response.addPackage(&Package{
+					ID: id,
+					Errors: []Error{
+						{
+							Kind: ListError,
+							Msg:  fmt.Sprintf("package %s expected but not seen", id),
+						},
+					},
+				})
+				continue
+			}
 			for _, f := range containFiles {
 				for _, g := range pkg.GoFiles {
 					if sameFile(f, g) {