Skip to main content
0 reactions

Fixed: OpenTofu Indented Heredoc Not Trimming Spaces After a Strip Marker

Card titled Indented Heredoc Not Trimming Spaces, labelled Fixed OpenTofu, comparing an indented heredoc, where a strip marker removes the line break and the spaces are kept, with a flush-left heredoc that gives clean lines.
Lines after a ~ strip marker kept their spaces. A flush-left heredoc fixes it.

While building a web page with OpenTofu®, I wrote its book list as an indented heredoc with a %{for} directive and strip markers. The list came out with extra spaces at the start of every line after the directive. Here’s what happened, what showed the cause, and the one-line fix.

What I Was Doing

In Part 5 of the OpenTofu Tutorial, the Bookshop page is built from a list of books with a heredoc template. I wrote it indented, like the rest of the file, with <<-EOT, and put a ~ in each directive so the lines wouldn’t be separated by empty ones:

main.tf (excerpt)HCL
locals {
  heading   = "Welcome to the ${var.shop_name}"
  home_page = <<-EOT
    <h1>${local.heading}</h1>
    <p>New this week: ${var.featured_book}.</p>
    <ul>
    %{for book in var.books~}
      <li>${book.title} by ${book.author}</li>
    %{endfor~}
    </ul>
  EOT
}

Then I ran tofu apply.

What I Expected

The OpenTofu docs say an indented heredoc finds the line with the fewest leading spaces and removes that many spaces from every line, and that a strip marker removes the whitespace next to it. So every line should start at the left edge, with the <li> lines two spaces in.

What I Got

The plan showed the page’s new content. The first lines were trimmed, but the <li> lines kept six spaces and </ul> kept four:

Output (the lines that matter)Plain text
      ~ content              = <<-EOT # forces replacement
          - <h1>Welcome to the Riverside Bookshop</h1><p>New this week: A Map of Quiet Places by Tomas Vell.</p>
          + <h1>Welcome to the Riverside Bookshop</h1>
          + <p>New this week: A Map of Quiet Places by Tomas Vell.</p>
          + <ul>
          +       <li>The Paper Lantern Café by Mira Kale</li>
          +       <li>A Map of Quiet Places by Tomas Vell</li>
          +       <li>The Clockmaker's Garden by Mira Kale</li>
          +       <li>Salt and Starlight by Nadia Quill</li>
          +     </ul>
        EOT

The page source in the browser showed the same:

The page source of the Bookshop page in a browser: the ul line starts at the left edge, but the four li lines start six spaces in and the closing ul line four spaces in.
The lines after the for directive kept their spaces.

What I Checked

I wrote the same list twice in a test file, once with the strip markers and once without, and printed both in tofu console:

main.tfHCL
locals {
  books = ["Book A", "Book B"]

  with_strip = <<-EOT
    <ul>
    %{for book in local.books~}
      <li>${book}</li>
    %{endfor~}
    </ul>
  EOT

  without_strip = <<-EOT
    <ul>
    %{for book in local.books}
      <li>${book}</li>
    %{endfor}
    </ul>
  EOT
}
tofu consolePlain text
> local.with_strip
<<EOT
<ul>
      <li>Book A</li>
      <li>Book B</li>
    </ul>

EOT
> local.without_strip
<<EOT
<ul>

  <li>Book A</li>

  <li>Book B</li>

</ul>

EOT
> exit

Without the strip markers, every line was trimmed correctly, but empty lines appeared between them. With them, the lines after each directive kept all their spaces. The strip marker is what stops the trimming.

Why It Happens

HCL, the language OpenTofu’s configuration is written in, defines the indented heredoc in its specification: it looks at the text at the start of each line, finds the smallest indentation, and removes that many spaces from all “line-leading” text (HCL Native Syntax Specification: Template Expressions (opens in a new tab)). A strip marker that removes a line break joins the next line onto the directive’s line. That text no longer starts a line, so the indentation isn’t removed from it.

The Fix

Write the template as a flush-left heredoc, <<EOT, as the OpenTofu docs’ own directive examples do. With nothing to remove, every line keeps exactly the spaces you give it:

main.tf (excerpt)HCL
locals {
  heading   = "Welcome to the ${var.shop_name}"
  home_page = <<EOT
<h1>${local.heading}</h1>
<p>New this week: ${var.featured_book}.</p>
<ul>
%{for book in var.books~}
  <li>${book.title} by ${book.author}</li>
%{endfor~}
</ul>
EOT
}

After the Fix

The next tofu apply showed every line coming out as written:

Output (the lines that matter)Plain text
      ~ content              = <<-EOT # forces replacement
            <h1>Welcome to the Riverside Bookshop</h1>
            <p>New this week: A Map of Quiet Places by Tomas Vell.</p>
            <ul>
          -       <li>The Paper Lantern Café by Mira Kale</li>
          -       <li>A Map of Quiet Places by Tomas Vell</li>
          -       <li>The Clockmaker's Garden by Mira Kale</li>
          -       <li>Salt and Starlight by Nadia Quill</li>
          -     </ul>
          +   <li>The Paper Lantern Café by Mira Kale</li>
          +   <li>A Map of Quiet Places by Tomas Vell</li>
          +   <li>The Clockmaker's Garden by Mira Kale</li>
          +   <li>Salt and Starlight by Nadia Quill</li>
          + </ul>
        EOT
The page source after the fix: every line starts at the left edge except the four li lines, which are indented by two spaces as written.
With a flush-left heredoc, every line comes out as written.

Common Mistakes

MistakeWhat happensDo this instead
Removing the strip markers to get the trimming backEvery line is trimmed, but empty lines appear between the items.Keep the strip markers and write the heredoc flush left.
Indenting the closing word of a <<EOT heredocOpenTofu doesn’t find the end of the heredoc.Only <<-EOT allows spaces before the closing word.

Troubleshooting

ProblemWhyFix
Lines after %{for … ~} or %{endfor~} keep their spacesThey follow a removed line break, so the indented heredoc doesn’t trim them.Use <<EOT and write the lines flush left.
Empty lines between repeated linesEach directive leaves its own line break.Add ~ before the closing } of each directive.

FAQ

Does this break the web page?

No. Browsers ignore the extra spaces in HTML. It matters when the exact text counts, such as a configuration file or a plan you read.

Does tofu fmt fix it?

No. tofu fmt doesn’t change the text of a heredoc; it only removes the spaces inside the directives’ braces.

Conclusion

An indented heredoc (<<-EOT) doesn’t trim the lines that follow a strip marker which removed a line break. Write templates with directives as flush-left heredocs (<<EOT) and every line keeps the spaces you wrote.

Additional Resources

OpenTofu® is a registered trademark of LF Projects, LLC. Other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by the OpenTofu project.

0 comments