@@ -33,6 +33,26 @@ import { resolveSystemPythonEnvironmentPath } from './utils';
3333export const VENV_WORKSPACE_KEY = `${ ENVS_EXTENSION_ID } :venv:WORKSPACE_SELECTED` ;
3434export const VENV_GLOBAL_KEY = `${ ENVS_EXTENSION_ID } :venv:GLOBAL_SELECTED` ;
3535
36+ /**
37+ * Result of environment creation operation.
38+ */
39+ export interface CreateEnvironmentResult {
40+ /**
41+ * The created environment, if successful.
42+ */
43+ environment ?: PythonEnvironment ;
44+
45+ /*
46+ * Exists if error occurred during environment creation and includes error explanation.
47+ */
48+ envCreationErr ?: string ;
49+
50+ /*
51+ * Exists if error occurred while installing packages and includes error description.
52+ */
53+ pkgInstallationErr ?: string ;
54+ }
55+
3656export async function clearVenvCache ( ) : Promise < void > {
3757 const keys = [ VENV_WORKSPACE_KEY , VENV_GLOBAL_KEY ] ;
3858 const state = await getWorkspacePersistentState ( ) ;
@@ -281,7 +301,7 @@ async function createWithProgress(
281301 venvRoot : Uri ,
282302 envPath : string ,
283303 packages ?: PipPackages ,
284- ) {
304+ ) : Promise < CreateEnvironmentResult | undefined > {
285305 const pythonPath =
286306 os . platform ( ) === 'win32' ? path . join ( envPath , 'Scripts' , 'python.exe' ) : path . join ( envPath , 'bin' , 'python' ) ;
287307
@@ -295,8 +315,10 @@ async function createWithProgress(
295315 ) ,
296316 } ,
297317 async ( ) => {
318+ const result : CreateEnvironmentResult = { } ;
298319 try {
299320 const useUv = await isUvInstalled ( log ) ;
321+ // env creation
300322 if ( basePython . execInfo ?. run . executable ) {
301323 if ( useUv ) {
302324 await runUV (
@@ -313,26 +335,33 @@ async function createWithProgress(
313335 ) ;
314336 }
315337 if ( ! ( await fsapi . pathExists ( pythonPath ) ) ) {
316- log . error ( 'no python executable found in virtual environment' ) ;
317338 throw new Error ( 'no python executable found in virtual environment' ) ;
318339 }
319340 }
320341
342+ // handle admin of new env
321343 const resolved = await nativeFinder . resolve ( pythonPath ) ;
322344 const env = api . createPythonEnvironmentItem ( await getPythonInfo ( resolved ) , manager ) ;
345+
346+ // install packages
323347 if ( packages && ( packages . install . length > 0 || packages . uninstall . length > 0 ) ) {
324- await api . managePackages ( env , {
325- upgrade : false ,
326- install : packages ?. install ,
327- uninstall : packages ?. uninstall ?? [ ] ,
328- } ) ;
348+ try {
349+ await api . managePackages ( env , {
350+ upgrade : false ,
351+ install : packages ?. install ,
352+ uninstall : packages ?. uninstall ?? [ ] ,
353+ } ) ;
354+ } catch ( e ) {
355+ // error occurred while installing packages
356+ result . pkgInstallationErr = e instanceof Error ? e . message : String ( e ) ;
357+ }
329358 }
330- return env ;
359+ result . environment = env ;
331360 } catch ( e ) {
332361 log . error ( `Failed to create virtual environment: ${ e } ` ) ;
333- showErrorMessage ( VenvManagerStrings . venvCreateFailed ) ;
334- return ;
362+ result . envCreationErr = `Failed to create virtual environment: ${ e } ` ;
335363 }
364+ return result ;
336365 } ,
337366 ) ;
338367}
@@ -365,7 +394,7 @@ export async function quickCreateVenv(
365394 baseEnv : PythonEnvironment ,
366395 venvRoot : Uri ,
367396 additionalPackages ?: string [ ] ,
368- ) : Promise < PythonEnvironment | undefined > {
397+ ) : Promise < CreateEnvironmentResult | undefined > {
369398 const project = api . getPythonProject ( venvRoot ) ;
370399
371400 sendTelemetryEvent ( EventNames . VENV_CREATION , undefined , { creationType : 'quick' } ) ;
@@ -387,6 +416,7 @@ export async function quickCreateVenv(
387416 venvPath = `${ venvPath } -${ i } ` ;
388417 }
389418
419+ // createWithProgress handles building CreateEnvironmentResult and adding err msgs
390420 return await createWithProgress ( nativeFinder , api , log , manager , baseEnv , venvRoot , venvPath , {
391421 install : allPackages ,
392422 uninstall : [ ] ,
@@ -401,7 +431,7 @@ export async function createPythonVenv(
401431 basePythons : PythonEnvironment [ ] ,
402432 venvRoot : Uri ,
403433 options : { showQuickAndCustomOptions : boolean ; additionalPackages ?: string [ ] } ,
404- ) : Promise < PythonEnvironment | undefined > {
434+ ) : Promise < CreateEnvironmentResult | undefined > {
405435 const sortedEnvs = ensureGlobalEnv ( basePythons , log ) ;
406436
407437 let customize : boolean | undefined = true ;
@@ -421,7 +451,9 @@ export async function createPythonVenv(
421451 const basePython = await pickEnvironmentFrom ( sortedEnvs ) ;
422452 if ( ! basePython || ! basePython . execInfo ) {
423453 log . error ( 'No base python selected, cannot create virtual environment.' ) ;
424- return ;
454+ return {
455+ envCreationErr : 'No base python selected, cannot create virtual environment.' ,
456+ } ;
425457 }
426458
427459 const name = await showInputBox ( {
@@ -439,7 +471,9 @@ export async function createPythonVenv(
439471 } ) ;
440472 if ( ! name ) {
441473 log . error ( 'No name entered, cannot create virtual environment.' ) ;
442- return ;
474+ return {
475+ envCreationErr : 'No name entered, cannot create virtual environment.' ,
476+ } ;
443477 }
444478
445479 const envPath = path . join ( venvRoot . fsPath , name ) ;
0 commit comments