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

Strange behavior with optional type constraint and variable default values #32109

Closed
seggcsont opened this issue Oct 28, 2022 · 4 comments · Fixed by #32178
Closed

Strange behavior with optional type constraint and variable default values #32109

seggcsont opened this issue Oct 28, 2022 · 4 comments · Fixed by #32178
Assignees
Labels
bug cty Use in conjunction with "upstream" when cty is the relevant upstream v1.3 Issues (primarily bugs) reported against v1.3 releases

Comments

@seggcsont
Copy link

seggcsont commented Oct 28, 2022

Terraform Version

Terraform v1.3.3
on linux_amd64

Terraform Configuration Files

variable "input" {
  type = list(object({
    a = string                # a required attribute
    b = optional(string)      # an optional attribute
    c = optional(number, 127) # an optional attribute with default value
    d = optional(list(any))
  }))
  default = [
    { a = "a" }, { a = "b", d = [1] }
  ]
}

output "out" {
  value = var.input
}

Debug Output

https://gist.github.com/seggcsont/e7a04c37155e876c4868e9acdc46268b

Expected Behavior

Without variable override it should use default value

Actual Behavior

Error: Invalid default value for module argument

on 1.tf line 1:
1: variable "input" {

The default value for variable "input" is incompatible with its type
constraint: element types must all match for conversion to list.

Steps to Reproduce

  1. terraform init
  2. terraform plan
  3. run command:
    tf plan -var "input=[{a=1},{a=2, d=[]}]"
    tf plan -var "input=[{a=1},{a=2, d=[1]}]"
    tf plan -var "input=[{a=1},{a=2}]"
    tf plan -var "input=[{a=1}]"
    tf plan

Additional Context

It works if you apply one of these:

  • remove the 1 from the b=[1] so it will be an empty list
  • use optional default value d = optional(list(any), [])
  • change type any to number like d = optional(list(number))

References

No response

@seggcsont seggcsont added bug new new issue not yet triaged labels Oct 28, 2022
@crw
Copy link
Collaborator

crw commented Nov 4, 2022

Thanks for this report.

@kmoe kmoe self-assigned this Nov 4, 2022
@kmoe
Copy link
Member

kmoe commented Nov 4, 2022

Thanks for the well-written bug report. This shouldn't be happening: a null list should be compatible with a concrete list(number) regardless of initial type constraint. It's interesting to see it works with an empty list - perhaps cty should handle null and empty lists similarly in this case. Will investigate upstream.

@seggcsont, I'm curious as to why you use list(any) here instead of list(number) - could you say more about your use case?

@kmoe kmoe added cty Use in conjunction with "upstream" when cty is the relevant upstream v1.3 Issues (primarily bugs) reported against v1.3 releases and removed new new issue not yet triaged labels Nov 4, 2022
@seggcsont
Copy link
Author

@seggcsont, I'm curious as to why you use list(any) here instead of list(number) - could you say more about your use case?

In my real case it is a list complex object and I do not really use any. I wanted to create a small peace of that code to try out something and I faced this issue.

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug cty Use in conjunction with "upstream" when cty is the relevant upstream v1.3 Issues (primarily bugs) reported against v1.3 releases
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants