martinohmann / hcl-rs

HCL parsing and encoding libraries for rust with serde support
Apache License 2.0
119 stars 14 forks source link

parser fails when given a multi-line ternary expression within parenthesis #319

Closed slyphon closed 3 months ago

slyphon commented 6 months ago

Our terraform codebase contains several declarartions of the form:

resource "foo" "inline" {
  role = var.role_name
  name = var.policy_name
  policy = (var.policy != "" ?
    var.policy :
    templatefile("path/to/template", {
      foo = bar
    })
  )
}

This is parsed correctly with the terraform binaries and with go-hcl, but hcl-rs will return an error such as:

ignored parsing error:   --> HCL parse error in line 10, column 36
   |
10 |   policy = (var.policy != "" ?
   |                               ^---
   |
   = invalid expression; expected `"`, `[`, `{`, `-`, `!`, `(`, `_`, `<`, letter or digit

I forked this repo and created a failing test case on a branch. It seems that in deserialize_any some extra context would need to be passed along in the case of parenthesis, as it's legal to have multiline expressions within.

Please let me know if there's anything I can do to help or any extra information you need.

martinohmann commented 3 months ago

@slyphon Thanks for the bug report and the reproducer. I'm sorry you had to wait quite a bit for a response.

I'm going to look into that. You're probably right that some extra context around parenthesis is needed. deserialize_any is not the place though since the error comes directly from the parser and not from serde.

martinohmann commented 3 months ago

@slyphon I just published hcl-rs v0.16.8 and hcl-edit v0.7.6 which contain a fix for that. Let me know if you find another case that the parser does not handle as expected :).