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

Fix heredoc parsing in parseConfig #97

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion tflint/client/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,35 @@ func parseExpression(src []byte, filename string, start hcl.Pos) (hcl.Expression
panic(fmt.Sprintf("Unexpected file: %s", filename))
}

func hasUnterminatedTemplateString(diags []*hcl.Diagnostic) bool {
for _, diag := range diags {
if diag.Summary == "Unterminated template string" &&
diag.Detail == "No closing marker was found for the string." {
return true
}
}

return false
}

func parseConfig(src []byte, filename string, start hcl.Pos) (*hcl.File, hcl.Diagnostics) {
if strings.HasSuffix(filename, ".tf") {
return hclsyntax.ParseConfig(src, filename, start)
file, diags := hclsyntax.ParseConfig(src, filename, start)
if hasUnterminatedTemplateString(diags) {
// HACK: Add a newline to avoid heredoc parse errors.
// @see https://github.com/hashicorp/hcl/issues/441
src = []byte(string(src) + "\n")
fixedFile, fixedDiags := hclsyntax.ParseConfig(src, filename, start)

// Still has error? Return first result
if hasUnterminatedTemplateString(fixedDiags) {
return file, diags
}

return fixedFile, fixedDiags
}

return file, diags
}

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