Skip to content

Report & fix ambiguous attribute key #1550

Open
@radeksimko

Description

@radeksimko

Background

HCL may report an attribute key as invalid when it's unclear whether it's reference or literal key. For example:

variable "toot" {
  default = "foot"
}

output "example" {
  value = {
    var.toot = "noot"
  }
}

This is currently not surfaced directly by the LS as a diagnostic because HCL only produces the diagnostic when obtaining Value() of the expression. The language server does use HCL parser but expressions are more often inspected as individual (Go) types one by one rather than evaluated to get the value. i.e. that codepath never gets executed in the context of LS.

It can be surfaced by running terraform validate however:

╷
│ Error: Ambiguous attribute key
│ 
│   on test.tf line 7, in output "example":
│    7:     var.toot = "noot"
│ 
│ 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.
╵

and so it can also be surfaced by calling the experimental validate command:

Screenshot 2024-01-05 at 11 59 59

Proposal

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions