3
3
4
4
import * as path from 'path' ;
5
5
import { inject , injectable } from 'inversify' ;
6
- import { ProgressOptions , ProgressLocation , MarkdownString , WorkspaceFolder } from 'vscode' ;
6
+ import {
7
+ ProgressOptions ,
8
+ ProgressLocation ,
9
+ MarkdownString ,
10
+ WorkspaceFolder ,
11
+ GlobalEnvironmentVariableCollection ,
12
+ EnvironmentVariableScope ,
13
+ } from 'vscode' ;
7
14
import { pathExists } from 'fs-extra' ;
8
15
import { IExtensionActivationService } from '../../activation/types' ;
9
16
import { IApplicationShell , IApplicationEnvironment , IWorkspaceService } from '../../common/application/types' ;
@@ -20,7 +27,7 @@ import {
20
27
} from '../../common/types' ;
21
28
import { Deferred , createDeferred } from '../../common/utils/async' ;
22
29
import { Interpreters } from '../../common/utils/localize' ;
23
- import { traceDecoratorVerbose , traceVerbose , traceWarn } from '../../logging' ;
30
+ import { traceDecoratorVerbose , traceError , traceVerbose , traceWarn } from '../../logging' ;
24
31
import { IInterpreterService } from '../contracts' ;
25
32
import { defaultShells } from './service' ;
26
33
import { IEnvironmentActivationService , ITerminalEnvVarCollectionService } from './types' ;
@@ -61,52 +68,56 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
61
68
) { }
62
69
63
70
public async activate ( resource : Resource ) : Promise < void > {
64
- if ( ! inTerminalEnvVarExperiment ( this . experimentService ) ) {
65
- this . context . environmentVariableCollection . clear ( ) ;
66
- await this . handleMicroVenv ( resource ) ;
71
+ try {
72
+ if ( ! inTerminalEnvVarExperiment ( this . experimentService ) ) {
73
+ this . context . environmentVariableCollection . clear ( ) ;
74
+ await this . handleMicroVenv ( resource ) ;
75
+ if ( ! this . registeredOnce ) {
76
+ this . interpreterService . onDidChangeInterpreter (
77
+ async ( r ) => {
78
+ await this . handleMicroVenv ( r ) ;
79
+ } ,
80
+ this ,
81
+ this . disposables ,
82
+ ) ;
83
+ this . registeredOnce = true ;
84
+ }
85
+ return ;
86
+ }
67
87
if ( ! this . registeredOnce ) {
68
88
this . interpreterService . onDidChangeInterpreter (
69
89
async ( r ) => {
70
- await this . handleMicroVenv ( r ) ;
90
+ this . showProgress ( ) ;
91
+ await this . _applyCollection ( r ) . ignoreErrors ( ) ;
92
+ this . hideProgress ( ) ;
93
+ } ,
94
+ this ,
95
+ this . disposables ,
96
+ ) ;
97
+ this . applicationEnvironment . onDidChangeShell (
98
+ async ( shell : string ) => {
99
+ this . showProgress ( ) ;
100
+ this . processEnvVars = undefined ;
101
+ // Pass in the shell where known instead of relying on the application environment, because of bug
102
+ // on VSCode: https://github.com/microsoft/vscode/issues/160694
103
+ await this . _applyCollection ( undefined , shell ) . ignoreErrors ( ) ;
104
+ this . hideProgress ( ) ;
71
105
} ,
72
106
this ,
73
107
this . disposables ,
74
108
) ;
75
109
this . registeredOnce = true ;
76
110
}
77
- return ;
78
- }
79
- if ( ! this . registeredOnce ) {
80
- this . interpreterService . onDidChangeInterpreter (
81
- async ( r ) => {
82
- this . showProgress ( ) ;
83
- await this . _applyCollection ( r ) . ignoreErrors ( ) ;
84
- this . hideProgress ( ) ;
85
- } ,
86
- this ,
87
- this . disposables ,
88
- ) ;
89
- this . applicationEnvironment . onDidChangeShell (
90
- async ( shell : string ) => {
91
- this . showProgress ( ) ;
92
- this . processEnvVars = undefined ;
93
- // Pass in the shell where known instead of relying on the application environment, because of bug
94
- // on VSCode: https://github.com/microsoft/vscode/issues/160694
95
- await this . _applyCollection ( undefined , shell ) . ignoreErrors ( ) ;
96
- this . hideProgress ( ) ;
97
- } ,
98
- this ,
99
- this . disposables ,
100
- ) ;
101
- this . registeredOnce = true ;
111
+ this . _applyCollection ( resource ) . ignoreErrors ( ) ;
112
+ } catch ( ex ) {
113
+ traceError ( `Activating terminal env collection failed` , ex ) ;
102
114
}
103
- this . _applyCollection ( resource ) . ignoreErrors ( ) ;
104
115
}
105
116
106
117
public async _applyCollection ( resource : Resource , shell = this . applicationEnvironment . shell ) : Promise < void > {
107
118
const workspaceFolder = this . getWorkspaceFolder ( resource ) ;
108
119
const settings = this . configurationService . getSettings ( resource ) ;
109
- const envVarCollection = this . context . getEnvironmentVariableCollection ( { workspaceFolder } ) ;
120
+ const envVarCollection = this . getEnvironmentVariableCollection ( { workspaceFolder } ) ;
110
121
// Clear any previously set env vars from collection
111
122
envVarCollection . clear ( ) ;
112
123
if ( ! settings . terminal . activateEnvironment ) {
@@ -221,6 +232,13 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
221
232
// PS1 should be set but no PS1 was set.
222
233
return ;
223
234
}
235
+ const config = this . workspaceService
236
+ . getConfiguration ( 'terminal' )
237
+ . get < boolean > ( 'integrated.shellIntegration.enabled' ) ;
238
+ if ( ! config ) {
239
+ traceVerbose ( 'PS1 is not set when shell integration is disabled.' ) ;
240
+ return ;
241
+ }
224
242
}
225
243
this . terminalPromptIsCorrect ( resource ) ;
226
244
}
@@ -232,7 +250,7 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
232
250
if ( interpreter ?. envType === EnvironmentType . Venv ) {
233
251
const activatePath = path . join ( path . dirname ( interpreter . path ) , 'activate' ) ;
234
252
if ( ! ( await pathExists ( activatePath ) ) ) {
235
- const envVarCollection = this . context . getEnvironmentVariableCollection ( { workspaceFolder } ) ;
253
+ const envVarCollection = this . getEnvironmentVariableCollection ( { workspaceFolder } ) ;
236
254
const pathVarName = getSearchPathEnvVarNames ( ) [ 0 ] ;
237
255
envVarCollection . replace (
238
256
'PATH' ,
@@ -241,13 +259,18 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
241
259
) ;
242
260
return ;
243
261
}
244
- this . context . getEnvironmentVariableCollection ( { workspaceFolder } ) . clear ( ) ;
262
+ this . getEnvironmentVariableCollection ( { workspaceFolder } ) . clear ( ) ;
245
263
}
246
264
} catch ( ex ) {
247
265
traceWarn ( `Microvenv failed as it is using proposed API which is constantly changing` , ex ) ;
248
266
}
249
267
}
250
268
269
+ private getEnvironmentVariableCollection ( scope : EnvironmentVariableScope = { } ) {
270
+ const envVarCollection = this . context . environmentVariableCollection as GlobalEnvironmentVariableCollection ;
271
+ return envVarCollection . getScoped ( scope ) ;
272
+ }
273
+
251
274
private getWorkspaceFolder ( resource : Resource ) : WorkspaceFolder | undefined {
252
275
let workspaceFolder = this . workspaceService . getWorkspaceFolder ( resource ) ;
253
276
if (
0 commit comments