Browse Source

capture error message reported by bake and forward to compose

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 10 months ago
parent
commit
f14c15fa57
2 changed files with 15 additions and 8 deletions
  1. 14 6
      pkg/compose/build_bake.go
  2. 1 2
      pkg/e2e/build_test.go

+ 14 - 6
pkg/compose/build_bake.go

@@ -17,12 +17,12 @@
 package compose
 
 import (
+	"bufio"
 	"bytes"
 	"context"
 	"encoding/json"
 	"errors"
 	"fmt"
-	"io"
 	"os"
 	"os/exec"
 	"path/filepath"
@@ -272,18 +272,23 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
 		return nil, err
 	}
 
+	var errMessage string
+	scanner := bufio.NewScanner(pipe)
+	scanner.Split(bufio.ScanLines)
+
 	err = cmd.Start()
 	if err != nil {
 		return nil, err
 	}
 	eg.Go(cmd.Wait)
-	for {
-		decoder := json.NewDecoder(pipe)
+	for scanner.Scan() {
+		line := scanner.Text()
+		decoder := json.NewDecoder(strings.NewReader(line))
 		var status client.SolveStatus
 		err := decoder.Decode(&status)
 		if err != nil {
-			if errors.Is(err, io.EOF) {
-				break
+			if strings.HasPrefix(line, "ERROR: ") {
+				errMessage = line[7:]
 			}
 			continue
 		}
@@ -293,7 +298,10 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
 
 	err = eg.Wait()
 	if err != nil {
-		return nil, err
+		if errMessage != "" {
+			return nil, errors.New(errMessage)
+		}
+		return nil, fmt.Errorf("failed to execute bake: %w", err)
 	}
 
 	b, err = os.ReadFile(metadata.Name())

+ 1 - 2
pkg/e2e/build_test.go

@@ -403,8 +403,7 @@ func TestBuildPlatformsStandardErrors(t *testing.T) {
 		res := c.RunDockerComposeCmdNoCheck(t, "--project-directory", "fixtures/build-test/platforms", "build")
 		res.Assert(t, icmd.Expected{
 			ExitCode: 1,
-			Err: `Multi-platform build is not supported for the docker driver.
-Switch to a different driver, or turn on the containerd image store, and try again.`,
+			Err:      "Multi-platform build is not supported for the docker driver.",
 		})
 	})