Skip to content

Commit

Permalink
hclsyntax: Hint about possible unescaped ${ for other languages
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
apparentlymart committed Apr 20, 2021
1 parent 72862ac commit b0e5665
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions hclsyntax/parser_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,13 +413,24 @@ Token:
close := p.Peek()
if close.Type != TokenTemplateSeqEnd {
if !p.recovery {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Extra characters after interpolation expression",
Detail: "Expected a closing brace to end the interpolation expression, but found extra characters.",
Subject: &close.Range,
Context: hcl.RangeBetween(startRange, close.Range).Ptr(),
})
switch close.Type {
case TokenColon:
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Extra characters after interpolation expression",
Detail: "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 \"${\".",
Subject: &close.Range,
Context: hcl.RangeBetween(startRange, close.Range).Ptr(),
})
default:
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Extra characters after interpolation expression",
Detail: "Expected a closing brace to end the interpolation expression, but found extra characters.\n\nThis 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 \"${\".",
Subject: &close.Range,
Context: hcl.RangeBetween(startRange, close.Range).Ptr(),
})
}
}
p.recover(TokenTemplateSeqEnd)
} else {
Expand Down

0 comments on commit b0e5665

Please sign in to comment.