Skip to content

Regression in 8.x: Terraform escape sequences are no longer processed (regression of #171) #308

Description

@livingstaccato

Describe the bug

Terraform escape sequences in quoted strings are no longer processed. "line1\nline2" loads as the literal characters line1\nline2 (backslash + n) instead of an actual newline. Same for \t, \r, \", and \\.

This is a regression of #171 ("Terraform escape sequences are not processed when parsing strings"), fixed in 2025-01 and working in 7.2.1/7.3.1.

Software:

  • OS: macOS 15 (arm64)

  • Python version: 3.11.16

  • python-hcl2 versions tested, each in a clean isolated environment:

    • 7.2.1 — correct
    • 7.3.1 — correct
    • 8.0.0rc1 — wrong
    • 8.1.0 — wrong
    • 8.1.2 — wrong

    So this arrived with the v8 rewrite rather than in a later 8.1.x change.

Snippet of HCL2 code causing the unexpected behaviour:

a = "quote \"in\" here"
b = "line1\nline2"
c = "tab\there"
d = "back\\slash"

Expected behavior (7.2.1)

{'a': 'quote "in" here', 'b': 'line1\nline2', 'c': 'tab\there', 'd': 'back\\slash'}

Actual behavior (8.1.2, quotes stripped for clarity)

{'a': 'quote \\"in\\" here', 'b': 'line1\\nline2', 'c': 'tab\\there', 'd': 'back\\\\slash'}

strip_string_quotes=True removes the surrounding quotes but leaves every escape unprocessed.

Root cause

7.x ran DictTransformer.process_escape_sequences() from strip_quotes(). The v8 rewrite (#203) serializes StringRule contents verbatim (hcl2/rules/strings.py), and no equivalent step exists.

Impact

A parsed value no longer matches what Terraform sees for the same source, and the difference is invisible until the string is used. Because the escape is still one backslash short of round-tripping, re-emitting through hcl2.dumps then re-parsing accumulates further backslashes.

Note this interacts with #289: making strip_string_quotes=True the default would surface unprocessed escapes to everyone by default.

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