hashicorp / hcl2

Former temporary home for experimental new version of HCL
https://github.com/hashicorp/hcl
Mozilla Public License 2.0
373 stars 66 forks source link

Missing newline after comment at EOF causes error #89

Closed radeksimko closed 5 years ago

radeksimko commented 5 years ago

This may be just another mutation of https://github.com/hashicorp/hcl2/issues/18

main.tf

# comment
$ hexdump -C main.tf
00000000  23 20 63 6f 6d 6d 65 6e  74                       |# comment|
00000009
$ terraform validate

Error: Invalid character

  on main.tf line 1:
   1: # comment

This character is not used within the language.

Error: Argument or block definition required

  on main.tf line 1:
   1: # comment

An argument or block definition is required here.
apparentlymart commented 5 years ago

I think what's going on here is that a trailing newline is part of the definition of a single-line comment, and so without that trailing newline the scanner doesn't recognize # comment as being a comment at all. I think fixing this will be a little tricky, though we may be able to do something clever with one of Ragel's advanced end-condition features to get this working.

We may be able to get away with a more straightforward solution in the short term of recognizing this situation better (I assume this can be recognized as a TokenInvalid whose bytes start with either # or //) and, at least, produce a more specialized error explaining that comments must end with newlines. Not the ideal solution by any means, but at least it would guide the user to a working solution for now until we're able to add this exception to the scanner and handle it fully.

apparentlymart commented 5 years ago

This was fixed in d1ed8ee6992b35a7995cde2654c3a5111eecaa50.