@@ -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+ }
0 commit comments