-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Incorrect plan when using replace_triggered_by with -replace #37831
Copy link
Copy link
Closed
Labels
bugconfirmeda Terraform Core team member has reproduced this issuea Terraform Core team member has reproduced this issuecore
Description
Terraform Version
Terraform v1.13.4
on linux_amd64Terraform Configuration Files
variable "t" {
type = string
}
resource "terraform_data" "t" {
count = 100
input = var.t
}
resource "terraform_data" "r" {
count = 100
input = count.index
lifecycle {
replace_triggered_by = [terraform_data.t[count.index]]
}
}Debug Output
Expected Behavior
Plan: 100 to add, 100 to change, 100 to destroy.
Actual Behavior
Plan: 16 to add, 97 to change, 16 to destroy.
The numbers vary every time.
Steps to Reproduce
terraform initterraform apply -var t=aterraform plan -var t=b -replace 'terraform_data.r[0]' -replace 'terraform_data.r[1]' -replace 'terraform_data.r[2]'
Additional Context
b.ForceReplace slice obtained from command line arguments is being passed by reference to graph nodes: https://github.com/hashicorp/terraform/blob/v1.13.4/internal/terraform/graph_builder_plan.go#L308
The number of -replace arguments matters. If len(b.ForceReplace) == cap(b.ForceReplace), the bug doesn't reproduce.
It doesn't matter which addresses to use for -replace. E.g. you can replace 3rd action with:
terraform plan -var t=b -replace a.a -replace a.b -replace a.cterraform plan -var t=b -replace a.a -replace a.a -replace a.a
The same slice is being concurrently modified: https://github.com/hashicorp/terraform/blob/v1.13.4/internal/terraform/node_resource_plan_instance.go#L572
I suggest making a copy of the slice at graph_builder_plan.go.
References
No response
Generative AI / LLM assisted development?
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugconfirmeda Terraform Core team member has reproduced this issuea Terraform Core team member has reproduced this issuecore