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

"panic: inconsistent list element types" when using optional object attributes across multiple modules #31844

Closed
hanneshayashi opened this issue Sep 22, 2022 · 3 comments · Fixed by #31847
Assignees
Labels
bug config confirmed a Terraform Core team member has reproduced this issue v1.3 Issues (primarily bugs) reported against v1.3 releases

Comments

@hanneshayashi
Copy link

Terraform Version

Terraform v1.3.0
on darwin_arm64

Terraform Configuration Files

Root module:

module "my_module" {
  source = "./my_module"
  permissions = [
    {
      role = "foo"
      service_accounts = [
        "bar"
      ]
    },
    {
      role          = "roles/servicenetworking.networksAdmin"
      create_groups = true
    },
  ]
}

my_module:

variable "permissions" {
  type = list(object({
    role             = string
    create_groups    = optional(bool)
    service_accounts = optional(set(string))
    condition = optional(object({
      title       = string
      description = optional(string)
      expression  = string
    }))
  }))
  default = []
}

module "my_submodule" {
  source      = "./my_submodule"
  permissions = var.permissions
}

my_submodule:

variable "permissions" {
  type = list(object({
    role             = string
    create_groups    = optional(bool, false)
    service_accounts = optional(set(string), [])
    condition = optional(object({
      title       = string
      description = optional(string)
      expression  = string
    }))
  }))
  default = []
}

Debug Output

https://gist.github.com/hanneshayashi/fe9e91f2254f733a3cd3e57239bec856

Expected Behavior

Terraform doesn't crash.

Actual Behavior

Terraform crashes.

Steps to Reproduce

  1. terraform init
  2. terraform plan

Additional Context

It seems like Terraform 1.3.0 has a problem with optional attributes in lists of objects. We have multiple nested modules and we were using the experimental feature until now.
Since I can no longer use the default() function, I need to declare my variable in the "lowest" submodule like so:

variable "permissions" {
  type = list(object({
    role             = string
    create_groups    = optional(bool, false)     # Defaults here
    service_accounts = optional(set(string), []) # Defaults here
    condition = optional(object({
      title       = string
      description = optional(string)
      expression  = string
    }))
  }))
  default = []
}

Since I don't want to declare the defaults in all modules that use the submodule I declare the variable like so:

  type = list(object({
    role             = string
    create_groups    = optional(bool)        # No defaults here
    service_accounts = optional(set(string)) # No defaults here
    condition = optional(object({
      title       = string
      description = optional(string)
      expression  = string
    }))
  }))
  default = []}

When I then call the first module with two differently constructed objects Terraform crashes:

module "my_module" {
  source = "./my_module"
  permissions = [
    {
      role = "foo"
      service_accounts = [
        "bar"
      ]
    },
    {
      role          = "roles/servicenetworking.networksAdmin"
      create_groups = true
    },
  ]
}

Removing either of the objects prevents the crash.

References

No response

@hanneshayashi hanneshayashi added bug new new issue not yet triaged labels Sep 22, 2022
@alisdair
Copy link
Member

Thanks for the report! I'm able to reproduce the crash, and I'm looking into the cause.

@alisdair alisdair added confirmed a Terraform Core team member has reproduced this issue v1.3 Issues (primarily bugs) reported against v1.3 releases and removed new new issue not yet triaged labels Sep 22, 2022
@alisdair alisdair self-assigned this Sep 22, 2022
@alisdair
Copy link
Member

A smaller reproduction:

main.tf:

variable "configs" {
  type = list(object({
    a = optional(set(string))
  }))
  default = [
    { a = ["boop"] },
    {},
  ]
}

module "child" {
  source  = "./child"
  configs = var.configs
}

child/main.tf:

variable "configs" {
  type = list(object({
    a = optional(set(string), [])
  }))
}

Same crash:

inconsistent list element types (cty.Object(map[string]cty.Type{"a":cty.Set(cty.String)})
then cty.Object(map[string]cty.Type{"a":cty.EmptyTuple}))

alisdair added a commit that referenced this issue Sep 22, 2022
alisdair added a commit that referenced this issue Sep 22, 2022
@apparentlymart apparentlymart changed the title Crash with new optional attributes in TF 1.3.0 "panic: inconsistent list element types" when using optional object attributes across multiple modules Sep 22, 2022
alisdair added a commit that referenced this issue Sep 23, 2022
@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 Oct 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug config confirmed a Terraform Core team member has reproduced this issue v1.3 Issues (primarily bugs) reported against v1.3 releases
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants