You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've been tortured by this issue for months, although I workaround each one eventually. I think it'd better be improved from terraform side, then I won't need to do extra workaround and write those ugly and unreadable code any more.
Consider such snippet of code
variable "a_list" {
type = "list"
default = []
}
data "template_file" "test" {
template = "$${text}"
vars {
text = "${length(var.a_list) > 0 ? element(var.a_list, 0) : ""}"
}
}
Terraform will fail on this code because element() doesn't like empty list. But you know what, that is why I didn't write it like text = ${element(var.a_list, 0)}" in the first place. I do understand that element() will go wrong on empty list, so I add the condition just to wish it won't be called when list is empty, but obviously I was wrong, it will be evaluated always.
So I think the way ternary operator works is not good enough. When list a_list is empty, the ternary operation will never run into the first part, it will return "". That means It is unnecessary to bother to evaluate the first part which is already determined to be impossible.
Another example (pseudo code)
list = ["pear", "orange", "peach", "banana"]
text = "${contains(list, "apple") ? index(list, "apple") : 0)}"
Likewise, I know index() will go wrong if the element doesn't exist in the list, so that's why I didn't write text = "${index(list, "apple")}" in the first place. But same, even I put the index() in the ternary operation, it will be evaluated anyway regardless of the condition.
Terraform Version
0.9.x - 0.10.x, they all have the issue. I guess <0.9 also have
The text was updated successfully, but these errors were encountered:
I think you are describing here the same issue covered by #15605, so I'm going to close this to consolidate the discussion over there. Work to fix this is in progress and will come in a future release.
No @apparentlymart , no need to be sorry, honestly. Terraform is awesome. You guys are great and brilliant.
Maybe my opening looks a little inpatient, but I am not complaining, just want to show some emergency so that you guys might give priority to this issue, hopefully ;) In fact, I want to make some contributions, but the only thing I can do is to use it and find issues. Hope it gets better and better.
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.
ghost
locked and limited conversation to collaborators
Apr 6, 2020
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I've been tortured by this issue for months, although I workaround each one eventually. I think it'd better be improved from terraform side, then I won't need to do extra workaround and write those ugly and unreadable code any more.
Consider such snippet of code
Terraform will fail on this code because
element()
doesn't like empty list. But you know what, that is why I didn't write it liketext = ${element(var.a_list, 0)}"
in the first place. I do understand thatelement()
will go wrong on empty list, so I add the condition just to wish it won't be called when list is empty, but obviously I was wrong, it will be evaluated always.So I think the way ternary operator works is not good enough. When list
a_list
is empty, the ternary operation will never run into the first part, it will return""
. That means It is unnecessary to bother to evaluate the first part which is already determined to be impossible.Another example (pseudo code)
Likewise, I know
index()
will go wrong if the element doesn't exist in the list, so that's why I didn't writetext = "${index(list, "apple")}"
in the first place. But same, even I put theindex()
in the ternary operation, it will be evaluated anyway regardless of the condition.Terraform Version
0.9.x - 0.10.x, they all have the issue. I guess <0.9 also have
The text was updated successfully, but these errors were encountered: