Skip to content

fix(presets): split replace-section lines only at Markdown line endings - #5572

Merged
mnriem merged 1 commit into
github:mainfrom
leninworld:fix/replace-section-markdown-line-endings
Nov 25, 2026
Merged

mnriem merged 1 commit into
github:mainfrom
leninworld:fix/replace-section-markdown-line-endings

Conversation

@leninworld

Copy link
Copy Markdown
Contributor

Description

replace_section (the replace-section preset strategy, #5462) scans the target document with str.splitlines(keepends=True). Python's splitlines also breaks lines at \v, \f, \x1c–\x1e, \x85, U+2028 and U+2029, but Markdown only ends lines at LF, CR and CRLF (CommonMark; markdown-it parses See page
## Fake as one paragraph, not a heading). So text after one of those characters was scanned as the start of a new line:

  • a ## ... after it became a fake heading that ended the selected body early, so part of the old body stayed in the composed template;
  • a ``` after it opened a fake fence that hid every later heading (SECTION_NOT_FOUND);
  • diagnostic candidate line numbers were too high.

Reproduction on main: specify init --here --integration claude --ignore-agent-tools --script sh, then specify preset add --dev a preset that replaces spec-template with a document whose ## Notes body is See the vendor page
## Not a heading, same paragraph, and a second preset with strategy: replace-section, selector: notes and the body \nNew notes.\n\n. .specify/scripts/bash/resolve-template.sh spec-template printed ## Notes, New notes., then the leftover ## Not a heading, same paragraph line before ## Review: the old text before the U+2028 was dropped and the text after it was kept. With this change the whole Notes body is replaced and the rest of the document is unchanged.

The fix splits only at "\r\n", "\r" and "\n", keeping the line endings, so offsets, frontmatter, fence and heading handling are unchanged. The bash, PowerShell and Python scripts all run this same Python engine, so no script changes are needed.

Testing

Set up this checkout's .venv and editable install using the local development guide. Run commands from the checkout root.

  • Tested locally with .venv/bin/specify --help (Windows PowerShell: .\.venv\Scripts\specify.exe --help)

  • Ran relevant existing tests with .venv/bin/python -m pytest tests -q (Windows PowerShell: .\.venv\Scripts\python.exe -m pytest tests -q); use affected test paths for focused checks and LC_ALL=en_US.UTF-8 on macOS

  • Tested with a sample project (if applicable)

  • New tests in tests/test_markdown_sections.py, parametrized over the 8 characters: a fake heading does not end the selected body, a fake fence does not hide a later heading, and a fake heading is not selectable; plus a case that a U+2028 does not shift the reported duplicate-heading line numbers. These 25 cases fail on main and pass with the fix. Two guard cases (lone \r and \r\n still end lines) pass on both.

  • Full suite with -n auto on macOS: 17293 passed, 635 skipped, 1 failed: test_version_execution.py::TestWindowsUvToolEnvironment::test_non_windows_uv_tool_dry_run_still_previews_installer, which fails on main in my environment too.

  • uvx ruff@0.15.0 check src tests: all checks passed. pre-commit hooks pass on the changed files.

  • Sample project: the specify init / specify preset add --dev / resolve-template.sh reproduction above, before and after (fresh specify init with the fixed checkout).

AI Disclosure

  • I did not use AI assistance for this contribution
  • I did use AI assistance (fill in the disclosure below)

AI disclosure: Claude Code (model: Claude Opus 5.5, autonomous) found the bug while reviewing markdown_sections.py, checked it against markdown-it, reproduced it end to end, reviewed the change, ran the tests, lint and hooks, and wrote this description. OpenAI Codex CLI (model: gpt-5.6-sol, autonomous) wrote the fix and tests (code generation) from Claude's task description. The commit carries Assisted-by: trailers.

replace_section scanned the document with str.splitlines(), which also
breaks lines at \v, \f, \x1c-\x1e, \x85, U+2028 and U+2029. Markdown
only ends lines at LF, CR and CRLF, so text after one of those
characters was scanned as a new line: a "## ..." after it became a fake
heading that ended the selected body early and left part of the old
body in the composed template, a ``` after it opened a fake fence that
hid every later heading, and diagnostic line numbers were too high.

Split only at "\r\n", "\r" and "\n", keeping the line endings, so the
offsets and everything else stay unchanged.

Assisted-by: OpenAI Codex CLI (model: gpt-5.6-sol, autonomous)
Assisted-by: Claude Code (model: Claude Opus 5.5, autonomous)
@leninworld
leninworld requested a review from mnriem as a code owner November 25, 2026 17:40
Copilot AI adaptive review requested due to automatic review settings November 25, 2026 17:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the change restricting Markdown line splitting to LF, CR, and CRLF.

No open issues 🎉

What changed in this PR

Fixes the shared replace-section engine so only LF, CR, and CRLF end Markdown lines. This prevents embedded control and Unicode separators from creating false headings or fences and shifting diagnostic line numbers.

Changes:

  • Replaces str.splitlines() with Markdown-specific splitting that preserves line endings.
  • Adds 25 regression cases and two guard cases for CR and CRLF support.

The PR reports before-and-after regression results; these were not independently rerun.

File Description
tests/​test_markdown_sections.py Adds coverage for non-Markdown separators, diagnostic line numbers, and carriage-return line endings.
src/​specify_cli/​markdown_sections.py Restricts line splitting to LF, CR, and CRLF.

Review effort: Balanced (adaptive)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues were identified for the Markdown section-scanning change.

No open issues 🎉

Review effort: Balanced

@mnriem
mnriem merged commit a01c3ea into github:main Nov 25, 2026
14 checks passed
@mnriem

mnriem commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Thank you!

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.

3 participants