Skip to content

Escape backslashes in fish completion descriptions#536

Open
greymoth-jp wants to merge 1 commit into
python-poetry:mainfrom
greymoth-jp:fix-fish-completion-backslash
Open

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

Conversation

@greymoth-jp

Copy link
Copy Markdown

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

def sanitize(s: str) -> str:
    return self._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.

@dosubot dosubot Bot 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.
@greymoth-jp greymoth-jp force-pushed the fix-fish-completion-backslash branch from 15101ac to 0eb0722 Compare June 30, 2026 08:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant