Skip to content

Write bash completion output as raw bytes#1745

Open
behlole wants to merge 2 commits into
fastapi:masterfrom
behlole:fix/bash-completion-crlf-on-windows
Open

Write bash completion output as raw bytes#1745
behlole wants to merge 2 commits into
fastapi:masterfrom
behlole:fix/bash-completion-crlf-on-windows

Conversation

@behlole
Copy link
Copy Markdown

@behlole behlole commented May 14, 2026

Closes #202

On Windows, sys.stdout is opened in text mode, which translates \n to \r\n. Typer's bash completion writes option names separated by \n; after text-mode translation those separators become \r\n, but bash on MSYS2 / Git Bash splits only on \n. The stray \r ends up attached to the next option name and shows up as ^M between completions:

goodbye^M  hello

This PR encodes the completion output to UTF-8 bytes before handing it to click.echo. click.echo writes bytes through the raw buffer, bypassing text-mode translation. On POSIX the output is unchanged.

The regression test runs subprocess.run(..., capture_output=True) without encoding="utf-8" so it reads the raw bytes, then asserts the in-string separator is a bare \n (the byte-level check would silently pass if subprocess decoded the bytes back to text). It complements the existing assert "delete\ndelete-all" in result.stdout test, which still passes.

On Windows, sys.stdout is opened in text mode, which translates \n to
\r\n. Typer's bash completion writes option names separated by \n;
after text-mode translation those separators become \r\n, but bash
on MSYS2 / Git Bash splits only on \n. The stray \r ends up attached
to the next option name and shows up as ^M between completions.

Encode the completion output to UTF-8 bytes before handing it to
click.echo. click.echo writes bytes through the raw buffer, bypassing
text-mode translation. On POSIX the output is unchanged.

Closes fastapi#202
@svlandeg svlandeg added the bug Something isn't working label May 18, 2026
Copy link
Copy Markdown
Member

@svlandeg svlandeg left a comment

Choose a reason for hiding this comment

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

I'll put this in draft as long as the CI is failing, feel free to mark as "ready for review" when everything is green! 🙏

@svlandeg svlandeg marked this pull request as draft May 18, 2026 06:52
@behlole behlole marked this pull request as ready for review May 18, 2026 15:11
@behlole
Copy link
Copy Markdown
Author

behlole commented May 18, 2026

CI is green now — the failure on test (windows-latest, 3.12, lowest-direct) was an unrelated breakage from Click 8.4 that #1753 has since pinned out. I merged latest master in (556fd6a) so this branch picks up the pin, and the full matrix now passes (Windows lowest-direct included). Marking ready for review per your note above — thanks @svlandeg!

@github-actions github-actions Bot removed the waiting label May 18, 2026
@svlandeg svlandeg self-assigned this May 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] MSYS2 Bash and Git Bash for Windows autocomplete get ^M between autocomplete options

3 participants