Skip to content

fix escaping for terminal completions for zsh/bash/fish #251989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

meganrogge
Copy link
Contributor

@meganrogge meganrogge commented Jun 20, 2025

fixes #249024

Screenshot 2025-06-20 at 9 28 20 AM Screenshot 2025-06-20 at 9 28 29 AM

@meganrogge meganrogge requested a review from Tyriar June 20, 2025 13:31
@meganrogge meganrogge self-assigned this Jun 20, 2025
@meganrogge meganrogge added this to the June 2025 milestone Jun 20, 2025
@meganrogge meganrogge marked this pull request as draft June 20, 2025 13:32
@meganrogge
Copy link
Contributor Author

Working on pwsh now

@meganrogge meganrogge changed the title fix escaping for bash/zsh/fish fix escaping for terminal completions Jun 20, 2025
@meganrogge
Copy link
Contributor Author

meganrogge commented Jun 20, 2025

Just realized maybe we want to handle escaping where this gets inserted instead of changing the label?

Yes, going with that instead

@meganrogge
Copy link
Contributor Author

meganrogge commented Jun 20, 2025

something weird is happening w pwsh . It gets escaped properly and sent to the terminal, but then the inserted text is wrong

Screenshot 2025-06-20 at 9 58 35 AM Screenshot 2025-06-20 at 9 58 56 AM

@meganrogge
Copy link
Contributor Author

pwsh seems involved, skipping for now

@meganrogge meganrogge changed the title fix escaping for terminal completions fix escaping for terminal completions for zsh/bash/fish Jun 20, 2025
@meganrogge meganrogge marked this pull request as ready for review June 20, 2025 14:04
@meganrogge meganrogge enabled auto-merge (squash) June 20, 2025 14:10
@Tyriar Tyriar disabled auto-merge June 20, 2025 14:15
@meganrogge
Copy link
Contributor Author

I manually tested each to make sure it's necessary @Tyriar and added tests too.

@meganrogge meganrogge enabled auto-merge (squash) June 23, 2025 19:14
@meganrogge meganrogge requested a review from Tyriar June 23, 2025 19:14
@meganrogge
Copy link
Contributor Author

@Tyriar could you pls review?

export function escapeTerminalCompletionLabel(label: string, shellType: TerminalShellType, pathSeparator: string): string {
// Only escape for bash/zsh/fish; PowerShell and cmd have different rules
if (shellType === GeneralShellType.PowerShell || shellType === WindowsShellType.CommandPrompt) {
// TODO: Implement escaping for PowerShell/cmd if needed
Copy link
Member

Choose a reason for hiding this comment

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

Let's remove unless it's needed?

Comment on lines +577 to +582
const specialCharsSet = new Set(['[', ']', '(', ')', '\'', '"', '\\', '`', '*', '?', ';', '&', '|', '<', '>']);
const specialCharsRegex = /[\[\]\(\)'"\\\`\*\?;|&<>]/;
if (!specialCharsRegex.test(label)) {
return label;
}
return label.split('').map(c => specialCharsSet.has(c) ? '\\' + c : c).join('');
Copy link
Member

Choose a reason for hiding this comment

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

Can simplify and use just regex:

input.replace(/[\[\]\(\)'"\\\`\*\?;|&<>]/g, '\\$&');

image

Comment on lines +405 to +407
if (shellType) {
label = escapeTerminalCompletionLabel(label, shellType, resourceRequestConfig.pathSeparator);
}
Copy link
Member

Choose a reason for hiding this comment

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

nit: Could move the if into the escape function and allow it to accept shellType undefined to simplify this function a little

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.

terminal completions don't escape properly
2 participants