Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It'd better that the ternary operator doesn't evaluate the expression in impossible part #16680

Closed
ckyoog opened this issue Nov 16, 2017 · 3 comments

Comments

@ckyoog
Copy link

ckyoog commented Nov 16, 2017

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

@apparentlymart
Copy link
Contributor

Hi @ckyoog! Sorry for this unfortunate behavior.

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.

@ckyoog
Copy link
Author

ckyoog commented Nov 17, 2017

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.

@ghost
Copy link

ghost commented Apr 6, 2020

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 ghost locked and limited conversation to collaborators Apr 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants