From fc6c2954e7f0fba00ba6cae949aa03d3b79b5c2c Mon Sep 17 00:00:00 2001 From: Travis Raines <571832+rainest@users.noreply.github.com> Date: Fri, 22 Dec 2023 15:33:08 -0500 Subject: [PATCH] feat(debug) include absolute line number in errors For YAML syntax errors, calculate the absolute line number of the problem line in the aggregate output. Wrap the existing document-relative line number in another error mentioning this line number. Signed-off-by: Travis Raines <571832+rainest@users.noreply.github.com> --- pkg/action/action.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pkg/action/action.go b/pkg/action/action.go index 5693f4838..7ba254984 100644 --- a/pkg/action/action.go +++ b/pkg/action/action.go @@ -23,6 +23,7 @@ import ( "path" "path/filepath" "regexp" + "strconv" "strings" "github.com/pkg/errors" @@ -172,11 +173,27 @@ func (cfg *Configuration) renderResources(ch *chart.Chart, values chartutil.Valu // // We return the files as a big blob of data to help the user debug parser // errors. + lineCount := 0 + offset := 0 for name, content := range files { if strings.TrimSpace(content) == "" { continue } + if strings.Contains(err.Error(), name) { + re := regexp.MustCompile(`line \d+`) + line := re.FindString(err.Error()) + if line != "" { + lineNumber, err := strconv.Atoi(strings.Split(line, " ")[1]) + if err == nil { + offset = lineCount + 2 + lineNumber // 2 accounts for the header this adds after + } + } + } fmt.Fprintf(b, "---\n# Source: %s\n%s\n", name, content) + lineCount += strings.Count(content, "\n") + 3 // 3 accounts for the header and newline between manifests + } + if offset != 0 { + err = fmt.Errorf("YAML parse error on line %d of complete output: %w", offset, err) } return hs, b, "", err }