diff --git a/internal/backend/local/backend_apply.go b/internal/backend/local/backend_apply.go index cb8bd0268180..413f92afc693 100644 --- a/internal/backend/local/backend_apply.go +++ b/internal/backend/local/backend_apply.go @@ -340,16 +340,49 @@ func (b *Local) opApply( }) } else { // The user can't override the planned variables, so we - // error when possible to avoid confusion. If the parsed - // variables comes from an auto-file however, it's not input - // directly by the user so we have to ignore it. - if parsedVar.Value.Equals(plannedVar).False() && parsedVar.SourceType != terraform.ValueFromAutoFile { - diags = diags.Append(&hcl.Diagnostic{ - Severity: hcl.DiagError, - Summary: "Can't change variable when applying a saved plan", - Detail: fmt.Sprintf("The variable %s cannot be set using the -var and -var-file options when applying a saved plan file, because a saved plan includes the variable values that were set when it was created. The saved plan specifies %s as the value whereas during apply the value %s was %s. To declare an ephemeral variable which is not saved in the plan file, use ephemeral = true.", varName, tfdiags.CompactValueStr(parsedVar.Value), tfdiags.CompactValueStr(plannedVar), parsedVar.SourceType.DiagnosticLabel()), - Subject: rng, - }) + // error when possible to avoid confusion. + if parsedVar.Value.Equals(plannedVar).False() { + switch parsedVar.SourceType { + case terraform.ValueFromAutoFile: + // If the parsed variables comes from an auto-file, + // it's not input directly by the user so we have to ignore it. + continue + case terraform.ValueFromEnvVar: + diags = diags.Append(&hcl.Diagnostic{ + Severity: hcl.DiagWarning, + Summary: "Ignoring variable when applying a saved plan", + Detail: fmt.Sprintf("The variable %s cannot be overriden when applying a saved plan file, "+ + "because a saved plan includes the variable values that were set when it was created. "+ + "The saved plan specifies %s as the value whereas during apply the value %s was %s. "+ + "To declare an ephemeral variable which is not saved in the plan file, use ephemeral = true.", + varName, tfdiags.CompactValueStr(plannedVar), tfdiags.CompactValueStr(parsedVar.Value), + parsedVar.SourceType.DiagnosticLabel()), + Subject: rng, + }) + case terraform.ValueFromCLIArg, terraform.ValueFromNamedFile: + diags = diags.Append(&hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "Can't change variable when applying a saved plan", + Detail: fmt.Sprintf("The variable %s cannot be set using the -var and -var-file options when "+ + "applying a saved plan file, because a saved plan includes the variable values that were "+ + "set when it was created. The saved plan specifies %s as the value whereas during apply "+ + "the value %s was %s. To declare an ephemeral variable which is not saved in the plan "+ + "file, use ephemeral = true.", + varName, tfdiags.CompactValueStr(plannedVar), tfdiags.CompactValueStr(parsedVar.Value), + parsedVar.SourceType.DiagnosticLabel()), + Subject: rng, + }) + default: + // Other SourceTypes should never reach this point because + // - ValueFromConfig - supplied plan already contains the original configuration + // - ValueFromInput - we disable prompt when plan file is supplied + // - ValueFromCaller - only used in tests + panic(fmt.Sprintf("Attempted to change variable %s when applying a saved plan. "+ + "The saved plan specifies %s as the value whereas during apply the value %s was %s. "+ + "This is a bug in Terraform, please report it.", + varName, tfdiags.CompactValueStr(plannedVar), tfdiags.CompactValueStr(parsedVar.Value), + parsedVar.SourceType.DiagnosticLabel())) + } } } }