Add errorHints to stdout when read-only file system errors are detected
The source tree will eventually be made ReadOnly, and recipes that write
directly to the source tree will fail. Use a pattern-match approach on
the results of stdout/stderr to provide hints to the user in such a
scenario.
If multiple patterns are found in raw output, print error hint
corresponding to first pattern match. first pattern match is chosen
since the failing function will be at the top of the stack, and hence
will be logged first
Test: Wrote a unit test to assert errorhint is added to output.
Wrote an integration test that writes to a file in the source tree
1. When source_tree is RO, the recipe fails and an error hint is printed
to stdout
2. When source tree is RW, the recipe succeeds and no error hint is
printed
Bug: 174726238
Change-Id: Id67b48f8094cdf8a571c239ae469d60464a1e89c
diff --git a/ui/status/ninja.go b/ui/status/ninja.go
index 765679f..2445972 100644
--- a/ui/status/ninja.go
+++ b/ui/status/ninja.go
@@ -19,6 +19,8 @@
"fmt"
"io"
"os"
+ "regexp"
+ "strings"
"syscall"
"time"
@@ -158,9 +160,10 @@
err = fmt.Errorf("exited with code: %d", exitCode)
}
+ outputWithErrorHint := errorHintGenerator.GetOutputWithErrorHint(msg.EdgeFinished.GetOutput(), exitCode)
n.status.FinishAction(ActionResult{
Action: started,
- Output: msg.EdgeFinished.GetOutput(),
+ Output: outputWithErrorHint,
Error: err,
Stats: ActionResultStats{
UserTime: msg.EdgeFinished.GetUserTime(),
@@ -219,3 +222,53 @@
return ret, nil
}
+
+// key is pattern in stdout/stderr
+// value is error hint
+var allErrorHints = map[string]string{
+ "Read-only file system": `\nWrite to a read-only file system detected. Possible fixes include
+1. Generate file directly to out/ which is ReadWrite, #recommend solution
+2. BUILD_BROKEN_SRC_DIR_RW_ALLOWLIST := <my/path/1> <my/path/2> #discouraged, subset of source tree will be RW
+3. BUILD_BROKEN_SRC_DIR_IS_WRITABLE := true #highly discouraged, entire source tree will be RW
+`,
+}
+var errorHintGenerator = *newErrorHintGenerator(allErrorHints)
+
+type ErrorHintGenerator struct {
+ allErrorHints map[string]string
+ allErrorHintPatternsCompiled *regexp.Regexp
+}
+
+func newErrorHintGenerator(allErrorHints map[string]string) *ErrorHintGenerator {
+ var allErrorHintPatterns []string
+ for errorHintPattern, _ := range allErrorHints {
+ allErrorHintPatterns = append(allErrorHintPatterns, errorHintPattern)
+ }
+ allErrorHintPatternsRegex := strings.Join(allErrorHintPatterns[:], "|")
+ re := regexp.MustCompile(allErrorHintPatternsRegex)
+ return &ErrorHintGenerator{
+ allErrorHints: allErrorHints,
+ allErrorHintPatternsCompiled: re,
+ }
+}
+
+func (errorHintGenerator *ErrorHintGenerator) GetOutputWithErrorHint(rawOutput string, buildExitCode int) string {
+ if buildExitCode == 0 {
+ return rawOutput
+ }
+ errorHint := errorHintGenerator.getErrorHint(rawOutput)
+ if errorHint == nil {
+ return rawOutput
+ }
+ return rawOutput + *errorHint
+}
+
+// Returns the error hint corresponding to the FIRST match in raw output
+func (errorHintGenerator *ErrorHintGenerator) getErrorHint(rawOutput string) *string {
+ firstMatch := errorHintGenerator.allErrorHintPatternsCompiled.FindString(rawOutput)
+ if _, found := errorHintGenerator.allErrorHints[firstMatch]; found {
+ errorHint := errorHintGenerator.allErrorHints[firstMatch]
+ return &errorHint
+ }
+ return nil
+}