@@ -17,8 +17,6 @@ import {
1717 getDefaultEnvManagerSetting ,
1818 getDefaultPkgManagerSetting ,
1919 EditProjectSettings ,
20- setAllManagerSettings ,
21- EditAllManagerSettings ,
2220} from './settings/settingHelpers' ;
2321
2422import { getAbsolutePath } from '../common/utils/fileNameUtils' ;
@@ -168,95 +166,62 @@ export async function handlePackagesCommand(
168166 }
169167}
170168
171- export interface EnvironmentSetResult {
172- project ?: PythonProject ;
173- environment : PythonEnvironment ;
174- }
175-
176169export async function setEnvironmentCommand (
177170 context : unknown ,
178171 em : EnvironmentManagers ,
179172 wm : PythonProjectManager ,
180- ) : Promise < EnvironmentSetResult [ ] | undefined > {
173+ ) : Promise < void > {
181174 if ( context instanceof PythonEnvTreeItem ) {
182- const view = context as PythonEnvTreeItem ;
183- const manager = view . parent . manager ;
184- const projects = await pickProjectMany ( wm . getProjects ( ) ) ;
185- if ( projects && projects . length > 0 ) {
186- await Promise . all ( projects . map ( ( p ) => manager . set ( p . uri , view . environment ) ) ) ;
187- await setAllManagerSettings (
188- projects . map ( ( p ) => ( {
189- project : p ,
190- envManager : manager . id ,
191- packageManager : manager . preferredPackageManagerId ,
192- } ) ) ,
193- ) ;
194- return projects . map ( ( p ) => ( { project : p , environment : view . environment } ) ) ;
175+ try {
176+ const view = context as PythonEnvTreeItem ;
177+ const projects = await pickProjectMany ( wm . getProjects ( ) ) ;
178+ if ( projects && projects . length > 0 ) {
179+ const uris = projects . map ( ( p ) => p . uri ) ;
180+ await em . setEnvironments ( uris , view . environment ) ;
181+ }
182+ } catch ( ex ) {
183+ if ( ex === QuickInputButtons . Back ) {
184+ await setEnvironmentCommand ( context , em , wm ) ;
185+ }
186+ throw ex ;
195187 }
196- return ;
197188 } else if ( context instanceof ProjectItem ) {
198189 const view = context as ProjectItem ;
199- return setEnvironmentCommand ( [ view . project . uri ] , em , wm ) ;
190+ await setEnvironmentCommand ( [ view . project . uri ] , em , wm ) ;
200191 } else if ( context instanceof Uri ) {
201- return setEnvironmentCommand ( [ context ] , em , wm ) ;
192+ await setEnvironmentCommand ( [ context ] , em , wm ) ;
202193 } else if ( context === undefined ) {
203- const project = await pickProjectMany ( wm . getProjects ( ) ) ;
204- if ( project && project . length > 0 ) {
205- try {
206- const result = setEnvironmentCommand ( project , em , wm ) ;
207- return result ;
208- } catch ( ex ) {
209- if ( ex === QuickInputButtons . Back ) {
210- return setEnvironmentCommand ( context , em , wm ) ;
211- }
194+ try {
195+ const projects = await pickProjectMany ( wm . getProjects ( ) ) ;
196+ if ( projects && projects . length > 0 ) {
197+ const uris = projects . map ( ( p ) => p . uri ) ;
198+ await setEnvironmentCommand ( uris , em , wm ) ;
199+ }
200+ } catch ( ex ) {
201+ if ( ex === QuickInputButtons . Back ) {
202+ await setEnvironmentCommand ( context , em , wm ) ;
212203 }
204+ throw ex ;
213205 }
214- return ;
215206 } else if ( Array . isArray ( context ) && context . length > 0 && context . every ( ( c ) => c instanceof Uri ) ) {
216207 const uris = context as Uri [ ] ;
217- const projects : PythonProject [ ] = [ ] ;
218- const projectEnvManagers : InternalEnvironmentManager [ ] = [ ] ;
219- uris . forEach ( ( uri ) => {
220- const project = wm . get ( uri ) ;
221- if ( project ) {
222- projects . push ( project ) ;
223- const manager = em . getEnvironmentManager ( uri ) ;
224- if ( manager && ! projectEnvManagers . includes ( manager ) ) {
225- projectEnvManagers . push ( manager ) ;
226- }
227- }
228- } ) ;
229-
208+ const projects = wm . getProjects ( uris ) . map ( ( p ) => p ) ;
209+ const projectEnvManagers = em . getProjectEnvManagers ( uris ) ;
230210 const recommended =
231211 projectEnvManagers . length === 1 && uris . length === 1 ? await projectEnvManagers [ 0 ] . get ( uris [ 0 ] ) : undefined ;
232212 const selected = await pickEnvironment ( em . managers , projectEnvManagers , {
233213 projects,
234214 recommended,
235215 showBackButton : uris . length > 1 ,
236216 } ) ;
237- const manager = em . managers . find ( ( m ) => m . id === selected ?. envId . managerId ) ;
238- if ( selected && manager ) {
239- const promises : Thenable < void > [ ] = [ ] ;
240- const settings : EditAllManagerSettings [ ] = [ ] ;
241- uris . forEach ( ( uri ) => {
242- const m = em . getEnvironmentManager ( uri ) ;
243- promises . push ( manager . set ( uri , selected ) ) ;
244- if ( manager . id !== m ?. id ) {
245- settings . push ( {
246- project : wm . get ( uri ) ,
247- envManager : manager . id ,
248- packageManager : manager . preferredPackageManagerId ,
249- } ) ;
250- }
251- } ) ;
252- await Promise . all ( promises ) ;
253- await setAllManagerSettings ( settings ) ;
254- return projects . map ( ( p ) => ( { project : p , environment : selected } ) ) ;
217+
218+ if ( selected ) {
219+ await em . setEnvironments ( uris , selected ) ;
255220 }
256- return ;
221+ } else {
222+ traceError ( `Invalid context for setting environment command: ${ context } ` ) ;
223+ window . showErrorMessage ( 'Invalid context for setting environment' ) ;
257224 }
258- traceError ( `Invalid context for setting environment command: ${ context } ` ) ;
259- window . showErrorMessage ( 'Invalid context for setting environment' ) ;
260225}
261226
262227export async function resetEnvironmentCommand (
@@ -338,9 +303,17 @@ export async function addPythonProject(
338303 return ;
339304 }
340305
341- let results = await creator . create ( ) ;
342- if ( results === undefined ) {
343- return ;
306+ let results : PythonProject | PythonProject [ ] | undefined ;
307+ try {
308+ results = await creator . create ( ) ;
309+ if ( results === undefined ) {
310+ return ;
311+ }
312+ } catch ( ex : any ) {
313+ if ( ex === QuickInputButtons . Back ) {
314+ return addPythonProject ( resource , wm , em , pc ) ;
315+ }
316+ throw ex ;
344317 }
345318
346319 if ( ! Array . isArray ( results ) ) {
@@ -435,21 +408,21 @@ export async function createTerminalCommand(
435408 const env = await api . getEnvironment ( uri ) ;
436409 const pw = api . getPythonProject ( uri ) ;
437410 if ( env && pw ) {
438- return await tm . create ( env , pw . uri ) ;
411+ return await tm . create ( env , { cwd : pw . uri } ) ;
439412 }
440413 } else if ( context instanceof ProjectItem ) {
441414 const view = context as ProjectItem ;
442415 const env = await api . getEnvironment ( view . project . uri ) ;
443416 if ( env ) {
444- const terminal = await tm . create ( env , view . project . uri ) ;
417+ const terminal = await tm . create ( env , { cwd : view . project . uri } ) ;
445418 terminal . show ( ) ;
446419 return terminal ;
447420 }
448421 } else if ( context instanceof PythonEnvTreeItem ) {
449422 const view = context as PythonEnvTreeItem ;
450423 const pw = await pickProject ( api . getPythonProjects ( ) ) ;
451424 if ( pw ) {
452- const terminal = await tm . create ( view . environment , pw . uri ) ;
425+ const terminal = await tm . create ( view . environment , { cwd : pw . uri } ) ;
453426 terminal . show ( ) ;
454427 return terminal ;
455428 }
0 commit comments