pyhcl icon indicating copy to clipboard operation
pyhcl copied to clipboard

Error converting .tf into .tf.json when there is a variable with a non primitive type

Open Rami-Kassouf-FOO opened this issue 1 year ago • 3 comments

Basically this is a minimal reconstruction of the issue

variables.tf:

variable "efs_tags" {
  description = "Tags for the EFS"
  type        = map(string)
  default = {
    Terraform = "true"
    Project   = "" 
  }
}

python code

import json
from pathlib import Path
import hcl

file_path = Path("./variables.tf")
with open(file_path) as fp:
    json_object = hcl.load(fp)

file_path = file_path.with_suffix('.tf.json')
with open(file_path, 'w') as fp:
    json.dump(json_object, fp, indent=4)

expected output:

{
    "variable": {
         "efs_tags": {
                  "description": "Tags for the EFSr",
                  "type": "map(string)",
                  "default": {
                          "Terraform": "true",
                          "Project": ""
                  }
        }
}

Actual output:

{
    "variable": {
         "efs_tags": {
                  "description": "Tags for the EFSr",
                  "type": "map(\"string\")",
                  "default": {
                          "Terraform": "true",
                          "Project": ""
                  }
        }
}

Rami-Kassouf-FOO avatar Dec 17 '24 14:12 Rami-Kassouf-FOO

Basically this is a minimal reconstruction of the issue

variables.tf:

variable "efs_tags" {
  description = "Tags for the EFS"
  type        = map(string)
  default = {
    Terraform = "true"
    Project   = "" 
  }
}

python code

import json
from pathlib import Path
import hcl

file_path = Path("./variables.tf")
with open(file_path) as fp:
    json_object = hcl.load(fp)

file_path = file_path.with_suffix('.tf.json')
with open(file_path, 'w') as fp:
    json.dump(json_object, fp, indent=4)

expected output:

{
    "variable": {
         "efs_tags": {
                  "description": "Tags for the EFSr",
                  "type": "map(string)",
                  "default": {
                          "Terraform": "true",
                          "Project": ""
                  }
        }
}

Actual output:

{
    "variable": {
         "efs_tags": {
                  "description": "Tags for the EFS",
                  "type": "map(\"string\")",
                  "default": {
                          "Terraform": "true",
                          "Project": ""
                  }
        }
}

A workaround is to simply remove any line that has a type={complex type}

Rami-Kassouf-FOO avatar Dec 17 '24 14:12 Rami-Kassouf-FOO

That syntax doesn't seem familiar to me, this feels like an HCL2 feature?

virtuald avatar Dec 17 '24 17:12 virtuald

Weirdly enough I kept switching back and forth between this and python-hcl2 and this library worked better for transforming a .tf file into it's json counterpart so I ended up going with it

The HCL2 had it's own issue which i references here python-hcl2issues/179

Does HCL not support something like key = value1(value2), because if it does support it it shouldn't add the " " right?

Rami-Kassouf-FOO avatar Dec 18 '24 11:12 Rami-Kassouf-FOO