Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/features/envCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -594,9 +596,12 @@ export async function runInDedicatedTerminalCommand(
const uri = item as Uri;
const project = api.getPythonProject(uri);
const environment = await api.getEnvironment(uri);

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,
Expand All @@ -612,8 +617,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],
Expand Down
4 changes: 2 additions & 2 deletions src/features/execution/execUtils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export function quoteArg(arg: string): string {
export function quoteStringIfNecessary(arg: string): string {
if (arg.indexOf(' ') >= 0 && !(arg.startsWith('"') && arg.endsWith('"'))) {
return `"${arg}"`;
}
return arg;
}

export function quoteArgs(args: string[]): string[] {
return args.map(quoteArg);
return args.map(quoteStringIfNecessary);
}
14 changes: 10 additions & 4 deletions src/features/execution/runAsTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,8 +26,14 @@ export async function runAsTask(
): Promise<TaskExecution> {
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(' ')}`);
Expand Down
12 changes: 9 additions & 3 deletions src/features/execution/runInBackground.ts
Original file line number Diff line number Diff line change
@@ -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<PythonProcess> {
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(' ')}`);
Expand Down
Loading