@@ -17,8 +17,14 @@ import { MultiStepAction, MultiStepNode, withProgress } from '../../../common/vs
17
17
import { sendTelemetryEvent } from '../../../telemetry' ;
18
18
import { EventName } from '../../../telemetry/constants' ;
19
19
import { VenvProgressAndTelemetry , VENV_CREATED_MARKER , VENV_EXISTING_MARKER } from './venvProgressAndTelemetry' ;
20
- import { showErrorMessageWithLogs } from '../common/commonUtils' ;
21
- import { IPackageInstallSelection , pickPackagesToInstall } from './venvUtils' ;
20
+ import { getVenvExecutable , showErrorMessageWithLogs } from '../common/commonUtils' ;
21
+ import {
22
+ ExistingVenvAction ,
23
+ IPackageInstallSelection ,
24
+ deleteEnvironment ,
25
+ pickExistingVenvAction ,
26
+ pickPackagesToInstall ,
27
+ } from './venvUtils' ;
22
28
import { InputFlowAction } from '../../../common/utils/multiStepInput' ;
23
29
import {
24
30
CreateEnvironmentProvider ,
@@ -150,33 +156,66 @@ export class VenvCreationProvider implements CreateEnvironmentProvider {
150
156
undefined ,
151
157
) ;
152
158
153
- let interpreter : string | undefined ;
154
- const interpreterStep = new MultiStepNode (
159
+ let existingVenvAction : ExistingVenvAction | undefined ;
160
+ const existingEnvStep = new MultiStepNode (
155
161
workspaceStep ,
156
- async ( ) => {
157
- if ( workspace ) {
162
+ async ( context ?: MultiStepAction ) => {
163
+ if ( workspace && context === MultiStepAction . Continue ) {
158
164
try {
159
- interpreter = await this . interpreterQuickPick . getInterpreterViaQuickPick (
160
- workspace . uri ,
161
- ( i : PythonEnvironment ) =>
162
- [
163
- EnvironmentType . System ,
164
- EnvironmentType . MicrosoftStore ,
165
- EnvironmentType . Global ,
166
- EnvironmentType . Pyenv ,
167
- ] . includes ( i . envType ) && i . type === undefined , // only global intepreters
168
- {
169
- skipRecommended : true ,
170
- showBackButton : true ,
171
- placeholder : CreateEnv . Venv . selectPythonPlaceHolder ,
172
- title : null ,
173
- } ,
174
- ) ;
165
+ existingVenvAction = await pickExistingVenvAction ( workspace ) ;
166
+ return MultiStepAction . Continue ;
175
167
} catch ( ex ) {
176
- if ( ex === InputFlowAction . back ) {
168
+ if ( ex === MultiStepAction . Back || ex === MultiStepAction . Cancel ) {
169
+ return ex ;
170
+ }
171
+ throw ex ;
172
+ }
173
+ } else if ( context === MultiStepAction . Back ) {
174
+ return MultiStepAction . Back ;
175
+ }
176
+ return MultiStepAction . Continue ;
177
+ } ,
178
+ undefined ,
179
+ ) ;
180
+ workspaceStep . next = existingEnvStep ;
181
+
182
+ let interpreter : string | undefined ;
183
+ const interpreterStep = new MultiStepNode (
184
+ existingEnvStep ,
185
+ async ( context ?: MultiStepAction ) => {
186
+ if ( workspace ) {
187
+ if (
188
+ existingVenvAction === ExistingVenvAction . Recreate ||
189
+ existingVenvAction === ExistingVenvAction . Create
190
+ ) {
191
+ try {
192
+ interpreter = await this . interpreterQuickPick . getInterpreterViaQuickPick (
193
+ workspace . uri ,
194
+ ( i : PythonEnvironment ) =>
195
+ [
196
+ EnvironmentType . System ,
197
+ EnvironmentType . MicrosoftStore ,
198
+ EnvironmentType . Global ,
199
+ EnvironmentType . Pyenv ,
200
+ ] . includes ( i . envType ) && i . type === undefined , // only global intepreters
201
+ {
202
+ skipRecommended : true ,
203
+ showBackButton : true ,
204
+ placeholder : CreateEnv . Venv . selectPythonPlaceHolder ,
205
+ title : null ,
206
+ } ,
207
+ ) ;
208
+ } catch ( ex ) {
209
+ if ( ex === InputFlowAction . back ) {
210
+ return MultiStepAction . Back ;
211
+ }
212
+ interpreter = undefined ;
213
+ }
214
+ } else if ( existingVenvAction === ExistingVenvAction . UseExisting ) {
215
+ if ( context === MultiStepAction . Back ) {
177
216
return MultiStepAction . Back ;
178
217
}
179
- interpreter = undefined ;
218
+ interpreter = getVenvExecutable ( workspace ) ;
180
219
}
181
220
}
182
221
@@ -189,7 +228,7 @@ export class VenvCreationProvider implements CreateEnvironmentProvider {
189
228
} ,
190
229
undefined ,
191
230
) ;
192
- workspaceStep . next = interpreterStep ;
231
+ existingEnvStep . next = interpreterStep ;
193
232
194
233
let addGitIgnore = true ;
195
234
let installPackages = true ;
@@ -200,19 +239,23 @@ export class VenvCreationProvider implements CreateEnvironmentProvider {
200
239
let installInfo : IPackageInstallSelection [ ] | undefined ;
201
240
const packagesStep = new MultiStepNode (
202
241
interpreterStep ,
203
- async ( ) => {
242
+ async ( context ?: MultiStepAction ) => {
204
243
if ( workspace && installPackages ) {
205
- try {
206
- installInfo = await pickPackagesToInstall ( workspace ) ;
207
- } catch ( ex ) {
208
- if ( ex === MultiStepAction . Back || ex === MultiStepAction . Cancel ) {
209
- return ex ;
244
+ if ( existingVenvAction !== ExistingVenvAction . UseExisting ) {
245
+ try {
246
+ installInfo = await pickPackagesToInstall ( workspace ) ;
247
+ } catch ( ex ) {
248
+ if ( ex === MultiStepAction . Back || ex === MultiStepAction . Cancel ) {
249
+ return ex ;
250
+ }
251
+ throw ex ;
210
252
}
211
- throw ex ;
212
- }
213
- if ( ! installInfo ) {
214
- traceVerbose ( 'Virtual env creation exited during dependencies selection.' ) ;
215
- return MultiStepAction . Cancel ;
253
+ if ( ! installInfo ) {
254
+ traceVerbose ( 'Virtual env creation exited during dependencies selection.' ) ;
255
+ return MultiStepAction . Cancel ;
256
+ }
257
+ } else if ( context === MultiStepAction . Back ) {
258
+ return MultiStepAction . Back ;
216
259
}
217
260
}
218
261
@@ -227,6 +270,32 @@ export class VenvCreationProvider implements CreateEnvironmentProvider {
227
270
throw action ;
228
271
}
229
272
273
+ if ( workspace ) {
274
+ if ( existingVenvAction === ExistingVenvAction . Recreate ) {
275
+ sendTelemetryEvent ( EventName . ENVIRONMENT_DELETE , undefined , {
276
+ environmentType : 'venv' ,
277
+ status : 'triggered' ,
278
+ } ) ;
279
+ if ( await deleteEnvironment ( workspace , interpreter ) ) {
280
+ sendTelemetryEvent ( EventName . ENVIRONMENT_DELETE , undefined , {
281
+ environmentType : 'venv' ,
282
+ status : 'deleted' ,
283
+ } ) ;
284
+ } else {
285
+ sendTelemetryEvent ( EventName . ENVIRONMENT_DELETE , undefined , {
286
+ environmentType : 'venv' ,
287
+ status : 'failed' ,
288
+ } ) ;
289
+ throw MultiStepAction . Cancel ;
290
+ }
291
+ } else if ( existingVenvAction === ExistingVenvAction . UseExisting ) {
292
+ sendTelemetryEvent ( EventName . ENVIRONMENT_REUSE , undefined , {
293
+ environmentType : 'venv' ,
294
+ } ) ;
295
+ return { path : getVenvExecutable ( workspace ) , workspaceFolder : workspace } ;
296
+ }
297
+ }
298
+
230
299
const args = generateCommandArgs ( installInfo , addGitIgnore ) ;
231
300
232
301
return withProgress (
0 commit comments