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 3 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
Comment on lines 12 to 25
export function escapeTerminalCompletionLabel(label: string, shellType: TerminalShellType | undefined, pathSeparator: string): string {
// Only escape for bash/zsh/fish; PowerShell and cmd have different rules
if (!shellType || shellType === WindowsShellType.CommandPrompt || shellType === GeneralShellType.PowerShell) {
return label;
}

// Bash/zsh/fish escaping
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.

I don't think we can do this generally, these characters may be perfectly valid in the context and this would likely introduce a host of other problems.

The safest approach here is to escape on the completion item in the provider, so escape for the git branch generator and for paths when we know it's safe to do. We also want the label to represent exactly what will be typed and this changes that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, re

We also want the label to represent exactly what will be typed and this changes that.

That's how I initially implemented it. Then changed the approach because it looked a bit ugly

Screenshot 2025-06-20 at 9 28 20 AM

Copy link
Contributor Author

Choose a reason for hiding this comment

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

went back there

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we resolve the paths in core, not the extension, so cannot do this for just git specifically. What is a case when this is not safe or desirable?

Copy link
Member

Choose a reason for hiding this comment

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

For git it would need to be in the post processing of the generator:

@Tyriar Tyriar disabled auto-merge June 20, 2025 14:15
return label;
}

const specialCharsSet = new Set(['[', ']', '(', ')', '{', '}', `'`, '"', '\\', '$', '`', ';', '&', '|', '<', '>', '*', '?', '~', '#', '=', '%', '!']);
Copy link
Member

Choose a reason for hiding this comment

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

We should verify that all of these are actually needed here and have tests for each case. For example in bash I don't think we should use the escape for = since it works without it:

image

Starting with less that we know is wrong and adding over time would be the better approach than trying to cover everything and maybe introducing problems.

100% agree that it's more ugly with the escapes, so we should only use them when needed.

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