Skip to content
2 changes: 1 addition & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@
"python-envs.revealProjectInExplorer.title": "Reveal Project in Explorer",
"python-envs.revealEnvInManagerView.title": "Reveal in Environment Managers View",
"python-envs.runPetInTerminal.title": "Run Python Environment Tool (PET) in Terminal...",
"python-envs.alwaysUseUv.description": "When set to true, uv will be used to manage all virtual environments if available. When set to false, uv will only manage virtual environments explicitly created by uv."
"python-envs.alwaysUseUv.description": "When set to true, uv will be used to manage all virtual environments if available, and the run button will execute files with uv run. When set to false, uv will only manage environments explicitly created by uv, including for the run button."
}
13 changes: 11 additions & 2 deletions src/features/execution/runAsTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { PythonEnvironment, PythonTaskExecutionOptions } from '../../api';
import { traceInfo, traceWarn } from '../../common/logging';
import { executeTask } from '../../common/tasks.apis';
import { getWorkspaceFolder } from '../../common/workspace.apis';
import { shouldUseUv } from '../../managers/builtin/helpers';
import { quoteStringIfNecessary } from './execUtils';

function getWorkspaceFolderOrDefault(uri?: Uri): WorkspaceFolder | TaskScope {
Expand All @@ -31,11 +32,19 @@ export async function runAsTask(
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];
const useUv = await shouldUseUv(undefined, environment.environmentPath.fsPath, options.project?.uri);

if (useUv) {
allArgs.unshift('--python', executable);
allArgs.unshift('run');
executable = 'uv';
Comment thread
eleanorjboyd marked this conversation as resolved.
}

// Check and quote the executable path if necessary
executable = quoteStringIfNecessary(executable);
traceInfo(`Running as task: ${executable} ${allArgs.join(' ')}`);

const task = new Task(
Expand Down
6 changes: 3 additions & 3 deletions src/managers/builtin/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CancellationError, CancellationToken, LogOutputChannel } from 'vscode';
import { CancellationError, CancellationToken, ConfigurationScope, LogOutputChannel } from 'vscode';
import { spawnProcess } from '../../common/childProcess.apis';
import { EventNames } from '../../common/telemetry/constants';
import { sendTelemetryEvent } from '../../common/telemetry/sender';
Expand Down Expand Up @@ -40,7 +40,7 @@ export async function isUvInstalled(log?: LogOutputChannel): Promise<boolean> {
* @param envPath - Optional environment path to check against UV environments list
* @returns True if uv should be used, false otherwise. For UV environments, returns true if uv is installed. For other environments, checks the 'python-envs.alwaysUseUv' setting and uv availability.
*/
export async function shouldUseUv(log?: LogOutputChannel, envPath?: string): Promise<boolean> {
export async function shouldUseUv(log?: LogOutputChannel, envPath?: string, scope?: ConfigurationScope): Promise<boolean> {
if (envPath) {
Comment thread
eleanorjboyd marked this conversation as resolved.
// always use uv if the given environment is stored as a uv env
const uvEnvs = await getUvEnvironments();
Expand All @@ -50,7 +50,7 @@ export async function shouldUseUv(log?: LogOutputChannel, envPath?: string): Pro
}

// For other environments, check the user setting
const config = getConfiguration('python-envs');
const config = getConfiguration('python-envs', scope);
const alwaysUseUv = config.get<boolean>('alwaysUseUv', true);

if (alwaysUseUv) {
Expand Down
Loading
Loading