Skip to content

syntax: fix nested heredoc indentation - #1409

Merged
mvdan merged 2 commits into
mvdan:masterfrom
milhnl:fix-nested-heredoc
Aug 31, 2026
Merged

mvdan merged 2 commits into
mvdan:masterfrom
milhnl:fix-nested-heredoc

Conversation

@milhnl

@milhnl milhnl commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Continuing the explanation from #1403 (comment):

The Write method is only really used when writing the content of a heredoc. In the normal case, one level of heredoc, this is fine. Everything in it will use WriteByte which handles the heredoc indentation.

Where it went wrong is with an inner heredoc. In that situation the inner extraIndenter writes only the tabs to the buffer of the outer heredoc, while writing the trimmed content directly to the outer Write (which is a bufio.Writer I think). This results in the content of the inner heredoc having no tabs, which accumulate in the buffer and are output with the ending marker.

This fix defines a Write method for extraIndenter that forwards the writes to the WriteByte (like WriteString) so that all writes are via the same path, as intended, and inner heredocs do not bypass it anymore.

Fixes #1403

milhnl and others added 2 commits August 31, 2026 11:45
Implement a Write method for extraIndenter that ensures a nested
heredoc does not bypass (by directly calling the bufio.Writer) the
indentation accounting of the outer extraIndenter.

Fixes #1403
Lines coming from a nested extraIndenter are wrapped in
tabwriter.Escape, so they appear to have no indentation and take the
under-indent branch. Pinning them to the first line's source
indentation only worked when it matched baseIndent, as with two levels
of nesting; use baseIndent, where the first line is actually printed.
@mvdan

mvdan commented Aug 31, 2026

Copy link
Copy Markdown
Owner

Thanks, the fix LGTM - I just found that at deeper levels we were still buggy due to another oversight. I've fixed that in a commit I've pushed on top of yours here, as both felt like they were closely related.

@mvdan
mvdan merged commit 3c1ff2a into mvdan:master Aug 31, 2026
@milhnl

milhnl commented Aug 31, 2026

Copy link
Copy Markdown
Contributor Author

Thank you! I should have tested for n :)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Nested tab heredoc formatting bug

2 participants