Skip to content

Commit

Permalink
lang: Add Extra to avoid duplicate diagnostic in check blocks (#35944)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko authored Nov 4, 2024
1 parent 7cf7cc8 commit 79a35f3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
11 changes: 10 additions & 1 deletion internal/lang/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/lang/marks"
"github.com/hashicorp/terraform/internal/tfdiags"
"github.com/zclconf/go-cty/cty"
Expand All @@ -21,7 +22,7 @@ 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) (string, tfdiags.Diagnostics) {
func EvalCheckErrorMessage(expr hcl.Expression, hclCtx *hcl.EvalContext, ruleAddr *addrs.CheckRule) (string, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics

val, hclDiags := expr.Value(hclCtx)
Expand Down Expand Up @@ -73,13 +74,21 @@ You can correct this by removing references to sensitive values, or by carefully
}

if _, ephemeral := valMarks[marks.Ephemeral]; ephemeral {
var extra interface{}
if ruleAddr != nil {
extra = &addrs.CheckRuleDiagnosticExtra{
CheckRule: *ruleAddr,
}
}

diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagWarning,
Summary: "Error message refers to ephemeral values",
Detail: `The error expression used to explain this condition refers to ephemeral values, so Terraform will not display the resulting message.
You can correct this by removing references to ephemeral values, or by using the ephemeralasnull() function on the references to not reveal ephemeral data.`,
Subject: expr.Range().Ptr(),
Extra: extra,
})
return "", diags
}
Expand Down
2 changes: 1 addition & 1 deletion internal/moduletest/eval_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (ec *EvalContext) Evaluate() (Status, cty.Value, tfdiags.Diagnostics) {
continue
}

errorMessage, moreDiags := lang.EvalCheckErrorMessage(rule.ErrorMessage, hclCtx)
errorMessage, moreDiags := lang.EvalCheckErrorMessage(rule.ErrorMessage, hclCtx, nil)
ruleDiags = ruleDiags.Append(moreDiags)

runVal, hclDiags := rule.Condition.Value(hclCtx)
Expand Down
2 changes: 1 addition & 1 deletion internal/terraform/eval_conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func validateCheckRule(addr addrs.CheckRule, rule *configs.CheckRule, ctx EvalCo
hclCtx, moreDiags := scope.EvalContext(refs)
diags = diags.Append(moreDiags)

errorMessage, moreDiags := lang.EvalCheckErrorMessage(rule.ErrorMessage, hclCtx)
errorMessage, moreDiags := lang.EvalCheckErrorMessage(rule.ErrorMessage, hclCtx, &addr)
diags = diags.Append(moreDiags)

return errorMessage, hclCtx, diags
Expand Down

0 comments on commit 79a35f3

Please sign in to comment.