Repository navigation
Description
Describe the bug
A heredoc whose delimiter is a single character fails to parse. The HEREDOC_TEMPLATE terminal requires two or more characters, so <<E is rejected while <<EO is accepted.
The HCL specification defines the heredoc delimiter as an Identifier:
heredocTemplate = (
("<<" | "<<-") Identifier Newline
(content as defined in prose above)
Identifier Newline
);
Identifier = ID_Start (ID_Continue | '-')*;The * permits a single character, so a one-character delimiter is valid HCL.
Software:
- python-hcl2:
main(1432aea), and identical behaviour in 7.x — see below - Python 3.9, lark 1.3.1
Snippet of HCL2 code causing the unexpected behaviour:
a = <<E
x
EAlso affects the trimmed form:
a = <<-E
x
EExpected behavior
Parses like any other heredoc, i.e. equivalent to a = <<EO\nx\nEO\n, which works today.
Actual behavior
lark.exceptions.UnexpectedToken: Unexpected token Token('STRING_CHARS', '<<E\nx\nE\n\n') at line 1, column 5.
Delimiter length is the only variable:
| Source | Result |
|---|---|
a = <<E\nx\nE\n |
raises |
a = <<-E\n x\n E\n |
raises |
a = <<EO\nx\nEO\n |
{'a': '"<<EO\nx\nEO"'} |
a = <<EOF\nx\nEOF\n |
{'a': '"<<EOF\nx\nEOF"'} |
Root cause
hcl2/hcl2.lark — the delimiter group is [a-zA-Z][a-zA-Z0-9._-]+, where the + requires at least one character after the first:
HEREDOC_TEMPLATE : /<<(?P<heredoc>[a-zA-Z][a-zA-Z0-9._-]+)\n(?:.|\n)*?\n\s*(?P=heredoc)\n/
HEREDOC_TEMPLATE_TRIM : /<<-(?P<heredoc_trim>[a-zA-Z][a-zA-Z0-9._-]+)\n(?:.|\n)*?\n\s*(?P=heredoc_trim)\n/
Changing + to * would admit single-character delimiters. I have not tested whether that introduces any lexing ambiguity, so it is a suggestion rather than a verified fix.
Not a regression. The same [a-zA-Z][a-zA-Z0-9._-]+ pattern is present pre-8.x (git show 21b7cf8^:hcl2/hcl2.lark), so this is a long-standing limitation rather than something the v8 rewrite introduced. No single-character delimiter appears anywhere in the current test suite.
Impact
Low in practice — real configurations overwhelmingly use EOF, EOT, JSON, POLICY and similar. It matters for generated or minified HCL, and for anything round-tripping documents produced by other HCL implementations. The failure is loud, so there is no silent data loss.
Workaround
Use a delimiter of two or more characters.
Found while reviewing #309 (empty heredoc), which touches the same two terminals. Filing separately because it is a distinct, pre-existing limitation and out of scope for the fix in #312.
Activity
fix: parse heredocs with a single-character delimiter (#314) (#323)
kkozik-amplify commentedon Aug 26, 2026
Released in v8.1.3, now on PyPI —
pip install python-hcl2==8.1.3. Fixed by #323.