Skip to content

A heredoc whose interpolation spans lines cannot be flattened to valid HCL #347

Description

@livingstaccato

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions