adrienverge / yamllint

A linter for YAML files.
GNU General Public License v3.0
2.9k stars 278 forks source link

Showing the line range that caused the error #681

Open elken opened 3 months ago

elken commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

Close, because the resulting YAML file is generated from templates the line range alone makes it difficult to discern what actually triggered the error :smile: 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 commented 3 months ago

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

image
elken commented 3 months ago

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 commented 1 month ago

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