amplify-education / python-hcl2

MIT License
240 stars 53 forks source link

HEREDOC Parsing Issue #25

Closed hugespoon closed 4 years ago

hugespoon commented 4 years ago

It looks like when using a HEREDOC and ending it on the same line as another character, the parser does not properly detect the end of the doc and will lump the next resource in as well. Example:

resource "aws_sqs_queue" "SQS_RESOURCE_NAME" {
  name                       = "SQS_QUEUE_NAME"
  visibility_timeout_seconds = 30
  message_retention_seconds  = 1209600
  max_message_size           = 262144
  delay_seconds              = 0
  receive_wait_time_seconds  = 0

  policy = <<POLICY
{
  "Version": "2012-10-17",
  "Id": "SomePolicyID",
  "Statement": [
    {
      "Sid": "Sid12345",
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": "SQS:SendMessage",
      "Resource": "SOME:SQS:ARN",
      "Condition": {
        "ArnEquals": {
          "aws:SourceArn": "${data.some.source.arn}"
        }
      }
    }
  ]
}POLICY

  redrive_policy = <<POLICY
{
  "deadLetterTargetArn":"${data.some.dead.arn}",
  "maxReceiveCount":1
}
POLICY
}

In this case, the 'policy' key in the dictionary will contain both the 'policy' and 'redrive_policy' information. Moving the first 'POLICY' HEREDOC terminator to its own line fixes this issue.

aoskotsky-amplify commented 4 years ago

Thanks for pointing this out.

The HCL2 spec says The template expression ends when the given identifier subsequently appears again on a line of its own. in the heredoc section https://github.com/hashicorp/hcl2/blob/master/hcl/hclsyntax/spec.md#template-expressions

That seems to mean that the terminator has to appear on a new line by itself. Let me know if I'm reading that wrong

hugespoon commented 4 years ago

I think you are right. I was using this on some TF 0.11.x (which doesn't use HCL2) where this was valid. Apologies for the mistake!