hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.4k stars 9.5k forks source link

terraform plan -generate-config-out crashed Terraform due to nil pointer dereference when using import blocks #34463

Closed maastha closed 8 months ago

maastha commented 9 months ago

The resource used is implemented in Terraform Plugin Framework. The import works as expected when running terraform import but crashes when trying to generate configuration using import blocks.

Terraform Version

Terraform v1.5.0
on darwin_arm64
+ provider registry.terraform.io/mongodb/mongodbatlas v1.13.1

Your version of Terraform is out of date! The latest version
is 1.6.6. You can update by downloading from https://www.terraform.io/downloads.html

Terraform Configuration Files

import {
  to = mongodbatlas_advanced_cluster.cluster-no-oc-collections
  id = "658d490d9140b13493dc029d-tested-cluster-single-provider2"
}

import {
  to = mongodbatlas_advanced_cluster.cluster-with-collections
  id = "658d490d9140b13493dc029d-cluster-single-with-collec2"
}

Debug Output

https://gist.github.com/maastha/8b71c7262a3a0aea97c6fad486d76247#file-gistfile1-txt

Expected Behavior

Terraform should generate configuration file for imported resource

Actual Behavior

Terraform crashes with error: runtime error: invalid memory address or nil pointer dereference

Steps to Reproduce

  1. verify resource for import exists
  2. run terraform import mongodbatlas_advanced_cluster.cluster-with-collections <id>-cluster-single-with-collec2
  3. terraform imports the resource and updates state successfully
  4. remove imported resource from state.
  5. verify resource for import exists
  6. create import blocks in Terraform config
  7. run terraform plan -generate-config-out=generated.tf
  8. Terraform crashes

Additional Context

References

No response

kmoe commented 8 months ago

Thanks for the bug report. That crash definitely shouldn't happen. Would you mind posting the whole output that Terraform gives when it crashes? There should be a stack trace underneath the "runtime error" line.

Since I don't have the provider I can't reproduce this, but it is possible that this is fixed in a later Terraform version than the one you're using. Please try using Terraform v1.6.6 or v1.7.0-rc1.

maastha commented 8 months ago

@kmoe Sharing the stack trace here (v1.5.0):

runtime error: invalid memory address or nil pointer dereference
goroutine 104 [running]:
runtime/debug.Stack()
        /Users/runner/hostedtoolcache/go/1.20.0/x64/src/runtime/debug/stack.go:24 +0x64
runtime/debug.PrintStack()
        /Users/runner/hostedtoolcache/go/1.20.0/x64/src/runtime/debug/stack.go:16 +0x1c
github.com/hashicorp/terraform/internal/logging.PanicHandler()
        /Users/runner/work/terraform/terraform/internal/logging/panic.go:58 +0x164
panic({0x102c90e40, 0x104422530})
        /Users/runner/hostedtoolcache/go/1.20.0/x64/src/runtime/panic.go:890 +0x248
github.com/hashicorp/terraform/internal/configs/configschema.(*Block).Filter(0x1400099e450, 0x14000cff650, 0x103043e98)
        /Users/runner/work/terraform/terraform/internal/configs/configschema/filter.go:53 +0x1c4
github.com/hashicorp/terraform/internal/terraform.(*NodePlannableResourceInstance).generateHCLStringAttributes(0x140006c6b00, {{}, {0x1044a5a90, 0x0, 0x0}, {{}, {{}, 0x4d, {0x14000581ca0, 0x1d}, ...}, ...}}, ...)
        /Users/runner/work/terraform/terraform/internal/terraform/node_resource_plan_instance.go:602 +0xf4
github.com/hashicorp/terraform/internal/terraform.(*NodePlannableResourceInstance).importState(0x140006c6b00, {0x1030892b8, 0x1400053e540}, {{}, {0x1044a5a90, 0x0, 0x0}, {{}, {{}, 0x4d, ...}, ...}}, ...)
        /Users/runner/work/terraform/terraform/internal/terraform/node_resource_plan_instance.go:563 +0xcf8
github.com/hashicorp/terraform/internal/terraform.(*NodePlannableResourceInstance).managedResourceExecute(0x140006c6b00, {0x1030892b8, 0x1400053e540})
        /Users/runner/work/terraform/terraform/internal/terraform/node_resource_plan_instance.go:190 +0x538
github.com/hashicorp/terraform/internal/terraform.(*NodePlannableResourceInstance).Execute(0x0?, {0x1030892b8?, 0x1400053e540?}, 0xb0?)
        /Users/runner/work/terraform/terraform/internal/terraform/node_resource_plan_instance.go:74 +0x7c
