From 736752b385ae3b82afa48e72f5d5b0de2e735ed8 Mon Sep 17 00:00:00 2001 From: Daniel Banck Date: Tue, 31 Mar 2026 10:38:36 +0000 Subject: [PATCH] backport of commit 50cd09c7e691118b8348c4e01270d4ab9338a768 --- internal/backend/local/backend_local.go | 30 ++++++++++++++++++++----- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/internal/backend/local/backend_local.go b/internal/backend/local/backend_local.go index 90f63a6f25..0c0269c182 100644 --- a/internal/backend/local/backend_local.go +++ b/internal/backend/local/backend_local.go @@ -271,12 +271,6 @@ func (b *Local) localRunForPlanFile(op *backendrun.Operation, pf *planfile.Reade return nil, nil, diags } - variables, varDiags := backendrun.ParseVariableValues(op.Variables, rootMod.Variables) - diags = diags.Append(varDiags) - if diags.HasErrors() { - return nil, nil, diags - } - // This check is an important complement to the check above: the locked // dependencies in the configuration must match the configuration, and // the locked dependencies in the plan must match the locked dependencies @@ -356,6 +350,30 @@ func (b *Local) localRunForPlanFile(op *backendrun.Operation, pf *planfile.Reade // we need to apply the plan. run.Plan = plan + // All variables that we need to load the configuration should be in the + // plan file. We don't need to look at plan.ApplyTimeVariables, because + // ephemeral values are not supported for constant variables. + variables := terraform.InputValues{} + for name, dyVal := range plan.VariableValues { + val, err := dyVal.Decode(cty.DynamicPseudoType) + if err != nil { + diags = diags.Append(tfdiags.Sourceless( + tfdiags.Error, + "Invalid variable value in plan", + fmt.Sprintf("Invalid value for variable %q recorded in plan file: %s.", name, err), + )) + continue + } + if pvm, ok := plan.VariableMarks[name]; ok { + val = val.MarkWithPaths(pvm) + } + + variables[name] = &terraform.InputValue{ + Value: val, + SourceType: terraform.ValueFromPlan, + } + } + tfCtx, moreDiags := terraform.NewContext(coreOpts) diags = diags.Append(moreDiags) if moreDiags.HasErrors() {