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

yamlfmt insert blank line between commented line #34

Closed
sharuzzaman opened this issue Aug 26, 2022 · 8 comments · Fixed by #36
Closed

yamlfmt insert blank line between commented line #34

sharuzzaman opened this issue Aug 26, 2022 · 8 comments · Fixed by #36
Milestone

Comments

@sharuzzaman
Copy link

I have the following YAML:

---
Resources:
  ElasticBeanstalk:
    Type: AWS::CloudFormation::Stack
    Properties:
      #NotificationARNs: 
      #  - String
      #Parameters: 
      #  Key : Value
      #Tags: 
      #  - Tag
      TemplateURL: https://examplebucket.s3.ap-southeast-1.amazonaws.com/example-file.json
      TimeoutInMinutes: 5

after formatting, each commented line will have a blank line inserted, becoming:

Resources:
  ElasticBeanstalk:
    Type: AWS::CloudFormation::Stack
    Properties:
      #NotificationARNs: 

      #  - String

      #Parameters: 

      #  Key : Value

      #Tags: 

      #  - Tag
      TemplateURL: https://examplebucket.s3.ap-southeast-1.amazonaws.com/example-file.json
      TimeoutInMinutes: 5

I don't see any option to configure the formatter so that it does not include a blank line after each comment.

Thank you.

@braydonk
Copy link
Collaborator

braydonk commented Aug 26, 2022

Hi @sharuzzaman,

Unfortunately at this time there aren't a ton of configuration options, so you wouldn't be able to configure a behaviour like this for the time being.

However, I'm unable to reproduce this behaviour locally at the moment. I tried copying the yaml from the first block into a file on its own and it did not insert the newlines as you've mentioned.

x.yaml:

---
Resources:
  ElasticBeanstalk:
    Type: AWS::CloudFormation::Stack
    Properties:
      #NotificationARNs: 
      #  - String
      #Parameters: 
      #  Key : Value
      #Tags: 
      #  - Tag
      TemplateURL: https://examplebucket.s3.ap-southeast-1.amazonaws.com/example-file.json
      TimeoutInMinutes: 5

Piping it into stdin:

braydonk@bk:~/Git/yamlfmt$ cat x.yaml | yamlfmt -
---
Resources:
  ElasticBeanstalk:
    Type: AWS::CloudFormation::Stack
    Properties:
      #NotificationARNs: 
      #  - String
      #Parameters: 
      #  Key : Value
      #Tags: 
      #  - Tag
      TemplateURL: https://examplebucket.s3.ap-southeast-1.amazonaws.com/example-file.json
      TimeoutInMinutes: 5

There's a chance this is a platform issue. I'm running this on Linux. What operating system are you on?

@sharuzzaman
Copy link
Author

I'm running yamlfmt on Windows 10 using the Windows command line

@braydonk
Copy link
Collaborator

This seems to be a Windows-specific issue. Gotta be some kind of CRLF problem if I had to guess. I'll keep looking into it.

@braydonk braydonk added this to the v0.3.0 milestone Aug 26, 2022
@braydonk
Copy link
Collaborator

This is tough. It's a combination of 2 bugs in yaml.v3, one is pretty simple and one is not.

yaml.Encoder always writes \n, I'll make a PR upstream that makes it configurable to write \r\n instead

When the parser scans a head comment ending in \r\n, if the next line is anything but another line break, it inserts an extra line. This is deep in the parser code, and won't be fast to solve...

The silver lining is that this is not a universal Windows issue, this is particularly with CRLF line endings. That is a possible workaround if you are flexible with line endings in your project. Regardless, I'll do what I can to get this solved.

@braydonk
Copy link
Collaborator

braydonk commented Aug 27, 2022

For the line ending problem, I opened go-yaml/yaml#891 and a PR for it go-yaml/yaml#892

@braydonk
Copy link
Collaborator

I managed to find the problem in the parser code too. Opened go-yaml/yaml#893 and go-yaml/yaml#894

@braydonk
Copy link
Collaborator

I've opened a hotfix for the behaviour while waiting for the upstream stuff to get fixed, and that's what will go in v0.3.0 so that Windows users can be fixed sooner.

@sharuzzaman
Copy link
Author

I would like to update that this issue is not happening again and the hotfix is working correctly. Thank you

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

Successfully merging a pull request may close this issue.

2 participants