terraform 0.12 panics when calling distinct with empty list

Created on 23 May 2019  ยท  4Comments  ยท  Source: hashicorp/terraform

Terraform Version

$ terraform -v
Terraform v0.12.0

Terraform Configuration Files

locals {
  d = distinct([])
}

Debug Output

https://gist.github.com/alex-goncharov/d51af9a2b88a089cb32154abb031081a

Crash Output

https://gist.github.com/alex-goncharov/66f756fc609371862200e69a97b5f6f2

Expected Behavior

distinct returned empty list

Actual Behavior

terraform panics

Steps to Reproduce

create file with about hcl
terraform init && terraform plan

Additional Context

References

bug config crash

Most helpful comment

Sorry for this crash @alex-goncharov, and thanks for reporting it.

This seems to just be a missing case not handled in the function implementation, here:

https://github.com/hashicorp/terraform/blob/f68673fbe4ae7f97741a60979a56afbff6f3b53d/lang/funcs/collection.go#L366

cty.ListVal relies on getting a non-empty list in order to know what element type the resulting list will have, so it panics if given an empty list. This should be fixable then by adding an extra guard condition just before:

if len(list) == 0 {
    return cty.ListValEmpty(retType.ElementType), nil
}

We'll get this fixed up! Thanks again for reporting it.

All 4 comments

Sorry for this crash @alex-goncharov, and thanks for reporting it.

This seems to just be a missing case not handled in the function implementation, here:

https://github.com/hashicorp/terraform/blob/f68673fbe4ae7f97741a60979a56afbff6f3b53d/lang/funcs/collection.go#L366

cty.ListVal relies on getting a non-empty list in order to know what element type the resulting list will have, so it panics if given an empty list. This should be fixable then by adding an extra guard condition just before:

if len(list) == 0 {
    return cty.ListValEmpty(retType.ElementType), nil
}

We'll get this fixed up! Thanks again for reporting it.

As a workaround to this issue I write it like this - compact(distinct(concat(var.maybe_empty, [""])))

https://github.com/terraform-aws-modules/terraform-aws-security-group/blob/687465a8f04af85fae13fd10d8982b12590debb7/modules/http-80/main.tf#L15

This should be fixed by #21538.

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings