11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT License.
33
4- import { CancellationToken , ProgressLocation , WorkspaceFolder } from 'vscode' ;
4+ import { CancellationToken , CancellationTokenSource , ProgressLocation , WorkspaceFolder } from 'vscode' ;
55import * as path from 'path' ;
66import { Commands , PVSC_EXTENSION_ID } from '../../../common/constants' ;
77import { traceError , traceInfo , traceLog } from '../../../logging' ;
@@ -35,6 +35,8 @@ import {
3535 CreateEnvironmentResult ,
3636 CreateEnvironmentProvider ,
3737} from '../proposed.createEnvApis' ;
38+ import { shouldDisplayEnvCreationProgress } from './hideEnvCreation' ;
39+ import { noop } from '../../../common/utils/misc' ;
3840
3941function generateCommandArgs ( version ?: string , options ?: CreateEnvironmentOptions ) : string [ ] {
4042 let addGitIgnore = true ;
@@ -261,6 +263,50 @@ async function createEnvironment(options?: CreateEnvironmentOptions): Promise<Cr
261263 }
262264 }
263265
266+ const createEnvInternal = async ( progress : CreateEnvironmentProgress , token : CancellationToken ) => {
267+ progress . report ( {
268+ message : CreateEnv . statusStarting ,
269+ } ) ;
270+
271+ let envPath : string | undefined ;
272+ try {
273+ sendTelemetryEvent ( EventName . ENVIRONMENT_CREATING , undefined , {
274+ environmentType : 'conda' ,
275+ pythonVersion : version ,
276+ } ) ;
277+ if ( workspace ) {
278+ envPath = await createCondaEnv (
279+ workspace ,
280+ getExecutableCommand ( conda ) ,
281+ generateCommandArgs ( version , options ) ,
282+ progress ,
283+ token ,
284+ ) ;
285+
286+ if ( envPath ) {
287+ return { path : envPath , workspaceFolder : workspace } ;
288+ }
289+
290+ throw new Error ( 'Failed to create conda environment. See Output > Python for more info.' ) ;
291+ } else {
292+ throw new Error ( 'A workspace is needed to create conda environment' ) ;
293+ }
294+ } catch ( ex ) {
295+ traceError ( ex ) ;
296+ showErrorMessageWithLogs ( CreateEnv . Conda . errorCreatingEnvironment ) ;
297+ return { error : ex as Error } ;
298+ }
299+ } ;
300+
301+ if ( ! shouldDisplayEnvCreationProgress ( ) ) {
302+ const token = new CancellationTokenSource ( ) ;
303+ try {
304+ return await createEnvInternal ( { report : noop } , token . token ) ;
305+ } finally {
306+ token . dispose ( ) ;
307+ }
308+ }
309+
264310 return withProgress (
265311 {
266312 location : ProgressLocation . Notification ,
@@ -270,40 +316,7 @@ async function createEnvironment(options?: CreateEnvironmentOptions): Promise<Cr
270316 async (
271317 progress : CreateEnvironmentProgress ,
272318 token : CancellationToken ,
273- ) : Promise < CreateEnvironmentResult | undefined > => {
274- progress . report ( {
275- message : CreateEnv . statusStarting ,
276- } ) ;
277-
278- let envPath : string | undefined ;
279- try {
280- sendTelemetryEvent ( EventName . ENVIRONMENT_CREATING , undefined , {
281- environmentType : 'conda' ,
282- pythonVersion : version ,
283- } ) ;
284- if ( workspace ) {
285- envPath = await createCondaEnv (
286- workspace ,
287- getExecutableCommand ( conda ) ,
288- generateCommandArgs ( version , options ) ,
289- progress ,
290- token ,
291- ) ;
292-
293- if ( envPath ) {
294- return { path : envPath , workspaceFolder : workspace } ;
295- }
296-
297- throw new Error ( 'Failed to create conda environment. See Output > Python for more info.' ) ;
298- } else {
299- throw new Error ( 'A workspace is needed to create conda environment' ) ;
300- }
301- } catch ( ex ) {
302- traceError ( ex ) ;
303- showErrorMessageWithLogs ( CreateEnv . Conda . errorCreatingEnvironment ) ;
304- return { error : ex as Error } ;
305- }
306- } ,
319+ ) : Promise < CreateEnvironmentResult | undefined > => createEnvInternal ( progress , token ) ,
307320 ) ;
308321}
309322
0 commit comments