From 8c4031ef15aff4fcc2dd80874ab976cf004accb9 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Fri, 17 Dec 2021 11:29:12 -0500 Subject: [PATCH] don't persist a nil state from Apply Apply should not return a nil state to be persisted. --- internal/backend/local/backend_apply.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/internal/backend/local/backend_apply.go b/internal/backend/local/backend_apply.go index 897c36e400..42e78d9405 100644 --- a/internal/backend/local/backend_apply.go +++ b/internal/backend/local/backend_apply.go @@ -168,6 +168,14 @@ func (b *Local) opApply( } diags = diags.Append(applyDiags) + // Even on error with an empty state, the state value should not be nil. + // Return early here to prevent corrupting any existing state. + if diags.HasErrors() && applyState == nil { + log.Printf("[ERROR] backend/local: apply returned nil state") + op.ReportResult(runningOp, diags) + return + } + // Store the final state runningOp.State = applyState err := statemgr.WriteAndPersist(opState, applyState)