JCC writes build errors to a file for reliable future access. (#11015)

Some projects (e.g., `mosh`) do not report the root build error sent to
`stderr`.
Saving the errors to a file so that we won't miss any and can always
access them later.
This commit is contained in:
Dongge Liu 2023-10-09 16:10:44 +11:00 committed by GitHub
parent 447456003a
commit b7ee7edf58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 10 deletions

View File

@ -341,6 +341,25 @@ func CppifyHeaderIncludes(contents string) (string, error) {
return contents, nil return contents, nil
} }
func AppendStringToFile(filepath, new_content string) error {
// Appends |new_content| to the content of |filepath|.
file, err := os.OpenFile(filepath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer file.Close()
_, err = file.WriteString(new_content)
return err
}
func WriteStdErrOut(outstr string, errstr string) {
// Prints |outstr| to stdout, prints |errstr| to stderr, and saves |errstr| to err.log.
fmt.Print(outstr)
fmt.Fprint(os.Stderr, errstr)
AppendStringToFile("/out/err.log", errstr)
}
func main() { func main() {
f, err := os.OpenFile("/tmp/jcc.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) f, err := os.OpenFile("/tmp/jcc.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
@ -367,8 +386,7 @@ func main() {
} }
retcode, out, errstr := Compile(bin, newArgs) retcode, out, errstr := Compile(bin, newArgs)
if retcode == 0 { if retcode == 0 {
fmt.Print(out) WriteStdErrOut(out, errstr)
fmt.Fprint(os.Stderr, errstr)
os.Exit(0) os.Exit(0)
} }
@ -389,8 +407,7 @@ func main() {
// Nothing else we can do. Just write the error and exit. // Nothing else we can do. Just write the error and exit.
// Just print the original error for debugging purposes and // Just print the original error for debugging purposes and
// to make build systems happy. // to make build systems happy.
fmt.Print(out) WriteStdErrOut(out, errstr)
fmt.Fprint(os.Stderr, errstr)
os.Exit(retcode) os.Exit(retcode)
} }
fixret, fixout, fixerr := TryFixCCompilation(newArgs) fixret, fixout, fixerr := TryFixCCompilation(newArgs)
@ -398,15 +415,12 @@ func main() {
// We failed, write stdout and stderr from the first failure and // We failed, write stdout and stderr from the first failure and
// from fix failures so we can know what the code did wrong and // from fix failures so we can know what the code did wrong and
// how to improve jcc to fix more issues. // how to improve jcc to fix more issues.
fmt.Print(out) WriteStdErrOut(out, errstr)
fmt.Fprint(os.Stderr, errstr)
fmt.Println("\nFix failure") fmt.Println("\nFix failure")
fmt.Print(fixout)
// Print error back to stderr so tooling that relies on this can proceed // Print error back to stderr so tooling that relies on this can proceed
fmt.Fprint(os.Stderr, fixerr) WriteStdErrOut(fixout, fixerr)
os.Exit(retcode) os.Exit(retcode)
} }
// The fix suceeded, write its out and err. // The fix suceeded, write its out and err.
fmt.Print(fixout) WriteStdErrOut(fixout, fixerr)
fmt.Fprint(os.Stderr, fixerr)
} }