Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use line number from ParseError #1130

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/pint/tests/0004_fail_invalid_yaml.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ cmp stderr stderr.txt
level=INFO msg="Loading configuration file" path=.pint.hcl
level=INFO msg="Finding all rules to check" paths=["rules"]
level=WARN msg="Failed to parse file content" err="error at line 4: did not find expected key" path=rules/bad.yaml lines=1-7
rules/bad.yaml:1 Fatal: YAML parser returned an error when reading this file: `error at line 4: did not find expected key`. (yaml/parse)
1 | xxx:
rules/bad.yaml:4 Fatal: error at line 4: did not find expected key (yaml/parse)
4 |

rules/ok.yml:5 Fatal: Prometheus failed to parse the query with this PromQL error: unclosed left bracket. (promql/syntax)
5 | expr: sum(foo[5m)
Expand Down
4 changes: 2 additions & 2 deletions cmd/pint/tests/0010_syntax_check.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ cmp stderr stderr.txt
level=INFO msg="Loading configuration file" path=.pint.hcl
level=INFO msg="Finding all rules to check" paths=["rules"]
level=WARN msg="Failed to parse file content" err="error at line 6: did not find expected '-' indicator" path=rules/1.yaml lines=1-12
rules/1.yaml:1 Fatal: YAML parser returned an error when reading this file: `error at line 6: did not find expected '-' indicator`. (yaml/parse)
1 | - alert: Good
rules/1.yaml:6 Fatal: error at line 6: did not find expected '-' indicator (yaml/parse)
6 |

level=INFO msg="Problems found" Fatal=1
level=ERROR msg="Fatal error" err="found 1 problem(s) with severity Bug or higher"
Expand Down
4 changes: 2 additions & 2 deletions cmd/pint/tests/0067_relaxed.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ cmp stderr stderr.txt
level=INFO msg="Loading configuration file" path=.pint.hcl
level=INFO msg="Finding all rules to check" paths=["rules"]
level=WARN msg="Failed to parse file content" err="error at line 2: top level field must be a groups key, got list" path=rules/strict.yml lines=1-4
rules/strict.yml:1 Fatal: YAML parser returned an error when reading this file: `error at line 2: top level field must be a groups key, got list`. (yaml/parse)
1 | {%- raw %} # pint ignore/line
rules/strict.yml:2 Fatal: error at line 2: top level field must be a groups key, got list (yaml/parse)
2 | - alert: No Owner

level=INFO msg="Problems found" Fatal=1
level=ERROR msg="Fatal error" err="found 1 problem(s) with severity Bug or higher"
Expand Down
4 changes: 2 additions & 2 deletions cmd/pint/tests/0074_strict_error.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ cmp stderr stderr.txt
-- stderr.txt --
level=INFO msg="Finding all rules to check" paths=["rules"]
level=WARN msg="Failed to parse file content" err="error at line 2: invalid group key alert" path=rules/strict.yml lines=1-9
rules/strict.yml:1 Fatal: YAML parser returned an error when reading this file: `error at line 2: invalid group key alert`. (yaml/parse)
1 | groups:
rules/strict.yml:2 Fatal: error at line 2: invalid group key alert (yaml/parse)
2 | - alert: Conntrack_Table_Almost_Full

level=INFO msg="Problems found" Fatal=1
level=ERROR msg="Fatal error" err="found 1 problem(s) with severity Bug or higher"
Expand Down
4 changes: 2 additions & 2 deletions cmd/pint/tests/0078_repeated_group.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ cmp stderr stderr.txt
-- stderr.txt --
level=INFO msg="Finding all rules to check" paths=["rules"]
level=WARN msg="Failed to parse file content" err="error at line 4: duplicated group name" path=rules/strict.yml lines=1-5
rules/strict.yml:1 Fatal: YAML parser returned an error when reading this file: `error at line 4: duplicated group name`. (yaml/parse)
1 | groups:
rules/strict.yml:4 Fatal: error at line 4: duplicated group name (yaml/parse)
4 | - name: foo

level=INFO msg="Problems found" Fatal=1
level=ERROR msg="Fatal error" err="found 1 problem(s) with severity Bug or higher"
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Improved accuracy of the [rule/duplicate](checks/rule/duplicate.md) check.
- Fixed GitHub reporter trying to create pull request comments to unmodified lines - #1120.
- Fixed colored output on some environments - #1106.
- Show correct line number when reporting YAML syntax errors.

## v0.65.1

Expand Down
11 changes: 6 additions & 5 deletions internal/checks/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (c ErrorCheck) Check(_ context.Context, _ discovery.Path, _ parser.Rule, _
func parseRuleError(rule parser.Rule, err error) Problem {
var commentErr comments.CommentError
var ignoreErr discovery.FileIgnoreError
var parseErr parser.ParseError

switch {
case errors.As(err, &ignoreErr):
Expand All @@ -86,15 +87,15 @@ func parseRuleError(rule parser.Rule, err error) Problem {
Severity: Warning,
}

case err != nil:
slog.Debug("yaml syntax report", slog.Any("err", err))
case errors.As(err, &parseErr):
slog.Debug("parse error", slog.Any("err", parseErr))
return Problem{
Lines: parser.LineRange{
First: 1,
Last: 1,
First: parseErr.Line,
Last: parseErr.Line,
},
Reporter: yamlParseReporter,
Text: fmt.Sprintf("YAML parser returned an error when reading this file: `%s`.", err),
Text: parseErr.Error(),
Details: `pint cannot read this file because YAML parser returned an error.
This usually means that you have an indention error or the file doesn't have the YAML structure required by Prometheus for [recording](https://prometheus.io/docs/prometheus/latest/configuration/recording_rules/) and [alerting](https://prometheus.io/docs/prometheus/latest/configuration/alerting_rules/) rules.
If this file is a template that will be rendered into valid YAML then you can instruct pint to ignore some lines using comments, see [pint docs](https://cloudflare.github.io/pint/ignoring.html).
Expand Down
2 changes: 1 addition & 1 deletion internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (p Parser) Parse(content []byte) (rules []Rule, err error) {

defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("unable to parse YAML file: %s", r)
err = ParseError{Err: fmt.Errorf("unable to parse YAML file: %s", r)}
}
}()

Expand Down