diff --git a/internal/refactoring/move_statement.go b/internal/refactoring/move_statement.go index baaf9d519..a363602c3 100644 --- a/internal/refactoring/move_statement.go +++ b/internal/refactoring/move_statement.go @@ -124,7 +124,7 @@ func impliedMoveStatements(cfg *configs.Config, prevRunState *states.State, expl fromKey = addrs.NoKey toKey = addrs.IntKey(0) } - default: + case rCfg.Count == nil && rCfg.ForEach == nil: // no repetition at all if riState := rState.Instances[addrs.IntKey(0)]; riState != nil { fromKey = addrs.IntKey(0) toKey = addrs.NoKey diff --git a/internal/refactoring/move_statement_test.go b/internal/refactoring/move_statement_test.go index 93164f94c..9724b1fe1 100644 --- a/internal/refactoring/move_statement_test.go +++ b/internal/refactoring/move_statement_test.go @@ -58,6 +58,16 @@ func TestImpliedMoveStatements(t *testing.T) { instObjState(), providerAddr, ) + s.SetResourceInstanceCurrent( + resourceAddr("now_for_each_formerly_count").Instance(addrs.IntKey(0)), + instObjState(), + providerAddr, + ) + s.SetResourceInstanceCurrent( + resourceAddr("now_for_each_formerly_no_count").Instance(addrs.NoKey), + instObjState(), + providerAddr, + ) // This "ambiguous" resource is representing a rare but possible // situation where we end up having a mixture of different index @@ -113,8 +123,8 @@ func TestImpliedMoveStatements(t *testing.T) { Implied: true, DeclRange: tfdiags.SourceRange{ Filename: "testdata/move-statement-implied/move-statement-implied.tf", - Start: tfdiags.SourcePos{Line: 42, Column: 1, Byte: 709}, - End: tfdiags.SourcePos{Line: 42, Column: 27, Byte: 735}, + Start: tfdiags.SourcePos{Line: 50, Column: 1, Byte: 858}, + End: tfdiags.SourcePos{Line: 50, Column: 27, Byte: 884}, }, }, } diff --git a/internal/refactoring/testdata/move-statement-implied/move-statement-implied.tf b/internal/refactoring/testdata/move-statement-implied/move-statement-implied.tf index 1de3238bd..142ffe702 100644 --- a/internal/refactoring/testdata/move-statement-implied/move-statement-implied.tf +++ b/internal/refactoring/testdata/move-statement-implied/move-statement-implied.tf @@ -39,6 +39,14 @@ moved { to = foo.now_count_explicit[1] } +resource "foo" "now_for_each_formerly_count" { + for_each = { a = 1 } +} + +resource "foo" "now_for_each_formerly_no_count" { + for_each = { a = 1 } +} + resource "foo" "ambiguous" { # this one doesn't have count in the config, but the test should # set it up to have both no-key and zero-key instances in the