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

Last list item if commented will be indented two spaces after formatting #112

Open
djgoku opened this issue Apr 12, 2023 · 3 comments
Open
Labels
yaml_v3_problem A bug in the underlying yaml library. These issues are vastly harder to fix.

Comments

@djgoku
Copy link

djgoku commented Apr 12, 2023

Originally posted here: #74 (comment)

input yaml:

repos:
  - repo: https://github.com/antonbabenko/pre-commit-terraform
    rev: v1.77.2
    hooks:
      # - id: infracost_breakdown
      - id: terraform_fmt
      # - id: terraform_providers_lock
      - id: terraform_validate
      # - id: terrascan

output yaml:

repos:
  - repo: https://github.com/antonbabenko/pre-commit-terraform
    rev: v1.77.2
    hooks:
      # - id: infracost_breakdown
      - id: terraform_fmt
      # - id: terraform_providers_lock
      - id: terraform_validate
        # - id: terrascan <-- two leading spaces
@braydonk
Copy link
Collaborator

Hi @djgoku I apologize for the delay in addressing the issue. I haven't had time to look at this project for the last month, but I finally had some time to check today.

This is a really funky edge case with the go-yaml libraries way of parsing comments. I know what the bug is, but I don't have a fix for it right now.

What's happening here is that when the comment is at the end of the array (in YAML syntax grammar called a "mapping block") it does not get rendered properly. I don't know if this is as a hack or for some genuine grammar reason, but when the parser encounters a foot comment for a mapping block like this, it forces it to be the foot comment of the key of the last item in the mapping block, so in this case the id in the last non-comment item. That's why it gets aligned like this. Under some scenarios, it might get mistakenly scanned as the foot comment for hooks and get moved back a tab too.

I couldn't figure out how to fix it while working yesterday but I will try to fix it soon.

@braydonk braydonk added the yaml_v3_problem A bug in the underlying yaml library. These issues are vastly harder to fix. label Oct 25, 2023
@chuckwondo
Copy link

I just encountered this behavior as well. @braydonk, did you ever get a chance to dig into this further?

@braydonk
Copy link
Collaborator

Hi @chuckwondo, sorry I haven't. Most of these problems with the underlying yaml formatting library end up being so time consuming that I can't fit them into the time I have for this project. (See this discussion post for more details)

I can try and take another look next block of yamlfmt time I have to see if I can come up with a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
yaml_v3_problem A bug in the underlying yaml library. These issues are vastly harder to fix.
Projects
None yet
Development

No branches or pull requests

3 participants