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

fmt command gives "semantic difference after formatting" error for multi-line values #126

Closed
JWFGit opened this issue Apr 20, 2023 · 3 comments · Fixed by #131
Closed

fmt command gives "semantic difference after formatting" error for multi-line values #126

JWFGit opened this issue Apr 20, 2023 · 3 comments · Fixed by #131
Assignees
Labels
bug Something isn't working

Comments

@JWFGit
Copy link

JWFGit commented Apr 20, 2023

rain fmt command does not work with multi-line yaml

Using this CF template

AWSTemplateFormatVersion: "2010-09-09"
Description: |
  Template generated by rain
  and edited my me
Resources:
  MyFunction:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        ZipFile: |
          import boto3
          import cfnresponse

          def handler(event, context):
              print(event)

      Description: Test lambda for rain fmt
      FunctionName: my-lambda
      Handler: index.handler
      ReservedConcurrentExecutions: 0 # Optional
      Role: arn:aws:iam::123456789012:role/aws-service-role/support.amazonaws.com/AWSServiceRoleForSupport
      Runtime: python3.8

      Tags:
        - Key: my_tag
          Value: ME
      Timeout: 100

running ./rain --version

Rain v1.3.3 darwin/amd64

when the fmt command is run

./rain fmt -v my_test/lambda.yaml

semantic difference after formatting:
(>) Description: |
  Template generated by rain

  and edited my me
(|) Resources:
(|)   MyFunction:
(|)     Properties:
(|)       Code:
(>)         ZipFile: |
  i        mport boto3
  i        mport cfnresponse
  d        ef handler(event, context):
              print(event)

@ericzbeard ericzbeard self-assigned this Apr 21, 2023
@ericzbeard
Copy link
Contributor

I think this is fixed by #117, which hasn't been released yet.

@ugwis
Copy link
Contributor

ugwis commented May 1, 2023

@ericzbeard
I had faced same issue in rain merge command at latest commit.

$ git log --oneline | head -n 1
e889d62 Merge pull request #130 from ericzbeard/forecast-bucket

$ git show 2af4ccc
commit 2af4cccaf6ea69b11efe2af73c498d1369e77e72
Merge: 4525cb1 163d5e6
Author: Eric Z. Beard <ericzbeard@gmail.com>
Date:   Fri Apr 7 16:10:22 2023 -0700

    Merge pull request #117 from ericzbeard/fix-110
    
    Fix formatting issue with multiline strings

$ cat test.template
Resources:
  CGW:
    Type: AWS::EC2::Instance
    Properties:
      UserData: !Base64
        Fn::Sub:
          - |
            #!/bin/bash -xe
            apt-get update

            apt-get upgrade -y
$ go build ./cmd/rain
$ ./rain merge test.template /dev/null
Resources:
  CGW:
    Type: AWS::EC2::Instance
    Properties:
      UserData: !Base64
        Fn::Sub:
          - |

            #!/bin/bash -xe
            apt-get update


            apt-get upgrade -y

I found these multi-line issues caused by go-yaml.v3 generating empty line without indent before formating.

source, err := parse.String(string(input))

I tested in go playground.
https://go.dev/play/p/OJt4xbqKWiE
Screen Shot 2023-05-02 at 0 38 26

Not sure where it was specificated in YAML 1.1 spec, but it seems good to follow go.yaml.v3 implementation to treat empty line without indentation as multi-line string.

I create PR.
#131

Thanks.

ericzbeard added a commit that referenced this issue May 1, 2023
Treat empty line without indentation as multi-line string #126
@ericzbeard
Copy link
Contributor

@ugwis Thanks for the contribution! This will be included in v1.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants