Skip to content

hcldec: Improved context for expression diagnostics#329

Merged
apparentlymart merged 1 commit intohcl2from
f-hcldec-error-context
Dec 11, 2019
Merged

hcldec: Improved context for expression diagnostics#329
apparentlymart merged 1 commit intohcl2from
f-hcldec-error-context

Conversation

@apparentlymart
Copy link
Copy Markdown
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
Copy Markdown
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