hcl-lang icon indicating copy to clipboard operation
hcl-lang copied to clipboard

Duplicate completion candidates for `for_each` & `for` collections

Open radeksimko opened this issue 2 years ago • 1 comments

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:

Screenshot 2022-12-01 at 12 19 10

resource "aws_ec2_fleet" "name" {
  for_each = ["value"]
  dynamic "spot_options" {
    for_each = # HERE
  }
}

We should only provide one completion candidate.

radeksimko avatar Dec 01 '22 12:12 radeksimko

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).

radeksimko avatar Dec 06 '22 15:12 radeksimko