Skip to content
Merged
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
43 changes: 39 additions & 4 deletions src/features/envCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
CreateEnvironmentOptions,
PythonEnvironment,
PythonEnvironmentApi,
PythonProject,
PythonProjectCreator,
PythonProjectCreatorOptions,
} from '../api';
Expand All @@ -21,7 +22,7 @@ import {} from '../common/errors/utils';
import { pickEnvironment } from '../common/pickers/environments';
import { pickCreator, pickEnvironmentManager, pickPackageManager } from '../common/pickers/managers';
import { pickProject, pickProjectMany } from '../common/pickers/projects';
import { activeTextEditor, showErrorMessage } from '../common/window.apis';
import { activeTextEditor, showErrorMessage, showInformationMessage } from '../common/window.apis';
import { quoteArgs } from './execution/execUtils';
import { runAsTask } from './execution/runAsTask';
import { runInTerminal } from './terminal/runInTerminal';
Expand Down Expand Up @@ -212,8 +213,8 @@ export async function setEnvironmentCommand(
if (projects.length > 0) {
const selected = await pickProjectMany(projects);
if (selected && selected.length > 0) {
const uris = selected.map((p) => p.uri);
await em.setEnvironments(uris, view.environment);
// Check if the selected environment is already the current one for each project
await setEnvironmentForProjects(selected, context.environment, em);
}
} else {
await em.setEnvironments('global', view.environment);
Expand Down Expand Up @@ -271,13 +272,47 @@ export async function setEnvironmentCommand(
});

if (selected) {
await em.setEnvironments(uris, selected);
// Use the same logic for checking already set environments
await setEnvironmentForProjects(projects, selected, em);
}
} else {
traceError(`Invalid context for setting environment command: ${context}`);
showErrorMessage('Invalid context for setting environment');
}
}
/**
* Sets the environment for the given projects, showing a warning for those already set.
* @param selectedProjects Array of PythonProject selected by user
* @param environment The environment to set for the projects
* @param em The EnvironmentManagers instance
*/
async function setEnvironmentForProjects(
selectedProjects: PythonProject[],
environment: PythonEnvironment,
em: EnvironmentManagers,
) {
let alreadySet: PythonProject[] = [];
for (const p of selectedProjects) {
const currentEnv = await em.getEnvironment(p.uri);
if (currentEnv?.envId.id === environment.envId.id) {
alreadySet.push(p);
}
}
if (alreadySet.length > 0) {
const env = alreadySet.length > 1 ? 'environments' : 'environment';
showInformationMessage(
`"${environment.name}" is already selected as the ${env} for: ${alreadySet
.map((p) => `"${p.name}"`)
.join(', ')}`,
);
}
const toSet: PythonProject[] = selectedProjects.filter((p) => !alreadySet.includes(p));
const uris = toSet.map((p) => p.uri);
if (uris.length === 0) {
return;
}
await em.setEnvironments(uris, environment);
}

export async function resetEnvironmentCommand(
context: unknown,
Expand Down