Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,15 @@ func planJSONHook(dfs fs.FS, input Input) (func(ctx *tfcontext.Context, blocks t

// priorPlanModule returns the state data of the module a given block is in.
func priorPlanModule(plan *tfjson.Plan, block *terraform.Block) *tfjson.StateModule {
if plan.PriorState == nil || plan.PriorState.Values == nil {
return nil // No root module available in the plan, nothing to do
}

rootModule := plan.PriorState.Values.RootModule

if !block.InModule() {
return plan.PriorState.Values.RootModule
// If the block is not in a module, then we can just return the root module.
return rootModule
}

var modPath []string
Expand All @@ -94,9 +101,12 @@ func priorPlanModule(plan *tfjson.Plan, block *terraform.Block) *tfjson.StateMod
}
}

current := plan.PriorState.Values.RootModule
current := rootModule
for i := range modPath {
idx := slices.IndexFunc(current.ChildModules, func(m *tfjson.StateModule) bool {
if m == nil {
return false
}
return m.Address == strings.Join(modPath[:i+1], ".")
})
if idx == -1 {
Expand Down
Loading