Skip to content

heredocs_to_strings=True emits a multi-line quoted string, which is not valid HCL #337

Description

@livingstaccato

Summary

The option converts a heredoc into a quoted string, but writes the heredoc's
source -- markers and all -- inside the quotes, across as many physical lines
as the original occupied. A quoted template cannot span lines, so the result is
not readable by Terraform, and reading it back with this library gives the
marker text rather than the value.

Reproduction

import hcl2
from hcl2.deserializer import DeserializerOptions

hcl2.dumps(
    hcl2.loads("a = <<EOT\nhello\nEOT\n"),
    deserializer_options=DeserializerOptions(heredocs_to_strings=True),
)
a = "<<EOT
hello
EOT"

OpenTofu rejects that with "Invalid multi-line string / Quoted strings may not
be split over multiple lines", although the input file evaluates to "hello\n".
Reading it back here yields '<<EOT\nhello\nEOT', so the option produces
neither a valid file nor the right value.

The field is DeserializerOptions.heredocs_to_strings
(hcl2/deserializer.py:73 at v8.1.3), read at :171 and :176, and it is
exposed on the shipped jsontohcl2 console script as --heredocs-to-strings
(cli/json_to_hcl.py:176, confirmed in jsontohcl2 --help).

Suggested shape of the fix

Convert the heredoc's value and escape it for a quoted string -- hello\n
becomes "hello\n" with the newline written as an escape -- rather than
quoting the source text.


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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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