@@ -77,9 +77,35 @@ func parseExpression(src []byte, filename string, start hcl.Pos) (hcl.Expression
77
77
panic (fmt .Sprintf ("Unexpected file: %s" , filename ))
78
78
}
79
79
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
+
80
91
func parseConfig (src []byte , filename string , start hcl.Pos ) (* hcl.File , hcl.Diagnostics ) {
81
92
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
83
109
}
84
110
85
111
if strings .HasSuffix (filename , ".tf.json" ) {
0 commit comments