Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4258a39b authored by Colin Cross's avatar Colin Cross
Browse files

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
parent fc2d842d
Loading
Loading
Loading
Loading
+18 −7
Original line number Diff line number Diff line
@@ -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 {