diff --git a/src/features/envCommands.ts b/src/features/envCommands.ts index 9ec9d7b9..6267b4d6 100644 --- a/src/features/envCommands.ts +++ b/src/features/envCommands.ts @@ -3,6 +3,7 @@ import { CreateEnvironmentOptions, PythonEnvironment, PythonEnvironmentApi, + PythonProject, PythonProjectCreator, PythonProjectCreatorOptions, } from '../api'; @@ -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'; @@ -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); @@ -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,