Improve eval_lifecycle_enabled diagnostic context for errors (#3042)

Co-Authored-By: Andrei Ciobanu <andrei.ciobanu@opentofu.org>
Signed-off-by: Diogenes Fernandes <diofeher@gmail.com>
This commit is contained in:
Diogenes Fernandes
2025-09-22 12:25:24 -03:00
committed by Diógenes Fernandes
parent f439baaf01
commit 85ea2a4fa5
2 changed files with 18 additions and 18 deletions

View File

@@ -50,18 +50,6 @@ func EvaluateEnabledExpression(expr hcl.Expression, hclCtxFunc ContextFunc) (boo
})
}
enabledVal, err := convert.Convert(rawEnabledVal, cty.Bool)
if err != nil {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid enabled argument",
Detail: fmt.Sprintf(`The given "enabled" argument value is unsuitable: %s.`, err),
Subject: expr.Range().Ptr(),
Expression: expr,
EvalContext: hclCtx,
})
}
if rawEnabledVal.HasMark(marks.Ephemeral) {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
@@ -95,6 +83,18 @@ func EvaluateEnabledExpression(expr hcl.Expression, hclCtxFunc ContextFunc) (boo
})
}
enabledVal, err := convert.Convert(rawEnabledVal, cty.Bool)
if err != nil {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid enabled argument",
Detail: fmt.Sprintf(`The given "enabled" argument value is unsuitable: %s.`, err),
Subject: expr.Range().Ptr(),
Expression: expr,
EvalContext: hclCtx,
})
}
if diags.HasErrors() {
return false, diags
}

View File

@@ -89,15 +89,15 @@ func TestEvaluateEnabledExpression_errors(t *testing.T) {
[]WantedError{
{
"Invalid enabled argument",
`The given "enabled" argument value is unsuitable: bool required, but have number.`,
`The given "enabled" argument value is unsuitable: the given value is null.`,
},
{
"Invalid enabled argument",
`The given "enabled" argument value is unsuitable: the given value is null.`,
`The given "enabled" argument value is unsuitable: bool required, but have number.`,
},
},
},
"1": {
"positive number": {
cty.NumberIntVal(1),
[]WantedError{
{
@@ -106,7 +106,7 @@ func TestEvaluateEnabledExpression_errors(t *testing.T) {
},
},
},
"negative": {
"negative number": {
cty.NumberIntVal(-1),
[]WantedError{
{
@@ -138,11 +138,11 @@ func TestEvaluateEnabledExpression_errors(t *testing.T) {
[]WantedError{
{
"Invalid enabled argument",
"The given \"enabled\" argument value is unsuitable: bool required, but have number.",
`The given "enabled" argument value is derived from a value that won't be known until the apply phase, so OpenTofu cannot determine whether an instance of this object is declared or not.`,
},
{
"Invalid enabled argument",
`The given "enabled" argument value is derived from a value that won't be known until the apply phase, so OpenTofu cannot determine whether an instance of this object is declared or not.`,
"The given \"enabled\" argument value is unsuitable: bool required, but have number.",
},
},
},