Skip to content

Commit 2fcc693

Browse files
authored
Check validity of executable used for debugging by executing a python file (microsoft#2699)
For microsoft#2531
1 parent 3774ed5 commit 2fcc693

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

pythonFiles/printOne.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
print('1')

src/client/debugger/mainV2.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { DebugProtocol } from 'vscode-debugprotocol';
2020
import '../../client/common/extensions';
2121
import { createDeferred, Deferred, sleep } from '../../utils/async';
2222
import { noop } from '../../utils/misc';
23+
import { EXTENSION_ROOT_DIR } from '../common/constants';
2324
import { isNotInstalledError } from '../common/helpers';
2425
import { IFileSystem } from '../common/platform/types';
2526
import { IProcessServiceFactory } from '../common/process/types';
@@ -123,11 +124,23 @@ export class PythonDebugger extends DebugSession {
123124
this.sendErrorResponse(response, { format: message, id: 1 }, undefined, undefined, ErrorDestination.User);
124125
});
125126
}
127+
/**
128+
* Checks the validity of the executable.
129+
* Do not use `<python> --version` as the output in 2.7 comes in stderr.
130+
* Do not use `<python> -c print('1')` as the executable could be pyspark.
131+
* Use `<python> xyz.py` to check output as this is absolutely necessary for debugger to start.
132+
* @private
133+
* @param {DebugProtocol.LaunchResponse} response
134+
* @param {LaunchRequestArguments} args
135+
* @returns {Promise<boolean>}
136+
* @memberof PythonDebugger
137+
*/
126138
private async validatePythonPath(response: DebugProtocol.LaunchResponse, args: LaunchRequestArguments): Promise<boolean> {
127139
const pythonPath = typeof args.pythonPath === 'string' && args.pythonPath.length > 0 ? args.pythonPath : 'python';
128140
const processFactory = this.serviceContainer.get<IProcessServiceFactory>(IProcessServiceFactory);
129141
const processService = await processFactory.create();
130-
const valid = await processService.exec(pythonPath, ['-c', 'print("1")'])
142+
const pythonFile = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'printOne.py');
143+
const valid = await processService.exec(pythonPath, [pythonFile])
131144
.then(output => output.stdout.trim() === '1' || (output.stderr || '').trim() === '1')
132145
.catch(() => false);
133146
if (!valid) {

0 commit comments

Comments
 (0)