From 0c3d2e3d333ecad7384d31698ed9a076b93abf42 Mon Sep 17 00:00:00 2001 From: jonathanmetzman <31354670+jonathanmetzman@users.noreply.github.com> Date: Thu, 21 Sep 2023 21:21:39 -0400 Subject: [PATCH] Improve stdout/stderr handling and make sure errors are debuggable (#10993) Whenever we fail, ensure there's a proper stderr written to stderr. Also, only write stderr to stderr never to stdout --- infra/base-images/base-builder/jcc/jcc.go | 27 +++++++++++++++++------ 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/infra/base-images/base-builder/jcc/jcc.go b/infra/base-images/base-builder/jcc/jcc.go index cb4014609..5bdc3931a 100644 --- a/infra/base-images/base-builder/jcc/jcc.go +++ b/infra/base-images/base-builder/jcc/jcc.go @@ -368,32 +368,45 @@ func main() { retcode, out, errstr := Compile(bin, newArgs) if retcode == 0 { fmt.Print(out) - fmt.Print(errstr) - // Print error back to stderr so tooling that relies on this can proceed fmt.Fprint(os.Stderr, errstr) os.Exit(0) } + // Note that on failures or when we succeed on the first try, we should + // try to write the first out/err to stdout/stderr. + // When we fail we should try to write the original out/err and one from + // the corrected. + headersFixed, _ := CorrectMissingHeaders(bin, newArgs) if headersFixed { + // We succeeded here but it's kind of complicated to get out and + // err from TryCompileAndFixHeadersOnce. The output and err is + // not so important on success so just be silent. os.Exit(0) } if isCPP { - // Nothing else we can do. Just print the error and exit. + // Nothing else we can do. Just write the error and exit. + // Just print the original error for debugging purposes and + // to make build systems happy. fmt.Print(out) - fmt.Print(err) + fmt.Fprint(os.Stderr, errstr) os.Exit(retcode) } fixret, fixout, fixerr := TryFixCCompilation(newArgs) if fixret != 0 { + // We failed, write stdout and stderr from the first failure and + // from fix failures so we can know what the code did wrong and + // how to improve jcc to fix more issues. fmt.Print(out) - fmt.Print(errstr) + fmt.Fprint(os.Stderr, errstr) fmt.Println("\nFix failure") fmt.Print(fixout) - fmt.Print(fixerr) // Print error back to stderr so tooling that relies on this can proceed - fmt.Fprint(os.Stderr, errstr) + fmt.Fprint(os.Stderr, fixerr) os.Exit(retcode) } + // The fix suceeded, write its out and err. + fmt.Print(fixout) + fmt.Fprint(os.Stderr, fixerr) }