-
Notifications
You must be signed in to change notification settings - Fork 33.2k
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
base: main
Are you sure you want to change the base?
Conversation
Working on pwsh now |
Just realized maybe we want to handle escaping where this gets inserted instead of changing the label? Yes, going with that instead |
pwsh seems involved, skipping for now |
src/vs/workbench/contrib/terminalContrib/suggest/browser/terminalCompletionService.ts
Outdated
Show resolved
Hide resolved
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(''); | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
went back there
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
const postProcessBranches = |
6d9c5e5
to
6cd435c
Compare
return label; | ||
} | ||
|
||
const specialCharsSet = new Set(['[', ']', '(', ')', '{', '}', `'`, '"', '\\', '$', '`', ';', '&', '|', '<', '>', '*', '?', '~', '#', '=', '%', '!']); |
There was a problem hiding this comment.
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:
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.
fixes #249024