move stray check for deprecation to central location

This commit is contained in:
Daniel Schmidt 2026-01-13 17:30:08 +01:00
parent 844bcd8e2c
commit c9cc64a260
3 changed files with 24 additions and 26 deletions

View file

@ -5,7 +5,6 @@ package lang
import (
"fmt"
"strings"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/terraform/internal/addrs"
@ -22,13 +21,13 @@ import (
// It will either return a non-empty message string or it'll return diagnostics
// with either errors or warnings that explain why the given expression isn't
// acceptable.
func EvalCheckErrorMessage(expr hcl.Expression, hclCtx *hcl.EvalContext, ruleAddr *addrs.CheckRule) (string, tfdiags.Diagnostics) {
func EvalCheckErrorMessage(expr hcl.Expression, hclCtx *hcl.EvalContext, ruleAddr *addrs.CheckRule) (cty.Value, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
val, hclDiags := expr.Value(hclCtx)
diags = diags.Append(hclDiags)
if hclDiags.HasErrors() {
return "", diags
return cty.StringVal(""), diags
}
val, err := convert.Convert(val, cty.String)
@ -41,10 +40,10 @@ func EvalCheckErrorMessage(expr hcl.Expression, hclCtx *hcl.EvalContext, ruleAdd
Expression: expr,
EvalContext: hclCtx,
})
return "", diags
return cty.StringVal(""), diags
}
if !val.IsKnown() {
return "", diags
return cty.StringVal(""), diags
}
if val.IsNull() {
diags = diags.Append(&hcl.Diagnostic{
@ -55,10 +54,10 @@ func EvalCheckErrorMessage(expr hcl.Expression, hclCtx *hcl.EvalContext, ruleAdd
Expression: expr,
EvalContext: hclCtx,
})
return "", diags
return cty.StringVal(""), diags
}
val, valMarks := val.Unmark()
_, valMarks := val.Unmark()
if _, sensitive := valMarks[marks.Sensitive]; sensitive {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagWarning,
@ -70,7 +69,7 @@ You can correct this by removing references to sensitive values, or by carefully
Expression: expr,
EvalContext: hclCtx,
})
return "", diags
return cty.StringVal(""), diags
}
if _, ephemeral := valMarks[marks.Ephemeral]; ephemeral {
@ -90,22 +89,8 @@ You can correct this by removing references to ephemeral values, or by using the
Subject: expr.Range().Ptr(),
Extra: extra,
})
return "", diags
return cty.StringVal(""), diags
}
if depMarks := marks.FilterDeprecationMarks(valMarks); len(depMarks) > 0 {
for _, depMark := range depMarks {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagWarning,
Summary: "Deprecated value used",
Detail: depMark.Message,
Subject: expr.Range().Ptr(),
})
}
}
// NOTE: We've discarded any other marks the string might have been carrying,
// aside from the sensitive mark.
return strings.TrimSpace(val.AsString()), diags
return val, diags
}

View file

@ -8,6 +8,7 @@ import (
"fmt"
"log"
"sort"
"strings"
"sync"
"github.com/hashicorp/hcl/v2"
@ -327,6 +328,9 @@ func (ec *EvalContext) EvaluateRun(run *configs.TestRun, module *configs.Module,
errorMessage, moreDiags := lang.EvalCheckErrorMessage(rule.ErrorMessage, hclCtx, nil)
ruleDiags = ruleDiags.Append(moreDiags)
errorMessage, _ = errorMessage.Unmark()
errorMessageStr := strings.TrimSpace(errorMessage.AsString())
runVal, hclDiags := rule.Condition.Value(hclCtx)
ruleDiags = ruleDiags.Append(hclDiags)
@ -390,7 +394,7 @@ func (ec *EvalContext) EvaluateRun(run *configs.TestRun, module *configs.Module,
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Test assertion failed",
Detail: errorMessage,
Detail: errorMessageStr,
Subject: rule.Condition.Range().Ptr(),
Expression: rule.Condition,
EvalContext: hclCtx,

View file

@ -6,6 +6,7 @@ package terraform
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty/cty"
@ -106,7 +107,15 @@ func validateCheckRule(addr addrs.CheckRule, rule *configs.CheckRule, ctx EvalCo
errorMessage, moreDiags := lang.EvalCheckErrorMessage(rule.ErrorMessage, hclCtx, &addr)
diags = diags.Append(moreDiags)
return errorMessage, hclCtx, diags
_, deprecationDiags := ctx.Deprecations().Validate(errorMessage, ctx.Path().Module(), rule.ErrorMessage.Range().Ptr())
diags = diags.Append(deprecationDiags)
// NOTE: We've discarded any other marks the string might have been carrying,
// aside from the sensitive mark.
errorMessage, _ = errorMessage.Unmark()
errorMessageStr := strings.TrimSpace(errorMessage.AsString())
return errorMessageStr, hclCtx, diags
}
func evalCheckRule(addr addrs.CheckRule, rule *configs.CheckRule, ctx EvalContext, keyData instances.RepetitionData, severity hcl.DiagnosticSeverity) (checkResult, tfdiags.Diagnostics) {