Files
opentf/internal/engine/planning/deferred.go
Martin Atkins 5d3f51c56a engine/planning: cty mark for propagating "deferred" status
We already have a few situations where the evaluation phase can produce
an incomplete placeholder for zero or more resource instances that might
appear in a future plan/apply round, and the plan implementation itself
will introduce some more based on replies from provider requests.

Our goal is to always plan as much as possible with the information we
have, including possibly returning errors for partially-evaluated objects
when we're confident that they could possibly become valid in the presence
of more information, and so in some cases we will end up visiting a
resource instance that would not need to be deferred if considered in
isolation but nonetheless its configuration is depending on an outcome of
an action that was already deferred and so it must therefore also be
deferred.

This currently-unused cty mark will allow us to use dynamic analysis to
track when parts of a resource instance whose action was deferred are used
as part of another resource instance. This is superior to a static analysis
approach because it will allow us to notice situations such as when only
one arm of a conditional relies on a deferred result.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-10-27 10:15:41 -07:00

43 lines
1.5 KiB
Go

// Copyright (c) The OpenTofu Authors
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2023 HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package planning
import (
"github.com/zclconf/go-cty/cty"
)
type ctyMark rune
// deferredMark is a cty Mark we use to represent that a value is derived from
// something whose planning has been deferred to a later plan/apply round
// for any reason.
//
// We use this to recognize when a resource instance wouldn't need to be
// deferred itself except that its configuration is based on something else that
// was previously deferred, and therefore the downstream must be transitively
// deferred to because whatever outcome it's relying on won't actually happen
// in the current plan/apply round.
//
// Using marks for this means that our analysis of deferrals is based on
// dynamic analysis, so and e.g. a conditional expression where only one arm
// is derived from something deferred will only be treated as deferred if
// that arm were selected.
const deferredMark = ctyMark('…')
// deferredVal returns a value equivalent to the given value except that
// the result and anything derived from it would cause [derivedFromDeferredVal]
// to return true.
func deferredVal(v cty.Value) cty.Value {
return v.Mark(deferredMark)
}
// derivedFromDeferredVal returns true if any part of the given value is
// derived from something that was previously produced by a call to
// deferredVal.
func derivedFromDeferredVal(v cty.Value) bool {
return v.HasMarkDeep(deferredMark)
}