Skip to content

syntax: heredoc trailing comment duplicated on each format when inside indented block #1246

@mbertheau

Description

@mbertheau

Summary

When a heredoc with a trailing comment (e.g., <<-EOF # comment) is inside an indented block, shfmt duplicates the comment after EOF on each run. This is non-idempotent behavior.

Steps to reproduce

$ cat test.sh
#!/bin/bash
if true; then
  cat <<-EOF # comment
    content
EOF
fi

$ shfmt -w test.sh
$ cat test.sh
#!/bin/bash
if true; then
  cat <<-EOF # comment
    content
EOF
  # comment
fi

$ shfmt -w test.sh
$ cat test.sh
#!/bin/bash
if true; then
  cat <<-EOF # comment
    content
EOF
  # comment
  # comment
fi

Each invocation adds another copy of the comment.

Environment

  • shfmt version: v3.12.0
  • OS: Linux (Fedora 42)

Related issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions