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

Showing the line range that caused the error #681

Open
elken opened this issue Aug 1, 2024 · 5 comments
Open

Showing the line range that caused the error #681

elken opened this issue Aug 1, 2024 · 5 comments

Comments

@elken
Copy link

elken commented Aug 1, 2024

Thanks for yamllint, this is a great tool!

The usecase we have for it is ensuring that our Helm templates don't include things like duplicate keys and such that helm lint doesn't pick up by default; and as such we pipe the result of the command that generates the output into yamllint.

Because there's no file that we can look at, having the failed line numbers isn't quite that useful as it's built from multiple templates.

Would be really good if this could be added 😄 Thanks!

@adrienverge
Copy link
Owner

Hello ! I'm not sure I understand well what you mean. You don't care about the failed lines and columns, but you care about the subpart of the YAML code that caused the failure. Is that it?

@elken
Copy link
Author

elken commented Aug 1, 2024

Close, because the resulting YAML file is generated from templates the line range alone makes it difficult to discern what actually triggered the error 😄 Showing the line range and maybe 1/2 extra lines for padding/context would be very useful.

If you're not a domain expert, a quick skim of the docs should assist in explaining why this would be hard to debug.

@andrewimeson
Copy link
Contributor

Perhaps a new output format (--format) called "contextual" that looks similar to ShellCheck's default output?

image

@elken
Copy link
Author

elken commented Aug 2, 2024

Exactly that yes! I don't think it needs to be too fancy, just show the line range (maybe behind an option in case people want the current behaviour instead) and 1/2 extra either side for context.

@guitarrapc
Copy link

guitarrapc commented Oct 10, 2024

I'm doing similar with simple sed.

    if ! result=$(echo "$yaml" | yamllint --format "parsable" -); then
      # error message
      echo "$result"

      line=$(echo "$result" | cut -d':' -f2)
      context_lines=3  # line numer to show before&after (e.g. 1 line)

      # Adjust not to exceed under 1st line
      start_line=$((line - context_lines))
      if [ "$start_line" -lt 1 ]; then
        start_line=1
      fi
      # Adjust not to exceed over final line
      total_lines=$(echo "$manifest" | wc -l)
      end_line=$((line + context_lines))
      if [ "$end_line" -gt "$total_lines" ]; then
        end_line=$total_lines
      fi

      # show line
      echo "$manifest" | cat -n - | sed -n "${start_line},${end_line}p"
      exit 1
    fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants