From 4258a39bd155d3e776eb041ea7c1eff5e3c2495f Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Thu, 15 Apr 2021 18:50:11 -0700 Subject: [PATCH] sbox: print failing command line before output The full command line run inside sbox can be very long, and printing it after the errors printed by the failing command can hide the error messages. Buffer the output of the command and print the failing command line before the output if it fails. Bug: 185516277 Test: m out/soong/.intermediates/frameworks/base/system-api-stubs-docs-non-updatable/android_common/metalava/api_lint.timestamp with lint error Change-Id: I893f3dd01f1baf195e182111c5c49e92eb82f3b0 --- cmd/sbox/sbox.go | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/cmd/sbox/sbox.go b/cmd/sbox/sbox.go index 9736ff6cf..f124e40b0 100644 --- a/cmd/sbox/sbox.go +++ b/cmd/sbox/sbox.go @@ -255,12 +255,11 @@ func runCommand(command *sbox_proto.Command, tempDir string) (depFile string, er return "", err } - commandDescription := rawCommand - cmd := exec.Command("bash", "-c", rawCommand) + buf := &bytes.Buffer{} cmd.Stdin = os.Stdin - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr + cmd.Stdout = buf + cmd.Stderr = buf if command.GetChdir() { cmd.Dir = tempDir @@ -284,9 +283,21 @@ func runCommand(command *sbox_proto.Command, tempDir string) (depFile string, er copyFiles(command.CopyAfter, tempDir, "", true) } + // If the command was executed but failed with an error, print a debugging message before + // the command's output so it doesn't scroll the real error message off the screen. if exit, ok := err.(*exec.ExitError); ok && !exit.Success() { - return "", fmt.Errorf("sbox command failed with err:\n%s\n%w\n", commandDescription, err) - } else if err != nil { + fmt.Fprintf(os.Stderr, + "The failing command was run inside an sbox sandbox in temporary directory\n"+ + "%s\n"+ + "The failing command line was:\n"+ + "%s\n", + tempDir, rawCommand) + } + + // Write the command's combined stdout/stderr. + os.Stdout.Write(buf.Bytes()) + + if err != nil { return "", err } @@ -298,7 +309,7 @@ func runCommand(command *sbox_proto.Command, tempDir string) (depFile string, er // build error message errorMessage := "mismatch between declared and actual outputs\n" - errorMessage += "in sbox command(" + commandDescription + ")\n\n" + errorMessage += "in sbox command(" + rawCommand + ")\n\n" errorMessage += "in sandbox " + tempDir + ",\n" errorMessage += fmt.Sprintf("failed to create %v files:\n", len(missingOutputErrors)) for _, missingOutputError := range missingOutputErrors {