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,57 @@ 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
+ // TODO: Remove try...catch once proposed APIs being used are finalized.
72
+ try {
73
+ if ( ! inTerminalEnvVarExperiment ( this . experimentService ) ) {
74
+ this . context . environmentVariableCollection . clear ( ) ;
75
+ await this . handleMicroVenv ( resource ) ;
76
+ if ( ! this . registeredOnce ) {
77
+ this . interpreterService . onDidChangeInterpreter (
78
+ async ( r ) => {
79
+ await this . handleMicroVenv ( r ) ;
80
+ } ,
81
+ this ,
82
+ this . disposables ,
83
+ ) ;
84
+ this . registeredOnce = true ;
85
+ }
86
+ return ;
87
+ }
67
88
if ( ! this . registeredOnce ) {
68
89
this . interpreterService . onDidChangeInterpreter (
69
90
async ( r ) => {
70
- await this . handleMicroVenv ( r ) ;
91
+ this . showProgress ( ) ;
92
+ await this . _applyCollection ( r ) . ignoreErrors ( ) ;
93
+ this . hideProgress ( ) ;
94
+ } ,
95
+ this ,
96
+ this . disposables ,
97
+ ) ;
98
+ this . applicationEnvironment . onDidChangeShell (
99
+ async ( shell : string ) => {
100
+ this . showProgress ( ) ;
101
+ this . processEnvVars = undefined ;
102
+ // Pass in the shell where known instead of relying on the application environment, because of bug
103
+ // on VSCode: https://github.com/microsoft/vscode/issues/160694
104
+ await this . _applyCollection ( undefined , shell ) . ignoreErrors ( ) ;
105
+ this . hideProgress ( ) ;
71
106
} ,
72
107
this ,
73
108
this . disposables ,
74
109
) ;
75
110
this . registeredOnce = true ;
76
111
}
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 ;
112
+ this . _applyCollection ( resource ) . ignoreErrors ( ) ;
113
+ } catch ( ex ) {
114
+ traceError ( `Activating terminal env collection failed` , ex ) ;
102
115
}
103
- this . _applyCollection ( resource ) . ignoreErrors ( ) ;
104
116
}
105
117
106
118
public async _applyCollection ( resource : Resource , shell = this . applicationEnvironment . shell ) : Promise < void > {
107
119
const workspaceFolder = this . getWorkspaceFolder ( resource ) ;
108
120
const settings = this . configurationService . getSettings ( resource ) ;
109
- const envVarCollection = this . context . getEnvironmentVariableCollection ( { workspaceFolder } ) ;
93D4
121
+ const envVarCollection = this . getEnvironmentVariableCollection ( { workspaceFolder } ) ;
110
122
// Clear any previously set env vars from collection
111
123
envVarCollection . clear ( ) ;
112
124
if ( ! settings . terminal . activateEnvironment ) {
@@ -232,22 +244,27 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ
232
244
if ( interpreter ?. envType === EnvironmentType . Venv ) {
233
245
const activatePath = path . join ( path . dirname ( interpreter . path ) , 'activate' ) ;
234
246
if ( ! ( await pathExists ( activatePath ) ) ) {
235
- const envVarCollection = this . context . getEnvironmentVariableCollection ( { workspaceFolder } ) ;
247
+ const envVarCollection = this . getEnvironmentVariableCollection ( { workspaceFolder } ) ;
236
248
const pathVarName = getSearchPathEnvVarNames ( ) [ 0 ] ;
237
249
envVarCollection . replace (
238
250
'PATH' ,
239
251
`${ path . dirname ( interpreter . path ) } ${ path . delimiter } ${ process . env [ pathVarName ] } ` ,
240
- { applyAtShellIntegration : true , applyAtProcessCreation : true } ,
252
+ { applyAtShellIntegration : true } ,
241
253
) ;
242
254
return ;
243
255
}
244
- this . context . getEnvironmentVariableCollection ( { workspaceFolder } ) . clear ( ) ;
245
256
}
257
+ this . getEnvironmentVariableCollection ( { workspaceFolder } ) . clear ( ) ;
246
258
} catch ( ex ) {
247
259
traceWarn ( `Microvenv failed as it is using proposed API which is constantly changing` , ex ) ;
248
260
}
249
261
}
250
262
263
+ private getEnvironmentVariableCollection ( scope ?: EnvironmentVariableScope ) {
264
+ const envVarCollection = this . context . environmentVariableCollection as GlobalEnvironmentVariableCollection ;
265
+ return scope ?. workspaceFolder ? envVarCollection . getScoped ( scope ) : envVarCollection ;
266
+ }
267
+
251
268
private getWorkspaceFolder ( resource : Resource ) : WorkspaceFolder | undefined {
252
269
let workspaceFolder = this . workspaceService . getWorkspaceFolder ( resource ) ;
253
270
if (
0 commit comments