Skip to content

Commit eae246c

Browse files
committed
normalize empty CheckResults fields in stateV4
Ensure that empty check results are normalized in state serialization to prevent unexpected state changes from being written. Because there is no consistent empty, null and omit_empty usage for state structs, there's no good way to create a test which will fail for future additions.
1 parent fa4c652 commit eae246c

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

internal/states/statefile/version4.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,7 @@ func writeStateV4(file *File, w io.Writer) tfdiags.Diagnostics {
414414
}
415415
}
416416

417-
if file.State.CheckResults != nil {
418-
sV4.CheckResults = encodeCheckResultsV4(file.State.CheckResults)
419-
}
417+
sV4.CheckResults = encodeCheckResultsV4(file.State.CheckResults)
420418

421419
sV4.normalize()
422420

@@ -576,6 +574,11 @@ func decodeCheckResultsV4(in []checkResultsV4) (*states.CheckResults, tfdiags.Di
576574
}
577575

578576
func encodeCheckResultsV4(in *states.CheckResults) []checkResultsV4 {
577+
// normalize empty and nil sets in the serialized state
578+
if in == nil || in.ConfigResults.Len() == 0 {
579+
return nil
580+
}
581+
579582
ret := make([]checkResultsV4, 0, in.ConfigResults.Len())
580583

581584
for _, configElem := range in.ConfigResults.Elems {

0 commit comments

Comments
 (0)