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

Infinite adding EOL #169

Open
dainok opened this issue Apr 23, 2024 · 4 comments
Open

Infinite adding EOL #169

dainok opened this issue Apr 23, 2024 · 4 comments
Labels
yaml_v3_problem A bug in the underlying yaml library. These issues are vastly harder to fix.

Comments

@dainok
Copy link

dainok commented Apr 23, 2024

Hi!

I have a YAML file embedding a Jinja template (cames from Ansible):

list: >-
  [
    {% for host in servers %}
      {"ip": "{{ host }}"},
    {% endfor %}
  ]

Using the dry flag, everything seems ok:

$ yamlfmt -dry test.yaml
2024/04/23 13:49:10 No files will be changed.

Without the dry flag a new line is added:

$ yamlfmt test.yaml
$ cat test.yaml
list: >-
  [

    {% for host in servers %}
      {"ip": "{{ host }}"},
    {% endfor %}
  ]

Every time I run yamlfmt a new line is added.

@braydonk
Copy link
Collaborator

braydonk commented May 3, 2024

Hi @dainok, thanks for opening an issue!

I am able to reproduce the consistently adding a newline behaviour, however when I run -dry with the example yaml, I actually do get a diff produced:

braydonk@bk:~/Git/yamlfmt$ go run ./cmd/yamlfmt/ -dry ./tmp/a.yaml 
./tmp/a.yaml:
  list: >-                       list: >-
    [                              [
+                                
      {% for host in servers %}      {% for host in servers %}
        {"ip": "{{ host }}"},          {"ip": "{{ host }}"},
      {% endfor %}                   {% endfor %}
    ]                              ]

So I'm not sure what is causing the difference between our systems unfortunately...

I did discover something while tinkering though. If I set the retain_line_breaks_single field to true, this doesn't happen anymore. I have no idea why.

braydonk@bk:~/Git/yamlfmt$ go run ./cmd/yamlfmt/ -formatter retain_line_breaks_single=true -dry ./tmp/a.yaml 

(In my intermediate dev version I have a bug that I'm about to fix where the confirmation message isn't printed when there are no diffs, working on it).

So if you are okay with that setting, perhaps that will work as a workaround for now. I notice the log format on No files will be changed, it seems like you might be on a yamlfmt version too old for that setting so you would need to update.

@dainok
Copy link
Author

dainok commented May 3, 2024

Hi @braydonk thank you for your attention.
I made additional tests, and I realized that I was using yamlfmt 0.9.0. Sorry about that.

I update to 0.11.0 and the -dry is reporting the change, and yamlfmt is always adding a new line.

$ yamlfmt -dry test.yaml
2024/05/03 15:12:20 test.yaml:
  list: >-                       list: >-
    [                              [
+
      {% for host in servers %}      {% for host in servers %}
        {"ip": "{{ host }}"},          {"ip": "{{ host }}"},
      {% endfor %}                   {% endfor %}
    ]                              ]

To me retain_line_breaks_single is fine, it doesn't have side effect on my repos.
Thank you

@braydonk
Copy link
Collaborator

braydonk commented May 3, 2024

Awesome, thank you for re-verifying!

I will leave this issue open to keep the constant newline bug around, but similar to other bugs that live in the yaml parsing I won't be able to easily address it.

@braydonk braydonk added the yaml_v3_problem A bug in the underlying yaml library. These issues are vastly harder to fix. label May 3, 2024
@dainok
Copy link
Author

dainok commented May 3, 2024

Sure, I totally understand. Thank you again!

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

2 participants