Skip to content

Escape backslashes in fish completion descriptions - #536

Closed
mahirhir wants to merge 1 commit into
python-poetry:mainfrom
mahirhir:fix-fish-completion-backslash
Closed

Escape backslashes in fish completion descriptions#536
mahirhir wants to merge 1 commit into
python-poetry:mainfrom
mahirhir:fix-fish-completion-backslash

Conversation

@mahirhir

Copy link
Copy Markdown

The fish completions generator escapes single quotes in each option and command description, but leaves backslashes untouched:

defsanitize(s: str) ->str:
returnself._io.output.formatter.remove_format(s).replace("'", "\'")

The result is emitted inside a single-quoted fish argument, -d '<description>'. Single quotes in fish are not fully literal: per the language docs, "the only meaningful escape sequences in single quotes are \', which escapes a single quote and \, which escapes the backslash symbol." So a backslash in a description is mishandled:

  • a doubled backslash collapses to a single one, and
  • a trailing backslash escapes the closing quote, which ends the string early and breaks the rest of the generated complete line.

The zsh path already accounts for this. _zsh_describe escapes backslashes (they are part of the character class it passes through re.sub), so the fish path is the odd one out.

The fix escapes backslashes before single quotes, matching the order zsh already uses:

return (
self._io.output.formatter.remove_format(s)
.replace("\\", "\\\\")
.replace("'", "\'")
)

I added a regression test that renders a fish completion for a command whose description contains a backslash and a single quote, and checks that both are escaped. Reverting the one-line change makes it fail. The full test suite passes and ruff check/ruff format are clean.

@dosubotdosubotBot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jun 30, 2026
The fish completions generator escapes single quotes in descriptions but
leaves backslashes untouched. Since a backslash inside fish single quotes
still escapes the next character, a doubled backslash collapses and a
trailing backslash escapes the closing quote, breaking the generated
completion line. Escape backslashes first, as the zsh path already does.
@mahirhir
mahirhirforce-pushed the fix-fish-completion-backslash branch from 15101ac to 0eb0722CompareJune 30, 2026 08:55
@mahirhir

Copy link
Copy Markdown
Author

Closing this to clean up my open PRs. It's a small change and not really worth taking up your review time. Sorry for the noise.

@mahirhirmahirhir closed this Jul 4, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:SThis PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@mahirhir