From 491e5be3522248653dae4ca4f4c4efe1b3572149 Mon Sep 17 00:00:00 2001
From: Chris Koch <chrisko@google.com>
Date: Wed, 26 Jul 2023 12:57:17 -0700
Subject: [PATCH] Fix panic writing dependency packages

Fix panic in the case where one of the dependencies of the commands has
a build error. In that case, WritePkg p.GoFiles[0] access will fail.

Signed-off-by: Chris Koch <chrisko@google.com>
---
 src/pkg/bb/bbinternal/bb.go | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/src/pkg/bb/bbinternal/bb.go b/src/pkg/bb/bbinternal/bb.go
index 75b36736..a23b6a65 100644
--- a/src/pkg/bb/bbinternal/bb.go
+++ b/src/pkg/bb/bbinternal/bb.go
@@ -407,6 +407,15 @@ func (p *Package) rewriteFile(f *ast.File) bool {
 
 // WritePkg writes p's files into destDir.
 func WritePkg(p *packages.Package, destDir string) error {
+	// TODO(hugelgupf):
+	// - join errors
+	// - seems a bit late to check for these errors, but works for now --
+	//   should check when these packages are queried? first used?
+	// - test
+	if len(p.Errors) > 0 {
+		return p.Errors[0]
+	}
+
 	if err := os.MkdirAll(destDir, 0755); err != nil {
 		return err
 	}