Skip to content

Commit 3d362a3

Browse files
committed
add quoting and logging for run as task and background
1 parent 38fbf5c commit 3d362a3

3 files changed

Lines changed: 21 additions & 9 deletions

File tree

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
export function quoteArg(arg: string): string {
1+
export function quoteStringIfNecessary(arg: string): string {
22
if (arg.indexOf(' ') >= 0 && !(arg.startsWith('"') && arg.endsWith('"'))) {
33
return `"${arg}"`;
44
}
55
return arg;
66
}
77

88
export function quoteArgs(args: string[]): string[] {
9-
return args.map(quoteArg);
9+
return args.map(quoteStringIfNecessary);
1010
}

src/features/execution/runAsTask.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import {
99
WorkspaceFolder,
1010
} from 'vscode';
1111
import { PythonEnvironment, PythonTaskExecutionOptions } from '../../api';
12-
import { traceInfo } from '../../common/logging';
12+
import { traceInfo, traceWarn } from '../../common/logging';
1313
import { executeTask } from '../../common/tasks.apis';
1414
import { getWorkspaceFolder } from '../../common/workspace.apis';
15-
import { quoteArg } from './execUtils';
15+
import { quoteStringIfNecessary } from './execUtils';
1616

1717
function getWorkspaceFolderOrDefault(uri?: Uri): WorkspaceFolder | TaskScope {
1818
const workspace = uri ? getWorkspaceFolder(uri) : undefined;
@@ -26,8 +26,14 @@ export async function runAsTask(
2626
): Promise<TaskExecution> {
2727
const workspace: WorkspaceFolder | TaskScope = getWorkspaceFolderOrDefault(options.project?.uri);
2828

29-
let executable = environment.execInfo?.activatedRun?.executable ?? environment.execInfo?.run.executable ?? 'python';
30-
executable = quoteArg(executable);
29+
let executable = environment.execInfo?.activatedRun?.executable ?? environment.execInfo?.run.executable;
30+
if (!executable) {
31+
traceWarn('No Python executable found in environment; falling back to "python".');
32+
executable = 'python';
33+
}
34+
// Check and quote the executable path if necessary
35+
executable = quoteStringIfNecessary(executable);
36+
3137
const args = environment.execInfo?.activatedRun?.args ?? environment.execInfo?.run.args ?? [];
3238
const allArgs = [...args, ...options.args];
3339
traceInfo(`Running as task: ${executable} ${allArgs.join(' ')}`);

src/features/execution/runInBackground.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import * as cp from 'child_process';
22
import { PythonBackgroundRunOptions, PythonEnvironment, PythonProcess } from '../../api';
3-
import { traceError, traceInfo } from '../../common/logging';
3+
import { traceError, traceInfo, traceWarn } from '../../common/logging';
4+
import { quoteStringIfNecessary } from './execUtils';
45

56
export async function runInBackground(
67
environment: PythonEnvironment,
78
options: PythonBackgroundRunOptions,
89
): Promise<PythonProcess> {
9-
const executable =
10-
environment.execInfo?.activatedRun?.executable ?? environment.execInfo?.run.executable ?? 'python';
10+
let executable = environment.execInfo?.activatedRun?.executable ?? environment.execInfo?.run.executable;
11+
if (!executable) {
12+
traceWarn('No Python executable found in environment; falling back to "python".');
13+
executable = 'python';
14+
}
15+
// Check and quote the executable path if necessary
16+
executable = quoteStringIfNecessary(executable);
1117
const args = environment.execInfo?.activatedRun?.args ?? environment.execInfo?.run.args ?? [];
1218
const allArgs = [...args, ...options.args];
1319
traceInfo(`Running in background: ${executable} ${allArgs.join(' ')}`);

0 commit comments

Comments
 (0)