Skip to content

Fix stray ^M characters in subprocess output when redirected to file - #462

Draft
lengau with Copilot wants to merge 4 commits into
mainfrom
copilot/fix-linefeeds-when-redirecting-output
Draft

Fix stray ^M characters in subprocess output when redirected to file#462
lengau with Copilot wants to merge 4 commits into
mainfrom
copilot/fix-linefeeds-when-redirecting-output

Conversation

Copilot AI commented Jul 15, 2026

Copy link
Copy Markdown

Subprocesses using bare \r for in-place terminal progress updates (e.g. Downloading 50%\rDownloading 100%\n) pass those carriage returns through _PipeReaderThread._write() unchanged. When the output stream is not a terminal (file redirect, pipe), the \r chars survive into the written text, producing ^M-riddled output.

Changes

  • craft_cli/messages.py — Normalize carriage returns in _PipeReaderThread._write() before line-splitting via a single re.sub pass:

    • \r\n / \r+\n (Windows endings, \r\r\n) → \n (no extra blank lines)
    • bare \r (in-place overwrites) → \n, so each update becomes its own line
    • b"\r" in data guard skips regex entirely for the common CR-free case
  • tests/unit/test_messages_stream_cm.py — Two new tests as reproducers:

    • test_pipereader_carriage_returns: b"Step 1\rStep 2\rStep 3\n" → three clean lines, no \r
    • test_pipereader_windows_line_endings: b"line1\r\nline2\r\n" → two clean lines, no \r
# Before: \r passed through to the captured message
# text == ':: Initializing\rDone!'   ← stray ^M in file

# After: bare \r treated as line separator
# text[0] == ':: Initializing'
# text[1] == ':: Done!'

Copilot AI self-assigned this Jul 15, 2026
Copilot AI review requested due to automatic review settings July 15, 2026 17:59
Copilot AI removed the request for review from Copilot July 15, 2026 17:59
Copilot AI linked an issue Jul 15, 2026 that may be closed by this pull request
Copilot AI requested review from Copilot and removed request for Copilot July 15, 2026 18:07
Copilot AI requested review from Copilot and removed request for Copilot July 15, 2026 18:10
Copilot AI requested review from Copilot and removed request for Copilot July 15, 2026 18:11
Copilot AI changed the title [WIP] Fix linefeeds in craft-cli application output redirection Fix stray ^M characters in subprocess output when redirected to file Jul 15, 2026
Copilot AI requested a review from lengau July 15, 2026 18:12
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.

Linefeeds are wrong when output is redirected to file

2 participants