Summary
preserve_heredocs=False returns a heredoc as quoted-string source. That form
cannot represent a body whose interpolation spans lines: the newlines inside
${...} are expression source, where neither a raw newline nor an escaped one
is valid.
Reproduction
locals {
a = <<EOT
${
1 + 2
}
EOT
}
OpenTofu v1.12.5 evaluates that to "3\n".
Flattening it produces "${\n 1 + 2\n}\n" with the newlines escaped, and
OpenTofu rejects that source with
This character is not used within the language.
Leaving them raw instead gives a quoted string spanning three physical lines,
which it rejects as an invalid multi-line string. Neither form is valid, so the
flattened output cannot be read back by Terraform whichever way it is written.
Why it is filed separately
The escapes-and-spans work in #339 makes the surrounding cases correct -- a
single-line interpolation now survives flattening and restoring intact -- but
it cannot fix this one, because there is no valid quoted spelling to produce.
The options are all judgement calls rather than corrections:
- collapse the interpolation onto one line, which is semantically identical for
most expressions but not for one containing a # comment or a nested
heredoc;
- leave the heredoc unflattened, which contradicts what the option promises;
- or raise, which turns a silent corruption into a loud failure but breaks
callers who are flattening documents that contain one.
Worth deciding deliberately rather than by whichever fix lands first.
This issue, and the investigation behind it, were produced by an AI assistant (Claude) working on behalf of the author. Please review with that provenance in mind.
Summary
preserve_heredocs=Falsereturns a heredoc as quoted-string source. That formcannot represent a body whose interpolation spans lines: the newlines inside
${...}are expression source, where neither a raw newline nor an escaped oneis valid.
Reproduction
OpenTofu v1.12.5 evaluates that to
"3\n".Flattening it produces
"${\n 1 + 2\n}\n"with the newlines escaped, andOpenTofu rejects that source with
Leaving them raw instead gives a quoted string spanning three physical lines,
which it rejects as an invalid multi-line string. Neither form is valid, so the
flattened output cannot be read back by Terraform whichever way it is written.
Why it is filed separately
The escapes-and-spans work in #339 makes the surrounding cases correct -- a
single-line interpolation now survives flattening and restoring intact -- but
it cannot fix this one, because there is no valid quoted spelling to produce.
The options are all judgement calls rather than corrections:
most expressions but not for one containing a
#comment or a nestedheredoc;
callers who are flattening documents that contain one.
Worth deciding deliberately rather than by whichever fix lands first.
This issue, and the investigation behind it, were produced by an AI assistant (Claude) working on behalf of the author. Please review with that provenance in mind.