Skip to content

Commit 7f3699d

Browse files
committed
Fix diagnostic & avoid variable override via environment
1 parent 1ad35c2 commit 7f3699d

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

internal/backend/local/backend_apply.go

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -340,16 +340,36 @@ 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.ValueFromConfig:
351+
// This should never happen since supplied plan
352+
// already contains the original configuration
353+
panic("TODO")
354+
case terraform.ValueFromInput:
355+
// This should never happen since we disable prompt
356+
// when plan file is supplied
357+
panic("TODO")
358+
case terraform.ValueFromCaller:
359+
// This should only happen in tests
360+
panic("TODO")
361+
case terraform.ValueFromCLIArg, terraform.ValueFromNamedFile, terraform.ValueFromEnvVar:
362+
diags = diags.Append(&hcl.Diagnostic{
363+
Severity: hcl.DiagError,
364+
Summary: "Can't change variable when applying a saved plan",
365+
Detail: fmt.Sprintf("The variable %s cannot be overriden when applying a saved plan file, "+
366+
"because a saved plan includes the variable values that were set when it was created. "+
367+
"The saved plan specifies %s as the value whereas during apply the value %s was %s. "+
368+
"To declare an ephemeral variable which is not saved in the plan file, use ephemeral = true.",
369+
varName, tfdiags.CompactValueStr(plannedVar), tfdiags.CompactValueStr(parsedVar.Value), parsedVar.SourceType.DiagnosticLabel()),
370+
Subject: rng,
371+
})
372+
}
353373
}
354374
}
355375
}

0 commit comments

Comments
 (0)