Skip to content

Commit 23fdb5a

Browse files
authored
add quoting, logging, and resolution for run as task and background (#731)
1 parent 38fbf5c commit 23fdb5a

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

src/features/envCommands.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -574,8 +574,10 @@ export async function runInTerminalCommand(
574574
const project = api.getPythonProject(uri);
575575
const environment = await api.getEnvironment(uri);
576576
if (environment && project) {
577-
const terminal = await tm.getProjectTerminal(project, environment);
578-
await runInTerminal(environment, terminal, {
577+
const resolvedEnv = await api.resolveEnvironment(environment.environmentPath);
578+
const envFinal = resolvedEnv ?? environment;
579+
const terminal = await tm.getProjectTerminal(project, envFinal);
580+
await runInTerminal(envFinal, terminal, {
579581
cwd: project.uri,
580582
args: [item.fsPath],
581583
show: true,
@@ -594,9 +596,12 @@ export async function runInDedicatedTerminalCommand(
594596
const uri = item as Uri;
595597
const project = api.getPythonProject(uri);
596598
const environment = await api.getEnvironment(uri);
599+
597600
if (environment && project) {
598-
const terminal = await tm.getDedicatedTerminal(item, project, environment);
599-
await runInTerminal(environment, terminal, {
601+
const resolvedEnv = await api.resolveEnvironment(environment.environmentPath);
602+
const envFinal = resolvedEnv ?? environment;
603+
const terminal = await tm.getDedicatedTerminal(item, project, envFinal);
604+
await runInTerminal(envFinal, terminal, {
600605
cwd: project.uri,
601606
args: [item.fsPath],
602607
show: true,
@@ -612,8 +617,10 @@ export async function runAsTaskCommand(item: unknown, api: PythonEnvironmentApi)
612617
const project = api.getPythonProject(uri);
613618
const environment = await api.getEnvironment(uri);
614619
if (environment) {
620+
const resolvedEnv = await api.resolveEnvironment(environment.environmentPath);
621+
const envFinal = resolvedEnv ?? environment;
615622
return await runAsTask(
616-
environment,
623+
envFinal,
617624
{
618625
project,
619626
args: [item.fsPath],
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)