Closed bernadinm closed 5 years ago
Hi @bernadinm,
I think actually the problem there is that period is not a valid character in an identifier, and unquoted map keys are now required to be identifiers. Wrapping those strings in quotes should get the desired effect. However, that error message could certainly be more actionable by noticing that the next character is a period and stating more specifically that periods are not allowed there and to put the value in quotes, since indeed HCL1 was more forgiving about that.
Having looked more closely at this, I see that it's a more subtle problem than I first thought.
The string centos_7.2_ap-south-1
tokenizes as Ident("centos_7") Dot Number(2) Ident("_ap-south-1")
, because an identifier can't start with a digit. This means the parser interprets it as an alternative syntax for centos_7[2]
and then terminates expression parsing, returning to the object constructor parser with Ident("_ap-south-1")
as the next token, which then fails because an =
is expected there.
This means we can't get quite as precise a helpful error message as I originally hoped, but I'm going to add a "Did you mean...?" suggestion to the error message in the situation where an identifier appears right after the attribute key expression to at least give a hint as to what fix is required here.
Some good news side from that is that for Terraform 0.12 in particular it will include a tool to help with making these upgrades and it will be able to make this particular fix automatically, wrapping your keys here in quotes once it detects that they are not valid HCL2 identifiers.
In 7ace05a3bed93021f87190cbb1f2e58bde8d593c I have added two new error messages, one of which addresses the situation you encountered here. As noted in my previous comment, we can't recognize this pattern unambiguously and so I instead phrased the error message as a hint as to what the problem might be:
Error: Missing key/value separator
(source code reference)
Expected an equals sign ("=") to mark the beginning of the attribute
value. If you intended to given an attribute name containing periods
or spaces, write the name in quotes to create a string literal.
This combined with the Terraform-specific configuration upgrade tool is, I think, the best we can do here without introducing confusing ambiguities into the language now that object/map keys are allowed to be arbitrary expressions rather than just literal strings. This is one of the situations where a slight incompatibility is an unavoidable consequence of supporting first-class expressions where HCL 1 did not.
When testing out
terraform-0.12.0
for forwards compatibility, I wanted to see if our scripts need to be changed. We've started changing some things until we've encountered this below which seems like underscores needs to be processed and looks like it may be a minor bug.