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

hclsyntax: Hint about possible unescaped ${ for other languages #462

Merged
merged 1 commit into from
Apr 20, 2021

Conversation

apparentlymart
Copy link
Member

It's a common error to accidentally include a ${ intended to be processed as part of another language after HCL processing, such as a shell script or an AWS IAM policy document.

Exactly what sort of error will appear in that case unfortunately depends on the syntax for that other language, but a lot of them happen to end up in the codepath where we report the "Extra characters after interpolation expression" diagnostic message. For that reason, this extends that error message with an additional hint about escaping.

I've also included a special case for colons because they happen to arise in both Bash and AWS IAM interpolation syntax, albeit with different meanings. The specialized error message doesn't really say anything the generic one doesn't, but it does explicitly identify the colon as being the problem, which I hope will be helpful in thinking about what parts of this are being parsed in which layer.

Because this message is coming from HCL and HCL doesn't typically assume anything about the application where it's being used, the hint message is pretty generic and focuses only on the hint about the escaping syntax, in the hope that this will be hint enough to prompt the user to think about what they are currently working on and realize how to respond to this error.

The existing test cases in HCL typically only test that particular input fails, not what messages are returned when doing so, and so since this is effectively just changing the text of an existing error message it's not covered by any new tests. However, I've included examples of the new error messages below, which I viewed by exercising this codepath using the hcldec command line tool:

Error: Extra characters after interpolation expression

  on bloop.hcl line 1:
   1: foo = "${foo bar}"

Expected a closing brace to end the interpolation expression, but found
extra characters.

This can happen when you include interpolation syntax for another language,
such as shell scripting, but forget to escape the interpolation start token.
If this is an embedded sequence for another language, escape it by starting
with "$${" instead of just "${".
Error: Extra characters after interpolation expression

  on bloop.hcl line 1:
   1: foo = "${aws:whatever}"

Template interpolation doesn't expect a colon at this location. Did you
intend this to be a literal sequence to be processed as part of another
language? If so, you can escape it by starting with "$${" instead of just
"${".

This is a compromise aimed at the problem discussed in #460.

It's a common error to accidentally include a ${ intended to be processed
as part of another language after HCL processing, such as a shell script
or an AWS IAM policy document.

Exactly what sort of error will appear in that case unfortunately depends
on the syntax for that other language, but a lot of them happen to end up
in the codepath where we report the "Extra characters after interpolation
expression" diagnostic message. For that reason, this extends that error
message with an additional hint about escaping, and I've also included
a special case for colons because they happen to arise in both Bash and
AWS IAM interpolation syntax, albeit with different meanings.

Because this message is coming from HCL and HCL doesn't typically assume
anything about the application where it's being used, the hint message
is pretty generic and focuses only on the hint about the escaping syntax,
in the hope that this will be hint enough to prompt the user to think
about what they are currently working on and realize how to respond to
this error.
@apparentlymart apparentlymart added v2 Relates to the v2 line of releases syntax/native labels Apr 15, 2021
@apparentlymart apparentlymart self-assigned this Apr 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
syntax/native v2 Relates to the v2 line of releases
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants