Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Check rule error message expressions #30613

Merged
merged 4 commits into from Mar 7, 2022

Conversation

alisdair
Copy link
Member

@alisdair alisdair commented Mar 3, 2022

Error messages for preconditions, postconditions, and custom variable validations have until now been string literals. This PR changes this to treat the field as an HCL expression, which must evaluate to a string. Most commonly this will either be a string literal or a template expression.

When the check rule condition is evaluated, we also evaluate the error message. This means that the error message should always evaluate to a string value, even if the condition passes. If it does not, this will result in an error diagnostic.

If the condition fails, and the error message also fails to evaluate, we fall back to a default error message. This means that the check rule failure will still be reported, alongside diagnostics explaining why the custom error message failed to render.

As part of this change, we also necessarily remove the heuristic about the error message format. This guidance can be re-added in future as part of a configuration hint system.

Fixes #24407. Fixes #24160.

Copy link
Member

@apparentlymart apparentlymart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great to me!

My inline feedback is all small things that needn't block merging. Most of them are subjective things, so feel free to ignore or take a different direction if you disagree with what I'm suggesting.

internal/terraform/eval_conditions.go Outdated Show resolved Hide resolved
internal/terraform/eval_conditions.go Outdated Show resolved Hide resolved
internal/terraform/eval_variable.go Outdated Show resolved Hide resolved
internal/terraform/eval_variable.go Outdated Show resolved Hide resolved
internal/terraform/eval_variable.go Show resolved Hide resolved
@joe-a-t
Copy link

joe-a-t commented Mar 4, 2022

Resolves #24407.
Resolves #24160.

Error messages for preconditions, postconditions, and custom variable
validations have until now been string literals. This commit changes
this to treat the field as an HCL expression, which must evaluate to a
string. Most commonly this will either be a string literal or a template
expression.

When the check rule condition is evaluated, we also evaluate the error
message. This means that the error message should always evaluate to a
string value, even if the condition passes. If it does not, this will
result in an error diagnostic.

If the condition fails, and the error message also fails to evaluate, we
fall back to a default error message. This means that the check rule
failure will still be reported, alongside diagnostics explaining why the
custom error message failed to render.

As part of this change, we also necessarily remove the heuristic about
the error message format. This guidance can be readded in future as part
of a configuration hint system.
During the validation walk, we attempt to proactively evaluate check
rule condition and error message expressions. This will help catch some
errors as early as possible.

At present, resource values in the validation walk are of dynamic type.
This means that any references to resources will cause validation to be
delayed, rather than presenting useful errors. Validation may still
catch other errors, and any future changes which cause better type
propagation will result in better validation too.
Custom variable validations specified using JSON syntax would always
parse error messages as string literals, even if they included template
expressions. We need to be as backwards compatible with this behaviour
as possible, which results in this complex fallback logic. More detail
about this in the extensive code comments.
@alisdair alisdair force-pushed the alisdair/check-rule-error-message-expressions branch from b848f79 to f21d0e8 Compare March 4, 2022 20:42
@alisdair alisdair merged commit 32f9fa9 into main Mar 7, 2022
@alisdair alisdair deleted the alisdair/check-rule-error-message-expressions branch March 7, 2022 17:10
@github-actions
Copy link

github-actions bot commented Mar 7, 2022

Reminder for the merging maintainer: if this is a user-visible change, please update the changelog on the appropriate release branch.

@github-actions
Copy link

github-actions bot commented Apr 7, 2022

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
3 participants