-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
Boolean operators should be capable of converting unknown values to known #31078
Comments
Hi @Nuru! Thanks for sharing this. I think this is roughly the same as the existing issue #24128. Although that one was talking about short circuit in general (not evaluating the second operand at all if the first gives enough information), solving that one would contribute to solving this one in the case you showed where the known value appears first. You are right to note though that the order of the operands doesn't matter when it comes to dealing with unknown values, so this is not entirely covered by the short circuit behavior and so for now I'll leave them both open with links between them in case that helps us to resolve one without the other. In practice though, I would suggest that anyone trying to solve one of them aim to solve them both. So far we've not tried to make the Terraform language very "clever" with spotting special cases like this where one operand has a value that makes the other operand irrelevant to the result, though it does seem to be backward compatible to do so, since we typically consider replacing an unknown value with a compatible known value as an enhancement rather than as a regression. The one concern I have is that it might make it easier to accidentally write a module that works with some input but fails (with "invalid for_each") on other input, and for the module author to not realize that until it is too late to fix without a breaking change. That is already possible, but typically today only with explicit conditionals that tend to make it clearer that there are two possibilities to test when verifying the module behavior. That doesn't mean that we should not do it, but it does suggest that we ought to prioritize improving Terraform's handling of unknown values (#30937) and/or have a more prescriptive solution for testing modules comprehensively in conjunction with making it easier to write expressions that might hide the possibility of a conditional unknown. |
@apparentlymart While I agree with you that it is appropriate to link this issue to the short-circuit feature request, they are different and one is not a substitute for the other. The short-circuit case should cover not just unknown values, but known invalid values, too, such as an index past the end of the array, while this boolean operator issue should cover unknown values whether they come first or second in the expression. The number of ways to fall into Side note: this is what triggered this report. If the Kubernetes provider is fed an unknown value for host = local.x == null || local.x != null ? "example.com" : local.x during |
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. |
Boolean operators
&&
and||
should be capable of converting unknown values to known values based on standard boolean logic.&&
true
false
unknown
true
true
false
unknown
false
false
false
false
unknown
unknown
false
unknown
||
true
false
unknown
true
true
true
true
false
true
false
unknown
unknown
true
unknown
unknown
Terraform Version
Terraform Configuration Files
This is obviously contrived to show the issue, but in practice this sort of thing allows certain configurations to tolerate certain unknowns. For example, a configuration could require either a list of AWS Availability Zones (AZs) or the number of zones to use, with the final list of AZs being either the configured list or the list from a data source, and the final
count
being either the provided count or the size of the configured list of AZs. Currently this has to be carefully managed with the tertiary operator? :
but it should be able to be managed with straightforward boolean arithmetic.Expected Behavior
count
should be known at plan time.Actual Behavior
Error: Invalid count argument
Steps to Reproduce
terraform init
terraform apply
The text was updated successfully, but these errors were encountered: