Skip to content

[Bug]: replace-section merges the following heading into a replacement without a trailing newline #5585

Backlog

Description

@Winget-Software

Bug Description

replace_section (the replace-section preset strategy from #5462, in src/specify_cli/markdown_sections.py, shipped as .specify/scripts/bash/markdown_sections.py) joins the next heading onto the last line of the replacement when the contribution has no trailing newline and the selected section is followed by a sibling or ancestor heading.

The function returns content[:body_start] + replacement + content[body_end:]. The only separator logic covers the case where the selected heading is the last line of the document (body_start == len(content)). Nothing makes sure replacement ends with a line break before content[body_end:], which begins with the next heading. The heading then stops being a heading: it is silently dropped from the composed template, and any later replace-section targeting it fails with SECTION_NOT_FOUND.

Steps to Reproduce

With specify-cli 1.4.5 (same code on main at a01c3ea):

from specify_cli.markdown_sections import replace_section

replace_section(
    "# First\nold body\n\n# Second\nkeep\n",
    "new body",            # contribution without a trailing newline
    "first",
    preset="p", contribution="c", artifact="a",
)

The same happens end to end with a preset whose replace-section contribution file has no final newline (common when an editor or generator strips it).

Expected Behavior

'# First\nnew body\n# Second\nkeep\n': the following heading is kept on its own line, using the document's line ending (\n or \r\n, as the existing end-of-document branch already does).

Actual Behavior

'# First\nnew body# Second\nkeep\n': # Second is merged into the replacement's last line and no longer parses as a heading.

Specify CLI Version

1.4.5

AI Agent

Claude Code

Operating System

macOS

Python Version

3.x

Error Logs

N/A (silent corruption; a later replace-section on the lost heading reports SECTION_NOT_FOUND)

Additional Context

Suggested fix: when content[body_end:] is non-empty and replacement is non-empty and does not end with \n or \r, append the document's line separator to replacement before concatenating, plus a regression test in tests/test_markdown_sections.py for a contribution without a trailing newline (LF and CRLF) followed by a sibling and by an ancestor heading. Found by an automated code review on a downstream project and confirmed by running the function.

AI Disclosure

  • I did use AI assistance (describe below)

Claude Code found the report in an automated review, reproduced it against 1.4.5, checked that main has the same code, and drafted this issue; a human reviewed it before filing.

Activity

  1. Winget-Software commented on Nov 26, 2026

    @Winget-Software
    Author

    Fix proposed in #5586.

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

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