diff options
author | Rob Lourens <roblourens@gmail.com> | 2022-05-12 04:12:45 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-12 04:12:45 +0300 |
commit | fbd5e15934278f6b1d0f98789e6c438185f5aedf (patch) | |
tree | 4c0ad2b6ff53400dee5cd6b55c184fc0d736d487 | |
parent | 93d046904eb3376017e9458105e2511c6122cd35 (diff) |
Some terminal launch config args are double-escaped (#149307)
Fixes #149283
-rw-r--r-- | src/vs/workbench/contrib/debug/node/terminals.ts | 8 | ||||
-rw-r--r-- | src/vs/workbench/contrib/debug/test/node/terminals.test.ts | 76 |
2 files changed, 82 insertions, 2 deletions
diff --git a/src/vs/workbench/contrib/debug/node/terminals.ts b/src/vs/workbench/contrib/debug/node/terminals.ts index 5390b15a8ec..465d92947c5 100644 --- a/src/vs/workbench/contrib/debug/node/terminals.ts +++ b/src/vs/workbench/contrib/debug/node/terminals.ts @@ -121,6 +121,10 @@ export function prepareCommand(shell: string, args: string[], cwd?: string, env? case ShellType.cmd: quote = (s: string) => { + // Note: Wrapping in cmd /C "..." complicates the escaping. + // cmd /C "node -e "console.log(process.argv)" """A^>0"""" # prints "A>0" + // cmd /C "node -e "console.log(process.argv)" "foo^> bar"" # prints foo> bar + // Outside of the cmd /C, it could be a simple quoting, but here, the ^ is needed too s = s.replace(/\"/g, '""'); s = s.replace(/([><!^&|])/g, '^$1'); return (' "'.split('').some(char => s.includes(char)) || s.length === 0) ? `"${s}"` : s; @@ -157,8 +161,8 @@ export function prepareCommand(shell: string, args: string[], cwd?: string, env? case ShellType.bash: { quote = (s: string) => { - s = s.replace(/(["'\\\$!><#()\[\]*&^|])/g, '\\$1'); - return (' ;'.split('').some(char => s.includes(char)) || s.length === 0) ? `"${s}"` : s; + s = s.replace(/(["'\\\$!><#()\[\]*&^| ;])/g, '\\$1'); + return s.length === 0 ? `""` : s; }; const hardQuote = (s: string) => { diff --git a/src/vs/workbench/contrib/debug/test/node/terminals.test.ts b/src/vs/workbench/contrib/debug/test/node/terminals.test.ts new file mode 100644 index 00000000000..cbb780f76de --- /dev/null +++ b/src/vs/workbench/contrib/debug/test/node/terminals.test.ts @@ -0,0 +1,76 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import * as assert from 'assert'; +import { prepareCommand } from 'vs/workbench/contrib/debug/node/terminals'; + + +suite('Debug - prepareCommand', () => { + test('bash', () => { + assert.strictEqual( + prepareCommand('bash', ['{$} (']).trim(), + '{\\$}\\ \\('); + assert.strictEqual( + prepareCommand('bash', ['hello', 'world', '--flag=true']).trim(), + 'hello world --flag=true'); + assert.strictEqual( + prepareCommand('bash', [' space arg ']).trim(), + '\\ space\\ arg\\'); + }); + + test('bash - do not escape > and <', () => { + assert.strictEqual( + prepareCommand('bash', ['arg1', '>', '> hello.txt', '<', '<input.in']).trim(), + 'arg1 > \\>\\ hello.txt < \\<input.in'); + }); + + test('cmd', () => { + assert.strictEqual( + prepareCommand('cmd.exe', ['^!< ']).trim(), + '"^^^!^< "'); + assert.strictEqual( + prepareCommand('cmd.exe', ['hello', 'world', '--flag=true']).trim(), + 'hello world --flag=true'); + assert.strictEqual( + prepareCommand('cmd.exe', [' space arg ']).trim(), + '" space arg "'); + assert.strictEqual( + prepareCommand('cmd.exe', ['"A>0"']).trim(), + '"""A^>0"""'); + assert.strictEqual( + prepareCommand('cmd.exe', ['']).trim(), + '""'); + }); + + test('cmd - do not escape > and <', () => { + assert.strictEqual( + prepareCommand('cmd.exe', ['arg1', '>', '> hello.txt', '<', '<input.in']).trim(), + 'arg1 > "^> hello.txt" < ^<input.in'); + }); + + test('powershell', () => { + assert.strictEqual( + prepareCommand('powershell', ['!< ']).trim(), + `& '!< '`); + assert.strictEqual( + prepareCommand('powershell', ['hello', 'world', '--flag=true']).trim(), + `& 'hello' 'world' '--flag=true'`); + assert.strictEqual( + prepareCommand('powershell', [' space arg ']).trim(), + `& ' space arg '`); + assert.strictEqual( + prepareCommand('powershell', ['"A>0"']).trim(), + `& '"A>0"'`); + assert.strictEqual( + prepareCommand('powershell', ['']).trim(), + `& ''`); + }); + + test('powershell - do not escape > and <', () => { + assert.strictEqual( + prepareCommand('powershell', ['arg1', '>', '> hello.txt', '<', '<input.in']).trim(), + `& 'arg1' > '> hello.txt' < '<input.in'`); + }); +}); |