google / yamlfmt

An extensible command line tool or library to format yaml files.
Apache License 2.0
1.2k stars 48 forks source link

Spaces before comments #208

Open kimllee opened 2 months ago

kimllee commented 2 months ago

Hello,

yamllint 1.35.1 output image

yamlfmt 0.13.0 doesn't add the required spaces asked by yamllint

.yamlfmt file

formatter:
    type: basic
    include_document_start: true
    pad_line_comments: 2

It happens when there's a comment in a like starting by the dash character like this :

...
    rules:
      - # Ingress host
        host: "xxxx.xxx.xxx"
        # Paths for the host
        paths:
          - path: /auth/admin/
            pathType: Prefix
    # Console TLS configuration
    tls:

Am I missing something please ?

Thank you.

braydonk commented 2 months ago

Hi @kimllee,

The underlying yaml parser has a concept of Head, Line, and Foot comments. These are to determine where to place the comments when rendering. A Head comment is a comment above the given yaml node, a line comment is on the same line, and a foot comment is below. pad_line_comments is a config value for adding space padding to line comments (where the default and minimum is 1).

The problem here is that the yaml parser interprets the # Ingress host comment to be a Head Comment to the host: "x" node rather than a Line Comment to the block sequence starting node -.

I'm not confident I could make the yaml parser do what yamllint wants here without potentially breaking other parsing code.

kimllee commented 5 days ago

Hello, I can understand.

Thank you for your answer