hcl-lang
hcl-lang copied to clipboard
Duplicate completion candidates for `for_each` & `for` collections
Currently we return two duplicate completion candidates when completing for_each under a dynamic block, in a block which also has the "standalone" for_each:

resource "aws_ec2_fleet" "name" {
for_each = ["value"]
dynamic "spot_options" {
for_each = # HERE
}
}
We should only provide one completion candidate.
This appears to be slightly more complicated than I originally expected.
Basically, the problem is that we first gather constraints and then do completion for each constraint individually.
Specifically for the mentioned example, the trigger is that for_each has multiple open constraints
https://github.com/hashicorp/hcl-lang/blob/9f07ba77f1d7135627f81c1998efcb7c61726ef8/decoder/decoder.go#L258-L263
and these are compared against the reference target of (also open) type cty.Dynamic results in the two duplicate matches.
https://github.com/hashicorp/hcl-lang/blob/9f07ba77f1d7135627f81c1998efcb7c61726ef8/decoder/expression_candidates.go#L19-L41
They both represent the same reference target (i.e. reference target collection works as expected), but first is a match against cty.Map(cty.DynamicPseudoType) and the second is a match against cty.Set(cty.String).