Skip to content

strings_to_heredocs writes escape sequences into the heredoc body, changing the value #329

Open

Description

@livingstaccato

Summary

DeserializerOptions(strings_to_heredocs=True) converts a quoted string into a
heredoc by taking the source text between the quotes and decoding three escapes
from it: \n, \" and \\. Every other escape is copied through as the two
characters it is written with.

That is not a re-spelling of the same value. A quoted template resolves \t,
\r, \uNNNN and \UNNNNNNNN; a heredoc template resolves none of them, which
is what #262 established for the read side. So "a\tb\n" — a tab — is written
as a heredoc whose body is a backslash followed by t, and the document now
means something different.

Reproduction

import hcl2
from hcl2.deserializer import DeserializerOptions
from hcl2.utils import SerializationOptions

heredocs = DeserializerOptions(strings_to_heredocs=True)
value = SerializationOptions(preserve_heredocs=False, strip_string_quotes=True)

out = hcl2.dumps({"x": r'"a\tb\n"'}, deserializer_options=heredocs)
print(out)
print(hcl2.loads(out, serialization_options=value))
x = <<EOF
a\tb

EOF

{'x': 'a\\tb\n'}

☺ and \r behave the same way. (The blank line before the closing marker
is a separate bug, filed as part of #326; it is not what this issue is about.)

Ground truth

Each line is the expression fed to tofu console, and the JSON string it
evaluates to:

"a\tb\n"                 →  "a\tb\n"        the escape resolves: a real tab
<<EOF\na\tb\nEOF\n        →  "a\\tb\n"       it does not: a backslash and a t

"a\u263Ab"                →  "a☺b"
<<EOF\na\u263Ab\nEOF\n    →  "a\\u263Ab\n"

So the input and the output are two different strings for OpenTofu, while the
option presents itself as a change of syntax.

Cause

hcl2/deserializer.py, in _deserialize_string_as_heredoc (8.1.3):

content = re.sub(
    r'\\(n|"|\\)',
    lambda m: "\n" if m.group(1) == "n" else m.group(1),
    inner,
)

Three escapes in, the rest untouched — and untouched is exactly wrong, because
the destination syntax does not interpret them.

Suggested shape of the fix

The package already has the full decoder: hcl2.utils.process_escape_sequences
resolves \n, \r, \t, \", \\, \uNNNN and \UNNNNNNNN in a single
pass. Decoding the string with it before wrapping the body would make the
conversion value-preserving for every escape the grammar accepts.

The alternative, if a heredoc is meant to stay a purely textual transformation,
is to refuse it: leave the value quoted when the source carries an escape other
than \n, \" or \\. Either is defensible; silently changing the value is
not.

Worth deciding alongside it: a body cannot express a $${ or %%{ escape
either, since a heredoc is still a template.


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