Skip to content

Heredocs should interpret backslash character literally #262

@DaveCTurner

Description

@DaveCTurner

Describe the bug

https://developer.hashicorp.com/terraform/language/expressions/strings#escape-sequences-1 says

Backslash sequences are not interpreted as escapes in a heredoc string expression. Instead, the backslash character is interpreted literally.

However, python-hcl2 appears to process backslash sequences in heredocs as if they were in a literal string.

Software:

  • OS: Mac OS Tahoe 26.3 (25D125)
  • Python version: Python 3.13.7
  • python-hcl2 version: main (currently a8e5ac0)

Snippet of HCL2 code causing the unexpected behaviour:

block "example" {
  value = <<-EOT
value with "quoted" word and \"escaped\" quote
and literal \n newline on line two
EOT
}

Expected behavior

{
  "block": [
    {
      "example": {
        "value": "value with \"quoted\" word and \\\"escaped\\\" quote\nand literal \\n newline on line two"
      }
    }
  ]
}

Actual behavior

{
  "block": [
    {
      "example": {
        "value": "value with \"quoted\" word and \"escaped\" quote\nand literal \n newline on line two"
      }
    }
  ]
}

Metadata

Metadata

Labels

available-in-prereleasefixed in the latest pre-release version. Install with: pip install python-hcl2 --pre --upgradebugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions