diff --git a/CHANGELOG.md b/CHANGELOG.md index 61dc143a..798710f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## \[Unreleased\] -- Nothing yet. +### Fixed + +- A heredoc whose closing marker carries trailing whitespace parses. The spec puts the delimiter "alone on its own line" and Terraform's scanner ends the heredoc at a line holding the word and nothing else that matters, trailing spaces and tabs included; `HEREDOC_TEMPLATE` required the newline to follow the word immediately, so `EOF ` was not a marker at all -- the heredoc ran on, swallowed the rest of the file, and the parse failed with an error pointing somewhere else. Trailing whitespace is invisible and survives copy-paste, so a file that has been running through Terraform for months could fail here. OpenTofu evaluates `<[a-zA-Z][a-zA-Z0-9._-]*)\r?\n(?:(?:.|\n)*?\r?\n)??\s*(?P=heredoc)\r?\n/ -HEREDOC_TEMPLATE_TRIM : /<<-(?P[a-zA-Z][a-zA-Z0-9._-]*)\r?\n(?:(?:.|\n)*?\r?\n)??\s*(?P=heredoc_trim)\r?\n/ +HEREDOC_TEMPLATE : /<<(?P[a-zA-Z][a-zA-Z0-9._-]*)\r?\n(?:(?:.|\n)*?\r?\n)??\s*(?P=heredoc)[ \t]*\r?\n/ +HEREDOC_TEMPLATE_TRIM : /<<-(?P[a-zA-Z][a-zA-Z0-9._-]*)\r?\n(?:(?:.|\n)*?\r?\n)??\s*(?P=heredoc_trim)[ \t]*\r?\n/ // Ignore whitespace (but not newlines, as they're significant in HCL). // \r is ignored too so CRLF line endings (\r\n) parse the same as LF: the diff --git a/test/unit/test_heredoc_marker_whitespace.py b/test/unit/test_heredoc_marker_whitespace.py new file mode 100644 index 00000000..94029734 --- /dev/null +++ b/test/unit/test_heredoc_marker_whitespace.py @@ -0,0 +1,67 @@ +# pylint: disable=C0103,C0114,C0115,C0116 +r"""A closing marker may carry trailing whitespace (GH #343). + +The spec puts the delimiter "alone on its own line", and Terraform's scanner +ends the heredoc at a line holding the word and nothing else that matters -- +trailing spaces and tabs included. `HEREDOC_TEMPLATE` required the newline to +follow the word immediately, so `EOF ` was not a marker: the heredoc ran on, +swallowed the rest of the file, and the parse failed with an error pointing +somewhere else entirely. + +Trailing whitespace is invisible, survives copy-paste, and is left behind by +editors that do not trim it, so a file someone has been running through +Terraform for months could fail here. + +Checked against OpenTofu v1.12.5: `<