Skip to content

panic occurs when nested object declared in schema #29538

@yufeiminds

Description

@yufeiminds

Module version

➜ terraform -version
Terraform v1.0.6
on darwin_amd64
+ provider hashicorp.com/edu/hashicups v0.3.2

Relevant provider source code

// Order Resource schema
func (r resourceOrderType) GetSchema(_ context.Context) (schema.Schema, []*tfprotov6.Diagnostic) {
	return schema.Schema{
		Attributes: map[string]schema.Attribute{
			"id": {
				Type: types.StringType,
				// When Computed is true, the provider will set value --
				// the user cannot define the value
				Computed: true,
			},
			"last_updated": {
				Type:     types.StringType,
				Computed: true,
			},
			"items": {
				// If Required is true, Terraform will throw error if user
				// doesn't specify value
				// If Optional is true, user can choose to supply a value
				Required: true,
				Attributes: schema.ListNestedAttributes(map[string]schema.Attribute{
					"quantity": {
						Type:     types.NumberType,
						Required: true,
					},
					"coffee": {
						Required: true,
						Attributes: schema.SingleNestedAttributes(map[string]schema.Attribute{
							"id": {
								Type:     types.NumberType,
								Required: true,
							},
							"name": {
								Type:     types.StringType,
								Computed: true,
							},
							"teaser": {
								Type:     types.StringType,
								Computed: true,
							},
							"description": {
								Type:     types.StringType,
								Computed: true,
							},
							"price": {
								Type:     types.NumberType,
								Computed: true,
							},
							"image": {
								Type:     types.StringType,
								Computed: true,
							},
							"ingredients": {
								Optional: true,
								Attributes: schema.ListNestedAttributes(map[string]schema.Attribute{
									"ingredient_id": {
										Type:     types.NumberType,
										Optional: true,
									},
									"name": {
										Type:     types.StringType,
										Optional: true,
									},
									"quantity": {
										Type:     types.NumberType,
										Optional: true,
									},
									"unit": {
										Type:     types.StringType,
										Optional: true,
									},
								}, schema.ListNestedAttributesOptions{}),
							},
						}),
					},
				}, schema.ListNestedAttributesOptions{}),
			},
		},
	}, nil
}

Terraform Configuration Files

terraform {
  required_providers {
    hashicups = {
      version = "~> 0.3.1"
      source  = "hashicorp.com/edu/hashicups"
    }
  }
}
provider "hashicups" {
  username = "education"
  password = "test123"
  host     = "http://localhost:19090"
}

resource "hashicups_order" "edu" {
  items = [{
    coffee = {
      id = 3

      ingredients = [{
        name = "sugar"
      }]

    }
    quantity = 2
    }, {
    coffee = {
      id = 1
    }
    quantity = 2
    }
  ]
}

output "edu_order" {
  value = hashicups_order.edu
}

Debug Output

Crash Log on Gist

Expected Behavior

No crash for nested objects.

Actual Behavior

hashicups_order.edu: Refreshing state... [id=1]
panic: inconsistent list element types (cty.Object(map[string]cty.Type{"coffee":cty.Object(map[string]cty.Type{"description":cty.String, "id":cty.Number, "image":cty.String, "ingredients":cty.List(cty.Object(map[string]cty.Type{"ingredient_id":cty.Number, "name":cty.String, "quantity":cty.Number, "unit":cty.String})), "name":cty.String, "price":cty.Number, "teaser":cty.String}), "quantity":cty.Number}) then cty.Object(map[string]cty.Type{"coffee":cty.Object(map[string]cty.Type{"description":cty.String, "id":cty.Number, "image":cty.String, "ingredients":cty.List(cty.ObjectWithOptionalAttrs(map[string]cty.Type{"ingredient_id":cty.Number, "name":cty.String, "quantity":cty.Number, "unit":cty.String}, []string{"ingredient_id", "name", "quantity", "unit"})), "name":cty.String, "price":cty.Number, "teaser":cty.String}), "quantity":cty.Number}))

goroutine 131 [running]:
github.com/zclconf/go-cty/cty.ListVal(0xc0003569c0, 0x2, 0x2, 0x2f88240, 0xc00039cfc0, 0x37babf8, 0xc000353940)
	/Users/distiller/go/pkg/mod/github.com/zclconf/go-cty@v1.9.1/cty/value_init.go:166 +0x57e
github.com/hashicorp/terraform/internal/plans/objchange.proposedNewNestedType(0xc0004704e0, 0x37bab88, 0xc000353c00, 0x2e66040, 0xc00000e2b8, 0x37bab88, 0xc000353a00, 0x2e66040, 0xc00000e288, 0xc00000e288, ...)
	/Users/distiller/project/project/internal/plans/objchange/objchange.go:346 +0x6d3
