Skip to content

Commit 8d8dc04

Browse files
committed
more safety
1 parent 3c96821 commit 8d8dc04

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

plan.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,15 @@ func planJSONHook(dfs fs.FS, input Input) (func(ctx *tfcontext.Context, blocks t
8080

8181
// priorPlanModule returns the state data of the module a given block is in.
8282
func priorPlanModule(plan *tfjson.Plan, block *terraform.Block) *tfjson.StateModule {
83+
if plan.PriorState == nil || plan.PriorState.Values == nil {
84+
return nil // No root module available in the plan, nothing to do
85+
}
86+
87+
rootModule := plan.PriorState.Values.RootModule
88+
8389
if !block.InModule() {
8490
// If the block is not in a module, then we can just return the root module.
85-
if plan.PriorState != nil && plan.PriorState.Values != nil {
86-
return plan.PriorState.Values.RootModule
87-
}
88-
return nil
91+
return rootModule
8992
}
9093

9194
var modPath []string
@@ -98,9 +101,12 @@ func priorPlanModule(plan *tfjson.Plan, block *terraform.Block) *tfjson.StateMod
98101
}
99102
}
100103

101-
current := plan.PriorState.Values.RootModule
104+
current := rootModule
102105
for i := range modPath {
103106
idx := slices.IndexFunc(current.ChildModules, func(m *tfjson.StateModule) bool {
107+
if m == nil {
108+
return false // Avoid nil pointer dereference
109+
}
104110
return m.Address == strings.Join(modPath[:i+1], ".")
105111
})
106112
if idx == -1 {

0 commit comments

Comments
 (0)