aws-cloudformation / rain

A development workflow tool for working with AWS CloudFormation.
Apache License 2.0
749 stars 69 forks source link

Cannot use rain fmt with Intrinsic function containing a list under a node with a comment #441

Open dd-924 opened 1 week ago

dd-924 commented 1 week ago

When running rain fmt on a Cloudformation template, if there is an intrinsic function where a list is to be defined under a node with a comment on the same line, rain will throw the error invalid YAML: yaml: line 1403: could not find expected ':'.

Example:

Environment: # Using If condition
  Fn::If:
    - IsProd
    - prod
    - dev

Substitution: # Using Sub function
  Fn::Sub:
    - String
    - Var1Name: Var1Value
      Var2Name: Var2Value

NOTE: The fmt command WILL work with any other spelling that does not match with an intrinsic function, ex.

Substitution: # Misspelling of "sub" works
  Fn::Subb:
    - String
    - Var1Name: Var1Value
      Var2Name: Var2Value

Using Shorthand Notation

There is slightly different behavior when using shorthand. Rain will throw an error when there is a list of any kind following the function name, no matter how the intrinsic function is spelled.

Environment: # Using shorthand
  !If [ IsProd, prod, dev ]

Environment: # Misspelling of "If" fails
  !Iff
   - IsProd
   - prod
   - dev

Other Cases for Context

The rain fmt will work if the comment is a line below the parent node of the function

environment: 
# Test Comment
  Fn::If:
    - IsProd
    - prod
    - dev

Lastly it will also work in the case that there is no list defined after the function name, ex:

Environment: # Empty list
  !If

Environment: # Empty list
  Fn::If:

It seems as though there is an issue with a list being defined if an intrinsic function is below a node with a comment on the same line.

ericzbeard commented 1 week ago

I'll look into this but in general any YAML errors are coming from the go yaml parser, which I can't fix myself. End-of-line comments tend to cause weird issues with YAML, so I try to avoid them.