github.com/hashicorp/terraform/internal/plans/objchange.proposedNewAttributes(0xc000907380, 0x37babf8, 0xc000353c10, 0x2f88240, 0xc00039cc00, 0x37babf8, 0xc000353a30, 0x2f88240, 0xc00039c990, 0x0)
	/Users/distiller/project/project/internal/plans/objchange/objchange.go:295 +0x49a
github.com/hashicorp/terraform/internal/plans/objchange.proposedNew(0xc0009073e0, 0x37babf8, 0xc000353c10, 0x2f88240, 0xc00039cc00, 0x37babf8, 0xc000353a30, 0x2f88240, 0xc00039c990, 0x20, ...)
	/Users/distiller/project/project/internal/plans/objchange/objchange.go:80 +0x165
github.com/hashicorp/terraform/internal/plans/objchange.ProposedNew(0xc0009073e0, 0x37babf8, 0xc000353c10, 0x2f88240, 0xc00039cc00, 0x37babf8, 0xc000353a30, 0x2f88240, 0xc00039c990, 0x0, ...)
	/Users/distiller/project/project/internal/plans/objchange/objchange.go:42 +0x11f
github.com/hashicorp/terraform/internal/terraform.(*NodeAbstractResourceInstance).plan(0xc000737340, 0x37ec5a0, 0xc0001d2b60, 0x0, 0xc0003dfa40, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
	/Users/distiller/project/project/internal/terraform/node_resource_abstract_instance.go:732 +0xdab
github.com/hashicorp/terraform/internal/terraform.(*NodePlannableResourceInstance).managedResourceExecute(0xc0002f90e0, 0x37ec5a0, 0xc0001d2b60, 0x100c408, 0x32fd020, 0xc000a1bc50)
	/Users/distiller/project/project/internal/terraform/node_resource_plan_instance.go:196 +0x8cb
github.com/hashicorp/terraform/internal/terraform.(*NodePlannableResourceInstance).Execute(0xc0002f90e0, 0x37ec5a0, 0xc0001d2b60, 0xd6ddc02, 0xc000a1bce8, 0x100d305, 0x2fd3580)
	/Users/distiller/project/project/internal/terraform/node_resource_plan_instance.go:54 +0xb3
github.com/hashicorp/terraform/internal/terraform.(*ContextGraphWalker).Execute(0xc0006588f0, 0x37ec5a0, 0xc0001d2b60, 0xd6ddc30, 0xc0002f90e0, 0x0, 0x0, 0x0)
	/Users/distiller/project/project/internal/terraform/graph_walk_context.go:129 +0xbf
github.com/hashicorp/terraform/internal/terraform.(*Graph).walk.func1(0x32fd020, 0xc0002f90e0, 0x0, 0x0, 0x0)
	/Users/distiller/project/project/internal/terraform/graph.go:59 +0xbd3
github.com/hashicorp/terraform/internal/dag.(*Walker).walkVertex(0xc0003df200, 0x32fd020, 0xc0002f90e0, 0xc0006d32c0)
	/Users/distiller/project/project/internal/dag/walk.go:381 +0x288
created by github.com/hashicorp/terraform/internal/dag.(*Walker).Update
	/Users/distiller/project/project/internal/dag/walk.go:304 +0x1246




!!!!!!!!!!!!!!!!!!!!!!!!!!! TERRAFORM CRASH !!!!!!!!!!!!!!!!!!!!!!!!!!!!

Terraform crashed! This is always indicative of a bug within Terraform.
A crash log has been placed at "crash.951271563.log" relative to your current
working directory. It would be immensely helpful if you could please
report the crash with Terraform[1] so that we can fix this.

When reporting bugs, please include your terraform version. That
information is available on the first line of crash.log. You can also
get it by running 'terraform --version' on the command line.

SECURITY WARNING: the "crash.951271563.log" file that was created may contain
sensitive information that must be redacted before it is safe to share
on the issue tracker.

[1]: https://github.com/hashicorp/terraform/issues

!!!!!!!!!!!!!!!!!!!!!!!!!!! TERRAFORM CRASH !!!!!!!!!!!!!!!!!!!!!!!!!!!!

Steps to Reproduce

Pre-requirements: Start the hashicups server according to []

  1. Clone my fork: yufeiminds/terraform-provider-hashicups-pf
  2. make install
  3. cd examples && terraform init
  4. terraform apply
  5. See the panic that occurs

References

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions