Skip to content

Commit f9b321a

Browse files
fix panic when import id is empty string
We can not detect the empty ID earlier (e.g. during config parsing) since it's an HCL expression to be evaluated. Since go uses the default value of "" for strings which is the value we try to guard against we can not properly differciate between an invalid configuration and the user not wanting to import the resource. We could change the type of the importID field to *string to make this differenciation, but that seemed to invasive for the scope of this bug. Fixes #33389
1 parent 64cd0da commit f9b321a

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

internal/terraform/node_resource_abstract_instance.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -752,15 +752,16 @@ func (n *NodeAbstractResourceInstance) plan(
752752

753753
// If we're importing and generating config, generate it now.
754754
if n.Config == nil {
755-
// This shouldn't happen. A node that isn't generating config should
756-
// have embedded config, and the rest of Terraform should enforce this.
757-
// If, however, we didn't do things correctly the next line will panic,
758-
// so let's not do that and return an error message with more context.
755+
// This can happen if the importID of a node is set to "".
756+
// A node that isn't generating config should have embedded config,
757+
// and the rest of Terraform should enforce this.
758+
// If, however, the user has an import block with id="" we might
759+
// end up here, #so let's return an error message with more context.
759760

760761
diags = diags.Append(tfdiags.Sourceless(
761762
tfdiags.Error,
762763
"Resource has no configuration",
763-
fmt.Sprintf("Terraform attempted to process a resource at %s that has no configuration. This is a bug in Terraform; please report it!", n.Addr.String())))
764+
fmt.Sprintf("Terraform attempted to process a resource at %s that has no configuration. This is can happen if you are using an import block and have the id to import from set to an empty string, in this case please provide a valid id to import from. If you are not doing that, then this is a bug in Terraform; please report it!", n.Addr.String())))
764765
return nil, nil, keyData, diags
765766
}
766767

internal/terraform/node_resource_plan_instance.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ func (n *NodePlannableResourceInstance) managedResourceExecute(ctx EvalContext)
255255
// If we are importing and generating a configuration, we need to
256256
// ensure the change is written out so the configuration can be
257257
// captured.
258-
if len(n.generateConfigPath) > 0 {
258+
if len(n.generateConfigPath) > 0 && len(n.generatedConfigHCL) > 0 && instanceRefreshState != nil {
259259
// Update our return plan
260260
change := &plans.ResourceInstanceChange{
261261
Addr: n.Addr,

0 commit comments

Comments
 (0)