Skip to content

Commit e73ac99

Browse files
committed
Fix diagnostic & avoid variable override via environment
1 parent 6ab7209 commit e73ac99

File tree

1 file changed

+43
-10
lines changed

1 file changed

+43
-10
lines changed

internal/backend/local/backend_apply.go

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -340,16 +340,49 @@ func (b *Local) opApply(
340340
})
341341
} else {
342342
// The user can't override the planned variables, so we
343-
// error when possible to avoid confusion. If the parsed
344-
// variables comes from an auto-file however, it's not input
345-
// directly by the user so we have to ignore it.
346-
if parsedVar.Value.Equals(plannedVar).False() && parsedVar.SourceType != terraform.ValueFromAutoFile {
347-
diags = diags.Append(&hcl.Diagnostic{
348-
Severity: hcl.DiagError,
349-
Summary: "Can't change variable when applying a saved plan",
350-
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()),
351-
Subject: rng,
352-
})
343+
// error when possible to avoid confusion.
344+
if parsedVar.Value.Equals(plannedVar).False() {
345+
switch parsedVar.SourceType {
346+
case terraform.ValueFromAutoFile:
347+
// If the parsed variables comes from an auto-file,
348+
// it's not input directly by the user so we have to ignore it.
349+
continue
350+
case terraform.ValueFromEnvVar:
351+
diags = diags.Append(&hcl.Diagnostic{
352+
Severity: hcl.DiagWarning,
353+
Summary: "Ignoring variable when applying a saved plan",
354+
Detail: fmt.Sprintf("The variable %s cannot be overriden when applying a saved plan file, "+
355+
"because a saved plan includes the variable values that were set when it was created. "+
356+
"The saved plan specifies %s as the value whereas during apply the value %s was %s. "+
357+
"To declare an ephemeral variable which is not saved in the plan file, use ephemeral = true.",
358+
varName, tfdiags.CompactValueStr(plannedVar), tfdiags.CompactValueStr(parsedVar.Value),
359+
parsedVar.SourceType.DiagnosticLabel()),
360+
Subject: rng,
361+
})
362+
case terraform.ValueFromCLIArg, terraform.ValueFromNamedFile:
363+
diags = diags.Append(&hcl.Diagnostic{
364+
Severity: hcl.DiagError,
365+
Summary: "Can't change variable when applying a saved plan",
366+
Detail: fmt.Sprintf("The variable %s cannot be set using the -var and -var-file options when "+
367+
"applying a saved plan file, because a saved plan includes the variable values that were "+
368+
"set when it was created. The saved plan specifies %s as the value whereas during apply "+
369+
"the value %s was %s. To declare an ephemeral variable which is not saved in the plan "+
370+
"file, use ephemeral = true.",
371+
varName, tfdiags.CompactValueStr(plannedVar), tfdiags.CompactValueStr(parsedVar.Value),
372+
parsedVar.SourceType.DiagnosticLabel()),
373+
Subject: rng,
374+
})
375+
default:
376+
// Other SourceTypes should never reach this point because
377+
// - ValueFromConfig - supplied plan already contains the original configuration
378+
// - ValueFromInput - we disable prompt when plan file is supplied
379+
// - ValueFromCaller - only used in tests
380+
panic(fmt.Sprintf("Attempted to change variable %s when applying a saved plan. "+
381+
"The saved plan specifies %s as the value whereas during apply the value %s was %s. "+
382+
"This is a bug in Terraform, please report it.",
383+
varName, tfdiags.CompactValueStr(plannedVar), tfdiags.CompactValueStr(parsedVar.Value),
384+
parsedVar.SourceType.DiagnosticLabel()))
385+
}
353386
}
354387
}
355388
}

0 commit comments

Comments
 (0)