From 3d362a3e256fac1237b31b925df962791cb7cf8f Mon Sep 17 00:00:00 2001 From: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com> Date: Mon, 18 Aug 2025 15:02:38 -0700 Subject: [PATCH 1/3] add quoting and logging for run as task and background --- src/features/execution/execUtils.ts | 4 ++-- src/features/execution/runAsTask.ts | 14 ++++++++++---- src/features/execution/runInBackground.ts | 12 +++++++++--- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/features/execution/execUtils.ts b/src/features/execution/execUtils.ts index 7c61975b..49409ef7 100644 --- a/src/features/execution/execUtils.ts +++ b/src/features/execution/execUtils.ts @@ -1,4 +1,4 @@ -export function quoteArg(arg: string): string { +export function quoteStringIfNecessary(arg: string): string { if (arg.indexOf(' ') >= 0 && !(arg.startsWith('"') && arg.endsWith('"'))) { return `"${arg}"`; } @@ -6,5 +6,5 @@ export function quoteArg(arg: string): string { } export function quoteArgs(args: string[]): string[] { - return args.map(quoteArg); + return args.map(quoteStringIfNecessary); } diff --git a/src/features/execution/runAsTask.ts b/src/features/execution/runAsTask.ts index 5d020d71..6b7e14e4 100644 --- a/src/features/execution/runAsTask.ts +++ b/src/features/execution/runAsTask.ts @@ -9,10 +9,10 @@ import { WorkspaceFolder, } from 'vscode'; import { PythonEnvironment, PythonTaskExecutionOptions } from '../../api'; -import { traceInfo } from '../../common/logging'; +import { traceInfo, traceWarn } from '../../common/logging'; import { executeTask } from '../../common/tasks.apis'; import { getWorkspaceFolder } from '../../common/workspace.apis'; -import { quoteArg } from './execUtils'; +import { quoteStringIfNecessary } from './execUtils'; function getWorkspaceFolderOrDefault(uri?: Uri): WorkspaceFolder | TaskScope { const workspace = uri ? getWorkspaceFolder(uri) : undefined; @@ -26,8 +26,14 @@ export async function runAsTask( ): Promise { const workspace: WorkspaceFolder | TaskScope = getWorkspaceFolderOrDefault(options.project?.uri); - let executable = environment.execInfo?.activatedRun?.executable ?? environment.execInfo?.run.executable ?? 'python'; - executable = quoteArg(executable); + let executable = environment.execInfo?.activatedRun?.executable ?? environment.execInfo?.run.executable; + if (!executable) { + traceWarn('No Python executable found in environment; falling back to "python".'); + executable = 'python'; + } + // Check and quote the executable path if necessary + executable = quoteStringIfNecessary(executable); + const args = environment.execInfo?.activatedRun?.args ?? environment.execInfo?.run.args ?? []; const allArgs = [...args, ...options.args]; traceInfo(`Running as task: ${executable} ${allArgs.join(' ')}`); diff --git a/src/features/execution/runInBackground.ts b/src/features/execution/runInBackground.ts index aff7c4ce..0543bc34 100644 --- a/src/features/execution/runInBackground.ts +++ b/src/features/execution/runInBackground.ts @@ -1,13 +1,19 @@ import * as cp from 'child_process'; import { PythonBackgroundRunOptions, PythonEnvironment, PythonProcess } from '../../api'; -import { traceError, traceInfo } from '../../common/logging'; +import { traceError, traceInfo, traceWarn } from '../../common/logging'; +import { quoteStringIfNecessary } from './execUtils'; export async function runInBackground( environment: PythonEnvironment, options: PythonBackgroundRunOptions, ): Promise { - const executable = - environment.execInfo?.activatedRun?.executable ?? environment.execInfo?.run.executable ?? 'python'; + let executable = environment.execInfo?.activatedRun?.executable ?? environment.execInfo?.run.executable; + if (!executable) { + traceWarn('No Python executable found in environment; falling back to "python".'); + executable = 'python'; + } + // Check and quote the executable path if necessary + executable = quoteStringIfNecessary(executable); const args = environment.execInfo?.activatedRun?.args ?? environment.execInfo?.run.args ?? []; const allArgs = [...args, ...options.args]; traceInfo(`Running in background: ${executable} ${allArgs.join(' ')}`); From 94badeb514766b48759ec6b1e078475ea9ff248e Mon Sep 17 00:00:00 2001 From: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com> Date: Mon, 18 Aug 2025 15:13:45 -0700 Subject: [PATCH 2/3] add resolution attempt --- src/features/envCommands.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/features/envCommands.ts b/src/features/envCommands.ts index ecdcc9bf..e5e1f563 100644 --- a/src/features/envCommands.ts +++ b/src/features/envCommands.ts @@ -574,8 +574,10 @@ export async function runInTerminalCommand( const project = api.getPythonProject(uri); const environment = await api.getEnvironment(uri); if (environment && project) { - const terminal = await tm.getProjectTerminal(project, environment); - await runInTerminal(environment, terminal, { + const resolvedEnv = await api.resolveEnvironment(environment.environmentPath); + const envFinal = resolvedEnv ?? environment; + const terminal = await tm.getProjectTerminal(project, envFinal); + await runInTerminal(envFinal, terminal, { cwd: project.uri, args: [item.fsPath], show: true, @@ -594,9 +596,13 @@ export async function runInDedicatedTerminalCommand( const uri = item as Uri; const project = api.getPythonProject(uri); const environment = await api.getEnvironment(uri); + + // add resolution here if (environment && project) { - const terminal = await tm.getDedicatedTerminal(item, project, environment); - await runInTerminal(environment, terminal, { + const resolvedEnv = await api.resolveEnvironment(environment.environmentPath); + const envFinal = resolvedEnv ?? environment; + const terminal = await tm.getDedicatedTerminal(item, project, envFinal); + await runInTerminal(envFinal, terminal, { cwd: project.uri, args: [item.fsPath], show: true, @@ -612,8 +618,10 @@ export async function runAsTaskCommand(item: unknown, api: PythonEnvironmentApi) const project = api.getPythonProject(uri); const environment = await api.getEnvironment(uri); if (environment) { + const resolvedEnv = await api.resolveEnvironment(environment.environmentPath); + const envFinal = resolvedEnv ?? environment; return await runAsTask( - environment, + envFinal, { project, args: [item.fsPath], @@ -624,6 +632,7 @@ export async function runAsTaskCommand(item: unknown, api: PythonEnvironmentApi) ); } } else if (item === undefined) { + // If no context is provided, try to use the active text editor const uri = activeTextEditor()?.document.uri; if (uri) { return runAsTaskCommand(uri, api); From 7a2d87c502905e445418785ac5a6af18dcdee6c9 Mon Sep 17 00:00:00 2001 From: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com> Date: Mon, 18 Aug 2025 15:14:55 -0700 Subject: [PATCH 3/3] remove comments --- src/features/envCommands.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/features/envCommands.ts b/src/features/envCommands.ts index e5e1f563..8f3d1d6c 100644 --- a/src/features/envCommands.ts +++ b/src/features/envCommands.ts @@ -597,7 +597,6 @@ export async function runInDedicatedTerminalCommand( const project = api.getPythonProject(uri); const environment = await api.getEnvironment(uri); - // add resolution here if (environment && project) { const resolvedEnv = await api.resolveEnvironment(environment.environmentPath); const envFinal = resolvedEnv ?? environment; @@ -632,7 +631,6 @@ export async function runAsTaskCommand(item: unknown, api: PythonEnvironmentApi) ); } } else if (item === undefined) { - // If no context is provided, try to use the active text editor const uri = activeTextEditor()?.document.uri; if (uri) { return runAsTaskCommand(uri, api);