From 4fbeaa50cbebce2e37b8b67a0680996475a2547c Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Fri, 3 May 2024 12:21:44 -0400 Subject: [PATCH] Fix escaping for `cmd.exe`: either quote or escape individual special characters (#49) --- CHANGELOG.md | 1 + src/shell-utils.ts | 14 +++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f757b9..efd0a44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/shell-utils.ts b/src/shell-utils.ts index b067935..f7a0472 100644 --- a/src/shell-utils.ts +++ b/src/shell-utils.ts @@ -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)) {