Skip to content

Commit 23731c3

Browse files
authored
Fix heredoc parsing in parseConfig (#97)
* Fix heredoc parsing in parseConfig * Add newline to body only on well-known error
1 parent 530c035 commit 23731c3

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

tflint/client/decode.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,35 @@ func parseExpression(src []byte, filename string, start hcl.Pos) (hcl.Expression
7777
panic(fmt.Sprintf("Unexpected file: %s", filename))
7878
}
7979

80+
func hasUnterminatedTemplateString(diags []*hcl.Diagnostic) bool {
81+
for _, diag := range diags {
82+
if diag.Summary == "Unterminated template string" &&
83+
diag.Detail == "No closing marker was found for the string." {
84+
return true
85+
}
86+
}
87+
88+
return false
89+
}
90+
8091
func parseConfig(src []byte, filename string, start hcl.Pos) (*hcl.File, hcl.Diagnostics) {
8192
if strings.HasSuffix(filename, ".tf") {
82-
return hclsyntax.ParseConfig(src, filename, start)
93+
file, diags := hclsyntax.ParseConfig(src, filename, start)
94+
if hasUnterminatedTemplateString(diags) {
95+
// HACK: Add a newline to avoid heredoc parse errors.
96+
// @see https://github.com/hashicorp/hcl/issues/441
97+
src = []byte(string(src) + "\n")
98+
fixedFile, fixedDiags := hclsyntax.ParseConfig(src, filename, start)
99+
100+
// Still has error? Return first result
101+
if hasUnterminatedTemplateString(fixedDiags) {
102+
return file, diags
103+
}
104+
105+
return fixedFile, fixedDiags
106+
}
107+
108+
return file, diags
83109
}
84110

85111
if strings.HasSuffix(filename, ".tf.json") {

0 commit comments

Comments
 (0)