@@ -72,6 +72,17 @@ func lintFile(filePath string, fix bool) (LintResult, error) {
7272 Message : fmt .Sprintf ("File must contain apiVersion: %s" , constants .Troubleshootv1beta3Kind ),
7373 Field : "apiVersion" ,
7474 })
75+ // Try to fix wrong apiVersion
76+ if fix {
77+ fixed , err := applyFixes (filePath , string (content ), result )
78+ if err != nil {
79+ return result , err
80+ }
81+ if fixed {
82+ // Re-lint to verify fixes
83+ return lintFile (filePath , false )
84+ }
85+ }
7586 return result , nil
7687 }
7788
@@ -80,18 +91,42 @@ func lintFile(filePath string, fix bool) (LintResult, error) {
8091
8192 // Validate YAML syntax (but be lenient with templated files)
8293 var parsed map [string ]interface {}
83- if err := yaml .Unmarshal (content , & parsed ); err != nil {
94+ yamlParseErr := yaml .Unmarshal (content , & parsed )
95+ if yamlParseErr != nil {
8496 // If the file has templates, YAML parsing may fail - that's expected
8597 // We'll still try to validate what we can
8698 if ! hasTemplates {
8799 result .Errors = append (result .Errors , LintError {
88- Line : extractLineFromError (err ),
89- Message : fmt .Sprintf ("YAML syntax error: %s" , err .Error ()),
100+ Line : extractLineFromError (yamlParseErr ),
101+ Message : fmt .Sprintf ("YAML syntax error: %s" , yamlParseErr .Error ()),
90102 })
103+ // Don't return yet - we want to try to fix this error
104+ // Continue to applyFixes at the end
105+ if fix {
106+ fixed , err := applyFixes (filePath , string (content ), result )
107+ if err != nil {
108+ return result , err
109+ }
110+ if fixed {
111+ // Re-lint to verify fixes
112+ return lintFile (filePath , false )
113+ }
114+ }
91115 return result , nil
92116 }
93117 // For templated files, we can't parse YAML strictly, so just check template syntax
94118 result .Errors = append (result .Errors , checkTemplateSyntax (string (content ))... )
119+ // Continue to applyFixes for templates too
120+ if fix {
121+ fixed , err := applyFixes (filePath , string (content ), result )
122+ if err != nil {
123+ return result , err
124+ }
125+ if fixed {
126+ // Re-lint to verify fixes
127+ return lintFile (filePath , false )
128+ }
129+ }
95130 return result , nil
96131 }
97132
@@ -388,12 +423,23 @@ func applyFixes(filePath, content string, result LintResult) (bool, error) {
388423
389424 for _ , err := range errs {
390425 // Fix 1: Add missing colon
426+ // YAML parsers often report the error on the line AFTER the actual problem
391427 if strings .Contains (err .Message , "could not find expected ':'" ) {
428+ // Check current line first
392429 if ! strings .Contains (line , ":" ) {
393430 trimmed := strings .TrimSpace (line )
394431 indent := line [:len (line )- len (strings .TrimLeft (line , " \t " ))]
395432 line = indent + trimmed + ":"
396433 fixed = true
434+ } else if lineNum > 1 {
435+ // Check previous line (where the colon is likely missing)
436+ prevLine := lines [lineNum - 2 ]
437+ if ! strings .Contains (prevLine , ":" ) && strings .TrimSpace (prevLine ) != "" {
438+ trimmed := strings .TrimSpace (prevLine )
439+ indent := prevLine [:len (prevLine )- len (strings .TrimLeft (prevLine , " \t " ))]
440+ lines [lineNum - 2 ] = indent + trimmed + ":"
441+ fixed = true
442+ }
397443 }
398444 }
399445
0 commit comments