Skip to content

Commit

Permalink
Fix diagnostic & avoid variable override via environment
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Feb 5, 2025
1 parent 1ad35c2 commit 7a52518
Showing 1 changed file with 43 additions and 10 deletions.
53 changes: 43 additions & 10 deletions internal/backend/local/backend_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
}
}
}
Expand Down

0 comments on commit 7a52518

Please sign in to comment.