VladRassokhin / intellij-hcl

HCL language support for IntelliJ platform based IDEs
Apache License 2.0
244 stars 47 forks source link

Suggested conversion to HCL2 expression is causing Ambiguous attribute key error #303

Closed karol-bujacek closed 3 years ago

karol-bujacek commented 4 years ago

Thank you for opening an issue. In this template paragraph text could be removed, however please retain headers.

Prerequisites

Installation details

IntelliJ IDEA 2020.1.2 (Ultimate Edition) Build #IU-201.7846.76, built on June 1, 2020

Terraform Configuration Files

provider "aws" {
  region = "eu-central-1"
}

variable "tags" {
    type = map(string)
    default = {
        Terraform = "true"
    }
}

variable "inventory_tag" {
  type = string
  default = "Inventory"
}

resource "aws_instance" "default" {
  ami = "ami-0b6d8a6db0c665fb7"
  instance_type = "t3.nano"
  tags = merge({
    "${var.inventory_tag}": "foobar"  # <-------- THIS LINE
  }, var.tags)
}

After accepting the suggested change:

resource "aws_instance" "default" {
  ami = "ami-0b6d8a6db0c665fb7"
  instance_type = "t3.nano"
  tags = merge({
    var.inventory_tag: "foobar"
  }, var.tags)
}

Exception

$ terraform plan

Error: Ambiguous attribute key

  on main.tf line 21, in resource "aws_instance" "default":
  21:     var.inventory_tag: "foobar"

If this expression is intended to be a reference, wrap it in parentheses. If
it's instead intended as a literal name containing periods, wrap it in quotes
to create a string literal.

Expected Behavior

Plugin should not suggest to change such interpolations to HCL2 expressions.

Actual Behavior

Plugin is suggesting changes which leads to Terraform error.

Steps to Reproduce

  1. Accept suggested change ‘Convert to HCL2 expression’
  2. Execute terraform plan command
VladRassokhin commented 3 years ago

Fixed in next version, will produce (var.inventory_tag) replacement as terraform error recommends