Repository navigation
fix(presets): split replace-section lines only at Markdown line endings - #5572
Merged
mnriem merged 1 commit intoNov 25, 2026
Merged
Conversation
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)
Contributor
There was a problem hiding this comment.
🟢 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)
Collaborator
|
Thank you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
replace_section(thereplace-sectionpreset strategy, #5462) scans the target document withstr.splitlines(keepends=True). Python'ssplitlinesalso 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 parsesSee page ## Fakeas one paragraph, not a heading). So text after one of those characters was scanned as the start of a new line:## ...after it became a fake heading that ended the selected body early, so part of the old body stayed in the composed template;```after it opened a fake fence that hid every later heading (SECTION_NOT_FOUND);Reproduction on main:
specify init --here --integration claude --ignore-agent-tools --script sh, thenspecify preset add --deva preset that replacesspec-templatewith a document whose## Notesbody isSee the vendor page ## Not a heading, same paragraph, and a second preset withstrategy: replace-section,selector: notesand the body\nNew notes.\n\n..specify/scripts/bash/resolve-template.sh spec-templateprinted## Notes,New notes., then the leftover## Not a heading, same paragraphline before## Review: the old text before the U+2028 was dropped and the text after it was kept. With this change the wholeNotesbody 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
.venvand 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 andLC_ALL=en_US.UTF-8on macOSTested 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\rand\r\nstill end lines) pass on both.Full suite with
-n autoon 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.shreproduction above, before and after (freshspecify initwith the fixed checkout).AI Disclosure
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 carriesAssisted-by:trailers.