Skip to content

Commit

Permalink
Fix escaping for cmd.exe: either quote or escape individual special…
Browse files Browse the repository at this point in the history
… characters (#49)
  • Loading branch information
gadenbuie authored May 3, 2024
1 parent c894fa8 commit 4fbeaa5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- **Create ShinyLive Link from Active File** creates a Shinylive link from the active file (Command Palette).
- **Create ShinyLive Link from Selected Files** creates a Shinylive link from the selected files or directories in the right-click context menu of the File Explorer.
- **Save App from Shinylive Link** saves an app and its files from a Shinylive link (Command Palette).
- Fixed a bug that would doubly-escape paths with spaces when launching Shiny apps on Windows via `cmd.exe`. ([#46](https://github.com/posit-dev/shiny-vscode/issues/46))

## 0.1.6

Expand Down
14 changes: 11 additions & 3 deletions src/shell-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,17 @@ function escapeStyle(terminal: vscode.Terminal): EscapeStyle {
export function escapeArg(arg: string, style: EscapeStyle): string {
switch (style) {
case "cmd":
// For cmd.exe, double quotes are used to handle spaces, and carets (^) are used to escape special characters.
const escaped = arg.replace(/([()%!^"<>&|])/g, "^$1");
return /\s/.test(escaped) ? `"${escaped}"` : escaped;
// For cmd.exe, use double quotes if input includes spaces
if (/\s/.test(arg)) {
if (!arg.includes('"')) {
return `"${arg}"`;
}
// Escape double quotes by doubling them
return `"${arg.replace(/"/g, '"""')}"`;
}

// Carets (^) are used to escape special characters in unquoted strings.
return arg.replace(/([()%!^"<>&|])/g, "^$1");

case "ps":
if (!/[ '"`,;(){}|&<>@#[\]]/.test(arg)) {
Expand Down

0 comments on commit 4fbeaa5

Please sign in to comment.