Skip to content

Respect system newline ending for hooks#1753

Merged
nathanjmcdougall merged 2 commits intomainfrom
copilot/respect-system-newline-ending
Apr 1, 2026
Merged

Respect system newline ending for hooks#1753
nathanjmcdougall merged 2 commits intomainfrom
copilot/respect-system-newline-ending

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 1, 2026

Hook scripts used hardcoded "\n" for newline characters when constructing file content. Replace with os.linesep and newline="" I/O to explicitly respect the OS newline convention.

Hook changes

All five file-writing hooks (export-functions, export-skills-directory, export-module-tree, fix-readme, fix-doc-sync) updated with the same pattern:

# Before
content = "\n".join(lines) + "\n"
existing = output_file.read_text(encoding="utf-8")
output_file.write_text(content, encoding="utf-8")

# After
content = os.linesep.join(lines) + os.linesep
with open(output_file, encoding="utf-8", newline="") as f:
    existing = f.read()
output_file.write_text(content, encoding="utf-8", newline="")

newline="" is required on both read and write to prevent Python's text mode from double-translating \r\n on Windows. read_text(newline=...) requires Python 3.13, so reads use open() instead (project minimum is 3.10).

For fix-readme.py, Jinja2 renders with \n internally, so conversion happens once after template.render(). For fix-doc-sync.py, _build_replacement() uses os.linesep and all file I/O uses newline="".

Agent skill update

Added a "Newline endings" section to the usethis-prek-hook-bespoke-create skill (v1.1 → v1.2) documenting the os.linesep + newline="" pattern for future hook authoring.

Use os.linesep instead of hardcoded "\n" when constructing file content
in hook scripts, and pass newline="" to I/O calls to prevent
double-translation. Update the bespoke hook creation skill with newline
handling guidance.

Resolves #1792

Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/db9749a3-ce81-4ce8-8f12-b9e0d6c81012

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix hooks to respect system newline ending Respect system newline ending for hooks Apr 1, 2026
Copilot AI requested a review from nathanjmcdougall April 1, 2026 22:25
@nathanjmcdougall nathanjmcdougall marked this pull request as ready for review April 1, 2026 22:41
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 1, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Apr 1, 2026

Merging this PR will not alter performance

✅ 2 untouched benchmarks


Comparing copilot/respect-system-newline-ending (34757e6) with main (0c72c0a)

Open in CodSpeed

@nathanjmcdougall nathanjmcdougall merged commit 3b8c7d2 into main Apr 1, 2026
10 checks passed
@nathanjmcdougall nathanjmcdougall deleted the copilot/respect-system-newline-ending branch April 1, 2026 22:48
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.

Respect system newline ending for hooks

2 participants