virtuald/pyhcl

error parsing 0.12

bruce-szalwinski opened this issue · 1 comments

I'm a big fan of this library so thank you for your efforts! We use it to find internally published modules that need to be updated. Kind of like a dependabot for terraform. We recently started our upgrade from 0.11 to 0.12 and ran into an issue where pyhcl has issues parsing. I've stripped things down to the use case below. This code applys fine using 0.12.28 but fails to parse with latest pyhcl, 0.4.4.

hcl_parser.py

import hcl

with open("locals.tf", "r") as fp:
    try:
        nodes = hcl.load(fp)
    except Exception as e:
        print(f"exception: {e}")

locals.tf

locals {
  ids = sort(
    split(
      ",",
      replace("a,b,c,d,e,f", "a", "z",),
    ),
  )
}

output "ids" {
  value = local.ids
}
exception: Line 5, column 58: unexpected LEFTPAREN; expected ASTERISK_PERIOD, RIGHTBRACKET, COMMA, RIGHTPAREN, ADD, MINUS, MULTIPLY, DIVIDE

Running terraform apply on the same code is successful.

terraform -v  
Terraform v0.12.28

terraform apply
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

ids = [
  "b",
  "c",
  "d",
  "e",
  "f",
  "z",
]

Looks like another HCL2 thing, recommend trying out https://pypi.org/project/python-hcl2/ as I am not planning to implement HCL2 support.