@@ -286,6 +286,8 @@ export class ExtHostTerminalService implements ExtHostTerminalServiceShape {
286286 private _terminalProcesses : { [ id : number ] : ITerminalChildProcess } = { } ;
287287 private _terminalRenderers : ExtHostTerminalRenderer [ ] = [ ] ;
288288 private _getTerminalPromises : { [ id : number ] : Promise < ExtHostTerminal > } = { } ;
289+ private _variableResolver : ExtHostVariableResolverService | undefined ;
290+ private _lastActiveWorkspace : IWorkspaceFolder | undefined ;
289291
290292 // TODO: Pull this from main side
291293 private _isWorkspaceShellAllowed : boolean = false ;
@@ -307,9 +309,12 @@ export class ExtHostTerminalService implements ExtHostTerminalServiceShape {
307309 private _extHostConfiguration : ExtHostConfiguration ,
308310 private _extHostWorkspace : ExtHostWorkspace ,
309311 private _extHostDocumentsAndEditors : ExtHostDocumentsAndEditors ,
310- private _logService : ILogService ,
312+ private _logService : ILogService
311313 ) {
312314 this . _proxy = mainContext . getProxy ( MainContext . MainThreadTerminalService ) ;
315+ this . updateLastActiveWorkspace ( ) ;
316+ this . updateVariableResolver ( ) ;
317+ this . registerListeners ( ) ;
313318 }
314319
315320 public createTerminal ( name ?: string , shellPath ?: string , shellArgs ?: string [ ] | string ) : vscode . Terminal {
@@ -366,18 +371,21 @@ export class ExtHostTerminalService implements ExtHostTerminalServiceShape {
366371 this . _isWorkspaceShellAllowed ,
367372 getSystemShell ( platform . platform ) ,
368373 process . env . hasOwnProperty ( 'PROCESSOR_ARCHITEW6432' ) ,
369- process . env . windir
374+ process . env . windir ,
375+ this . _lastActiveWorkspace ,
376+ this . _variableResolver
370377 ) ;
371378 }
372379
373- private _getDefaultShellArgs ( configProvider : ExtHostConfigProvider ) : string [ ] | string | undefined {
380+ private _getDefaultShellArgs ( configProvider : ExtHostConfigProvider ) : string [ ] | string {
374381 const fetchSetting = ( key : string ) => {
375382 const setting = configProvider
376383 . getConfiguration ( key . substr ( 0 , key . lastIndexOf ( '.' ) ) )
377384 . inspect < string | string [ ] > ( key . substr ( key . lastIndexOf ( '.' ) + 1 ) ) ;
378385 return this . _apiInspectConfigToPlain < string | string [ ] > ( setting ) ;
379386 } ;
380- return terminalEnvironment . getDefaultShellArgs ( fetchSetting , this . _isWorkspaceShellAllowed ) ;
387+
388+ return terminalEnvironment . getDefaultShellArgs ( fetchSetting , this . _isWorkspaceShellAllowed , this . _lastActiveWorkspace , this . _variableResolver ) ;
381389 }
382390
383391 public async resolveTerminalRenderer ( id : number ) : Promise < vscode . TerminalRenderer > {
@@ -526,6 +534,24 @@ export class ExtHostTerminalService implements ExtHostTerminalServiceShape {
526534 return env ;
527535 }
528536
537+ private registerListeners ( ) : void {
538+ this . _extHostDocumentsAndEditors . onDidChangeActiveTextEditor ( ( ) => this . updateLastActiveWorkspace ( ) ) ;
539+ this . _extHostWorkspace . onDidChangeWorkspace ( ( ) => this . updateVariableResolver ( ) ) ;
540+ }
541+
542+ private updateLastActiveWorkspace ( ) : void {
543+ const activeEditor = this . _extHostDocumentsAndEditors . activeEditor ( ) ;
544+ if ( activeEditor ) {
545+ this . _lastActiveWorkspace = this . _extHostWorkspace . getWorkspaceFolder ( activeEditor . document . uri ) as IWorkspaceFolder ;
546+ }
547+ }
548+
549+ private async updateVariableResolver ( ) : Promise < void > {
550+ const configProvider = await this . _extHostConfiguration . getConfigProvider ( ) ;
551+ const workspaceFolders = await this . _extHostWorkspace . getWorkspaceFolders2 ( ) ;
552+ this . _variableResolver = new ExtHostVariableResolverService ( workspaceFolders || [ ] , this . _extHostDocumentsAndEditors , configProvider ) ;
553+ }
554+
529555 public async $createProcess ( id : number , shellLaunchConfigDto : ShellLaunchConfigDto , activeWorkspaceRootUriComponents : UriComponents , cols : number , rows : number , isWorkspaceShellAllowed : boolean ) : Promise < void > {
530556 const shellLaunchConfig : IShellLaunchConfig = {
531557 name : shellLaunchConfigDto . name ,
@@ -541,6 +567,21 @@ export class ExtHostTerminalService implements ExtHostTerminalServiceShape {
541567 if ( ! shellLaunchConfig . executable ) {
542568 shellLaunchConfig . executable = this . getDefaultShell ( configProvider ) ;
543569 shellLaunchConfig . args = this . _getDefaultShellArgs ( configProvider ) ;
570+ } else {
571+ if ( this . _variableResolver ) {
572+ shellLaunchConfig . executable = this . _variableResolver . resolve ( this . _lastActiveWorkspace , shellLaunchConfig . executable ) ;
573+ if ( shellLaunchConfig . args ) {
574+ if ( Array . isArray ( shellLaunchConfig . args ) ) {
575+ const resolvedArgs : string [ ] = [ ] ;
576+ for ( const arg of shellLaunchConfig . args ) {
577+ resolvedArgs . push ( this . _variableResolver . resolve ( this . _lastActiveWorkspace , arg ) ) ;
578+ }
579+ shellLaunchConfig . args = resolvedArgs ;
580+ } else {
581+ shellLaunchConfig . args = this . _variableResolver . resolve ( this . _lastActiveWorkspace , shellLaunchConfig . args ) ;
582+ }
583+ }
584+ }
544585 }
545586
546587 // Get the initial cwd
@@ -559,14 +600,12 @@ export class ExtHostTerminalService implements ExtHostTerminalServiceShape {
559600 }
560601 } as IWorkspaceFolder : null ;
561602 const envFromConfig = this . _apiInspectConfigToPlain ( configProvider . getConfiguration ( 'terminal.integrated' ) . inspect < ITerminalEnvironment > ( `env.${ platformKey } ` ) ) ;
562- const workspaceFolders = await this . _extHostWorkspace . getWorkspaceFolders2 ( ) ;
563- const variableResolver = workspaceFolders ? new ExtHostVariableResolverService ( workspaceFolders , this . _extHostDocumentsAndEditors , configProvider ) : undefined ;
564603 const baseEnv = terminalConfig . get < boolean > ( 'inheritEnv' , true ) ? process . env as platform . IProcessEnvironment : await this . _getNonInheritedEnv ( ) ;
565604 const env = terminalEnvironment . createTerminalEnvironment (
566605 shellLaunchConfig ,
567606 lastActiveWorkspace ,
568607 envFromConfig ,
569- variableResolver ,
608+ this . _variableResolver ,
570609 isWorkspaceShellAllowed ,
571610 pkg . version ,
572611 terminalConfig . get < boolean > ( 'setLocaleVariables' , false ) ,
0 commit comments