github.com/hashicorp/terraform/internal/terraform.(*ContextGraphWalker).Execute(0x140010ce7e0, {0x1030892b8, 0x1400053e540}, {0x12c4d2538, 0x140006c6b00})
        /Users/runner/work/terraform/terraform/internal/terraform/graph_walk_context.go:143 +0xa8
github.com/hashicorp/terraform/internal/terraform.(*Graph).walk.func1({0x102ff8c60, 0x140006c6b00})
        /Users/runner/work/terraform/terraform/internal/terraform/graph.go:78 +0x238
github.com/hashicorp/terraform/internal/dag.(*Walker).walkVertex(0x140000b9b00, {0x102ff8c60, 0x140006c6b00}, 0x1400102ba00)
        /Users/runner/work/terraform/terraform/internal/dag/walk.go:384 +0x2b8
created by github.com/hashicorp/terraform/internal/dag.(*Walker).Update
        /Users/runner/work/terraform/terraform/internal/dag/walk.go:307 +0xaf4
maastha commented 8 months ago

@kmoe Unfortunately the issue persists in 1.6.6 and 1.7-rc1, here's the stack trace:

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

runtime error: invalid memory address or nil pointer dereference
goroutine 101 [running]:
runtime/debug.Stack()
        /Users/runner/hostedtoolcache/go/1.21.5/x64/src/runtime/debug/stack.go:24 +0x64
runtime/debug.PrintStack()
        /Users/runner/hostedtoolcache/go/1.21.5/x64/src/runtime/debug/stack.go:16 +0x1c
github.com/hashicorp/terraform/internal/logging.PanicHandler()
        /Users/runner/work/terraform/terraform/internal/logging/panic.go:58 +0x164
panic({0x10795f5c0?, 0x10977bc30?})
        /Users/runner/hostedtoolcache/go/1.21.5/x64/src/runtime/panic.go:920 +0x26c
github.com/hashicorp/terraform/internal/configs/configschema.(*Block).Filter(0x140005b0cc0, 0x14000acc490, 0x107e6e440)
        /Users/runner/work/terraform/terraform/internal/configs/configschema/filter.go:56 +0x1c8
github.com/hashicorp/terraform/internal/terraform.(*NodePlannableResourceInstance).generateHCLStringAttributes(0x14000826210, {{}, {0x10980eb20, 0x0, 0x0}, {{}, {{}, 0x4d, {0x14000644fc0, 0x1d}, ...}, ...}}, ...)
        /Users/runner/work/terraform/terraform/internal/terraform/node_resource_plan_instance.go:645 +0xc4
github.com/hashicorp/terraform/internal/terraform.(*NodePlannableResourceInstance).importState(0x14000826210, {0x107ed3d70, 0x14000a5e2d0}, {{}, {0x10980eb20, 0x0, 0x0}, {{}, {{}, 0x4d, ...}, ...}}, ...)
        /Users/runner/work/terraform/terraform/internal/terraform/node_resource_plan_instance.go:606 +0xff4
github.com/hashicorp/terraform/internal/terraform.(*NodePlannableResourceInstance).managedResourceExecute(0x14000826210, {0x107ed3d70, 0x14000a5e2d0})
        /Users/runner/work/terraform/terraform/internal/terraform/node_resource_plan_instance.go:169 +0x3a4
github.com/hashicorp/terraform/internal/terraform.(*NodePlannableResourceInstance).Execute(0x1512006a0?, {0x107ed3d70?, 0x14000a5e2d0?}, 0x80?)
        /Users/runner/work/terraform/terraform/internal/terraform/node_resource_plan_instance.go:75 +0x7c
github.com/hashicorp/terraform/internal/terraform.(*ContextGraphWalker).Execute(0x140007c6140, {0x107ed3d70, 0x14000a5e2d0}, {0x1510ac1c0, 0x14000826210})
        /Users/runner/work/terraform/terraform/internal/terraform/graph_walk_context.go:148 +0xa0
github.com/hashicorp/terraform/internal/terraform.(*Graph).walk.func1({0x107e39720?, 0x14000826210})
        /Users/runner/work/terraform/terraform/internal/terraform/graph.go:87 +0x2b4
github.com/hashicorp/terraform/internal/dag.(*Walker).walkVertex(0x1400068e3c0, {0x107e39720, 0x14000826210}, 0x14000828c40)
        /Users/runner/work/terraform/terraform/internal/dag/walk.go:384 +0x2a8
created by github.com/hashicorp/terraform/internal/dag.(*Walker).Update in goroutine 133
        /Users/runner/work/terraform/terraform/internal/dag/walk.go:307 +0xb0c

If it helps at all, here's the provider repo branch https://github.com/mongodb/terraform-provider-mongodbatlas/tree/CLOUDP-215171-adv-cluster-rs-framework

github-actions[bot] commented 7 months ago

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.