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

hcldec: Improved context for expression diagnostics #329

Merged
merged 1 commit into from
Dec 11, 2019

Conversation

apparentlymart
Copy link
Contributor

The two cases where we decode attribute values should include the expression and EvalContext in any diagnostics they generate so that the calling application can give hints about the types and values of variables that are used within the expression.

This also includes some adjustments to the returned source ranges so that both cases are consistent with one another and so that both indicate the entire expression as the Subject and include the attribute name in the Context. Including the whole expression in the range ensures that when there's a problem inside a tuple or object constructor, for example, we'll show the line containing the problem and not just the opening [ or { symbol.


We don't have any specific tests in HCL for the metadata on diagnostics so in order to test this I linked it into Terraform and did the mistake that most commonly causes this error in Terraform usage:

Error: Incorrect attribute value type

  on wrong-type-context-msg.tf line 11, in resource "null_resource" "foo":
  11:   triggers = {
  12:     foo = "woooo"
  13:     bar = local.thingy
  14:   }
    |----------------
    | local.thingy is tuple with 2 elements

Inappropriate value for attribute "triggers": element "bar": string required.

Before this change, the message looked like this instead:

Error: Incorrect attribute value type

  on wrong-type-context-msg.tf line 11, in resource "null_resource" "foo":
  11:   triggers = {

Inappropriate value for attribute "triggers": element "bar": string required.

The two cases where we decode attribute values should include the
expression and EvalContext in any diagnostics they generate so that the
calling application can give hints about the types and values of variables
that are used within the expression.

This also includes some adjustments to the returned source ranges so that
both cases are consistent with one another and so that both indicate
the entire expression as the Subject and include the attribute name in
the Context. Including the whole expression in the range ensures that
when there's a problem inside a tuple or object constructor, for example,
we'll show the line containing the problem and not just the opening [
or { symbol.
Copy link
Contributor

@mildwonkey mildwonkey left a comment

Choose a reason for hiding this comment

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

Looks great!

@apparentlymart apparentlymart merged commit c22ad7a into hcl2 Dec 11, 2019
@apparentlymart apparentlymart deleted the f-hcldec-error-context branch December 11, 2019 15:25
@apparentlymart apparentlymart added the v2 Relates to the v2 line of releases label Dec 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cmd/hcldec enhancement v2 Relates to the v2 line of releases
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants