Skip to content

Commit 71d4b16

Browse files
authored
Fix debugpy.ps1 argument forwarding with splatting (@Args) (#1146)
* Fix debugpy.ps1 argument forwarding with splatting The PowerShell no-config wrapper forwarded $args without splatting, so an array of arguments passed by a caller reached the Python process concatenated as a single string (e.g. 'arg1 arg2' instead of two separate argv entries). Use @Args in both the Windows (python) and non-Windows (python3) invocations so array arguments expand into separate arguments, matching how the caller could pass them to python directly. Fixes #1024 * Restore executable bit on PowerShell no-config wrapper * Add regression test for debugpy.ps1 argument forwarding Run the real wrapper under PowerShell with the bundled debugpy path pointed at a stub that prints sys.argv, called with an array argument. The array must reach the Python process as separate arguments, not one space-joined string. --------- Co-authored-by: PCTEJA <pcteja@users.noreply.github.com>
1 parent af502e7 commit 71d4b16

2 files changed

Lines changed: 55 additions & 2 deletions

File tree

‎bundled/scripts/noConfigScripts/debugpy.ps1‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
$env:DEBUGPY_ADAPTER_ENDPOINTS = $env:VSCODE_DEBUGPY_ADAPTER_ENDPOINTS
33
$os = [System.Environment]::OSVersion.Platform
44
if ($os -eq [System.PlatformID]::Win32NT) {
5-
python $env:BUNDLED_DEBUGPY_PATH --listen 0 --wait-for-client $args
5+
python $env:BUNDLED_DEBUGPY_PATH --listen 0 --wait-for-client @args
66
} else {
7-
python3 $env:BUNDLED_DEBUGPY_PATH --listen 0 --wait-for-client $args
7+
python3 $env:BUNDLED_DEBUGPY_PATH --listen 0 --wait-for-client @args
88
}

‎src/test/unittest/noConfigDebugInit.unit.test.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
// Licensed under the MIT License.
33

44
import * as path from 'path';
5+
import { execFile } from 'child_process';
6+
import { promisify } from 'util';
7+
import { expect } from 'chai';
58
import { IExtensionContext } from '../../extension/common/types';
9+
import { EXTENSION_ROOT_DIR } from '../../extension/common/constants';
10+
import { TEST_TIMEOUT } from '../constants';
611
import { registerNoConfigDebug as registerNoConfigDebug } from '../../extension/noConfigDebugInit';
712
import * as TypeMoq from 'typemoq';
813
import * as sinon from 'sinon';
@@ -13,6 +18,8 @@ import * as fs from 'fs';
1318
import * as os from 'os';
1419
import * as crypto from 'crypto';
1520

21+
const execFileAsync = promisify(execFile);
22+
1623
suite('setup for no-config debug scenario', function () {
1724
let envVarCollectionReplaceStub: sinon.SinonStub;
1825
let envVarCollectionAppendStub: sinon.SinonStub;
@@ -225,8 +232,54 @@ suite('setup for no-config debug scenario', function () {
225232
fsExistsSyncStub.restore();
226233
fsUnlinkSyncStub.restore();
227234
});
235+
236+
test('debugpy.ps1 forwards array arguments as separate arguments', async function () {
237+
this.timeout(TEST_TIMEOUT);
238+
const shell = await findPowerShell();
239+
if (!shell) {
240+
return this.skip();
241+
}
242+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'debugpy-ps1-test-'));
243+
try {
244+
const adapterStub = path.join(tmpDir, 'print_argv.py');
245+
fs.writeFileSync(adapterStub, 'import json, sys\nprint(json.dumps(sys.argv[1:]))\n');
246+
const wrapper = path.join(EXTENSION_ROOT_DIR, 'bundled', 'scripts', 'noConfigScripts', 'debugpy.ps1');
247+
const caller = path.join(tmpDir, 'caller.ps1');
248+
fs.writeFileSync(
249+
caller,
250+
`$argList = @('arg1', 'arg2')\n& '${wrapper.replace(/'/g, "''")}' 'test.py' $argList\n`,
251+
);
252+
const env = { ...process.env };
253+
env.BUNDLED_DEBUGPY_PATH = adapterStub;
254+
env.VSCODE_DEBUGPY_ADAPTER_ENDPOINTS = '';
255+
const { stdout } = await execFileAsync(
256+
shell,
257+
['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', caller],
258+
{
259+
env,
260+
},
261+
);
262+
const argv = JSON.parse(stdout.trim().split('\n').pop()!);
263+
expect(argv).to.deep.equal(['--listen', '0', '--wait-for-client', 'test.py', 'arg1', 'arg2']);
264+
} finally {
265+
fs.rmSync(tmpDir, { recursive: true, force: true });
266+
}
267+
});
228268
});
229269

270+
async function findPowerShell(): Promise<string | undefined> {
271+
const shells = process.platform === 'win32' ? ['powershell', 'pwsh'] : ['pwsh', 'powershell'];
272+
for (const shell of shells) {
273+
try {
274+
await execFileAsync(shell, ['-NoProfile', '-Command', '$PSVersionTable.PSVersion']);
275+
return shell;
276+
} catch {
277+
continue;
278+
}
279+
}
280+
return undefined;
281+
}
282+
230283
function setupFileSystemWatchers(): sinon.SinonStub {
231284
// create stub of createFileSystemWatcher function that will return a fake watcher with a callback
232285
let createFileSystemWatcherFunct: sinon.SinonStub;

0 commit comments

Comments
 (0)