Skip to content

import: Fix expansion panic with matching child module + root module import targets - #38993

Open
austinvalle wants to merge 1 commit into
mainfrom
av/child-module-stuffz
Open

import: Fix expansion panic with matching child module + root module import targets#38993
austinvalle wants to merge 1 commit into
mainfrom
av/child-module-stuffz

Conversation

@austinvalle

@austinvalle austinvalle commented Aug 7, 2026

Copy link
Copy Markdown
Member

No open bug for this as child module imports aren't released yet! I just ran into a panic when messing around with child modules. I've included what I believe to be the narrow fix. The child module import target adds a resource node in the root module (which eventually causes a panic when they both attempt to expand).

I accidentally created an invalid configuration attempting to test the error message when running terraform plan -generate-config-out=generated.tf:

# main.tf
module "child" {
  source = "./child"
}
import {
  to = test_object.samename
  id = "test-a"
}

# child/main.tf
import {
  to = test_object.samename
  id = "test-b"
}

This is invalid configuration because we can't generate configuration in child modules, but rather than a validation error I got a panic (the one below is from the test I included in this PR):

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

Terraform crashed! This is always indicative of a bug within Terraform.
Please report the crash with Terraform[[1](https://github.com/hashicorp/terraform/issues)] so that we can fix this.

When reporting bugs, please include your terraform version, the stack trace
shown below, and any additional information which may help replicate the issue.

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

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

panic: expansion already registered for test_object.samename
goroutine 94 [running]:
runtime/debug.Stack()
	/Users/austin.valle/go/pkg/mod/golang.org/[toolchain@v0.0.1-go1.26.4.darwin-arm64](mailto:toolchain@v0.0.1-go1.26.4.darwin-arm64)/src/runtime/debug/stack.go:26 +0x64
github.com/hashicorp/terraform/internal/logging.PanicHandler()
	/Users/austin.valle/code/terraform/internal/logging/panic.go:84 +0x164
panic({0x1079ccbe0?, 0x41b46b67e270?})
	/Users/austin.valle/go/pkg/mod/golang.org/[toolchain@v0.0.1-go1.26.4.darwin-arm64](mailto:toolchain@v0.0.1-go1.26.4.darwin-arm64)/src/runtime/panic.go:860 +0x12c
github.com/hashicorp/terraform/internal/terraform.(*Graph).walk.func1.1()
	/Users/austin.valle/code/terraform/internal/terraform/graph.go:55 +0x304
panic({0x1079ccbe0?, 0x41b46b67e270?})
	/Users/austin.valle/go/pkg/mod/golang.org/[toolchain@v0.0.1-go1.26.4.darwin-arm64](mailto:toolchain@v0.0.1-go1.26.4.darwin-arm64)/src/runtime/panic.go:860 +0x12c
github.com/hashicorp/terraform/internal/instances.(*Expander).setResourceExpansion(0x41b46b836800, {0x0, 0x0, 0x0}, {{}, 0x4d, {0x41b46b6afd80, 0xb}, {0x41b46b6afd78, 0x8}}, ...)
	/Users/austin.valle/code/terraform/internal/instances/expander.go:556 +0x1a8
github.com/hashicorp/terraform/internal/instances.(*Expander).SetResourceSingle(...)
	/Users/austin.valle/code/terraform/internal/instances/expander.go:96
github.com/hashicorp/terraform/internal/terraform.(*NodeAbstractResource).recordResourceData(0x41b46b8f41c0, {0x107fade18, 0x41b46b63c500}, {{}, {0x0, 0x0, 0x0}, {{}, 0x4d, {0x41b46b6afd80, ...}, ...}})
	/Users/austin.valle/code/terraform/internal/terraform/node_resource_abstract.go:532 +0x1e4
github.com/hashicorp/terraform/internal/terraform.(*nodeExpandPlannableResource).expandResourceInstances(0x41b46b648600, {0x107fade18, 0x41b46b310a00}, {{}, {0x0, 0x0, 0x0}, {{}, 0x4d, {0x41b46b6afd80, ...}, ...}}, ...)
	/Users/austin.valle/code/terraform/internal/terraform/node_resource_plan.go:460 +0xb4
github.com/hashicorp/terraform/internal/terraform.(*nodeExpandPlannableResource).dynamicExpand(0x41b46b648600, {0x107fade18, 0x41b46b310a00}, {0x1081bbe00, 0x1, 0x1}, {0x41b46b8bf6b8?})
	/Users/austin.valle/code/terraform/internal/terraform/node_resource_plan.go:414 +0x814
github.com/hashicorp/terraform/internal/terraform.(*nodeExpandPlannableResource).DynamicExpand(0x41b46b648600, {0x107fade18, 0x41b46b310a00})
	/Users/austin.valle/code/terraform/internal/terraform/node_resource_plan.go:139 +0x42c
github.com/hashicorp/terraform/internal/terraform.(*Graph).walk.func1({0x107f53500, 0x41b46b648600})
	/Users/austin.valle/code/terraform/internal/terraform/graph.go:149 +0x708
github.com/hashicorp/terraform/internal/dag.(*Walker).walkVertex(0x41b46b647d60, {0x107f53500, 0x41b46b648600}, 0x41b46b89bc50)
	/Users/austin.valle/code/terraform/internal/dag/walk.go:277 +0x1a4
created by github.com/hashicorp/terraform/internal/dag.(*Walker).Walk in goroutine 49
	/Users/austin.valle/code/terraform/internal/dag/walk.go:207 +0x5b0
FAIL	github.com/hashicorp/terraform/internal/terraform	0.994s
FAIL

Target Release

1.16.x

Rollback Plan

  • If a change needs to be reverted, we will roll out an update to the code within 7 days.

Changes to Security Controls

Are there any changes to security controls (access controls, encryption, logging) in this pull request? If so, explain.

CHANGELOG entry

  • This change is user-facing and I added a changelog entry.
  • This change is not user-facing. (it is user facing, but we don't need a changelog imo)

@austinvalle austinvalle added the no-changelog-needed Add this to your PR if the change does not require a changelog entry label Aug 7, 2026
@austinvalle
austinvalle requested a review from a team as a code owner August 7, 2026 19:13
@austinvalle austinvalle added the 1.16-backport If you add this label to a PR before merging, backport-assistant will open a new PR once merged label Aug 7, 2026
// resource
abstract := &NodeAbstractResource{
Addr: i.Config.ToResource,
Addr: i.AbsToConfigResource,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mildwonkey tagging you hoping you might have more context on whether this was the right fix (or if there might be other places to check for errors like this).

Originally I thought maybe i.Config.ToResource was incorrect and needed to be fixed, but I saw we recently added i.AbsToConfigResource and that made me think that i.Config.ToResource was kept to be relative purposefully 🤔

@austinvalle
austinvalle force-pushed the av/child-module-stuffz branch from 35dba4a to 0312ca7 Compare August 7, 2026 19:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

1.16-backport If you add this label to a PR before merging, backport-assistant will open a new PR once merged no-changelog-needed Add this to your PR if the change does not require a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant