Skip to content

Commit 980bf43

Browse files
authored
Merge pull request #31990 from hashicorp/jbardin/orphan-noop
core: A NoOp orphan change has nothing to apply
2 parents 73c3994 + 3779dbc commit 980bf43

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

internal/terraform/context_apply2_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,3 +1442,46 @@ resource "test_object" "x" {
14421442
t.Fatalf("apply: %s", diags.Err())
14431443
}
14441444
}
1445+
1446+
func TestContext2Apply_missingOrphanedResource(t *testing.T) {
1447+
m := testModuleInline(t, map[string]string{
1448+
"main.tf": `
1449+
# changed resource address to create a new object
1450+
resource "test_object" "y" {
1451+
test_string = "y"
1452+
}
1453+
`,
1454+
})
1455+
1456+
p := simpleMockProvider()
1457+
1458+
// report the prior value is missing
1459+
p.ReadResourceFn = func(req providers.ReadResourceRequest) (resp providers.ReadResourceResponse) {
1460+
resp.NewState = cty.NullVal(req.PriorState.Type())
1461+
return resp
1462+
}
1463+
1464+
state := states.NewState()
1465+
root := state.EnsureModule(addrs.RootModuleInstance)
1466+
root.SetResourceInstanceCurrent(
1467+
mustResourceInstanceAddr("test_object.x").Resource,
1468+
&states.ResourceInstanceObjectSrc{
1469+
Status: states.ObjectReady,
1470+
AttrsJSON: []byte(`{"test_string":"x"}`),
1471+
},
1472+
mustProviderConfig(`provider["registry.terraform.io/hashicorp/test"]`),
1473+
)
1474+
1475+
ctx := testContext2(t, &ContextOpts{
1476+
Providers: map[addrs.Provider]providers.Factory{
1477+
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
1478+
},
1479+
})
1480+
1481+
opts := SimplePlanOpts(plans.NormalMode, nil)
1482+
plan, diags := ctx.Plan(m, state, opts)
1483+
assertNoErrors(t, diags)
1484+
1485+
_, diags = ctx.Apply(plan, m)
1486+
assertNoErrors(t, diags)
1487+
}

internal/terraform/node_resource_apply_instance.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,16 @@ func (n *NodeApplyableResourceInstance) Execute(ctx EvalContext, op walkOperatio
113113
addr := n.ResourceInstanceAddr()
114114

115115
if n.Config == nil {
116-
// This should not be possible, but we've got here in at least one
117-
// case as discussed in the following issue:
118-
// https://github.com/hashicorp/terraform/issues/21258
119-
// To avoid an outright crash here, we'll instead return an explicit
120-
// error.
116+
// If there is no config, and there is no change, then we have nothing
117+
// to do and the change was left in the plan for informational
118+
// purposes only.
119+
changes := ctx.Changes()
120+
csrc := changes.GetResourceInstanceChange(n.ResourceInstanceAddr(), states.CurrentGen)
121+
if csrc == nil || csrc.Action == plans.NoOp {
122+
log.Printf("[DEBUG] NodeApplyableResourceInstance: No config or planned change recorded for %s", n.Addr)
123+
return nil
124+
}
125+
121126
diags = diags.Append(tfdiags.Sourceless(
122127
tfdiags.Error,
123128
"Resource node has no configuration attached",

0 commit comments

Comments
 (0)