@@ -7,17 +7,16 @@ import { ITerminalInstanceService } from 'vs/workbench/contrib/terminal/browser/
77import { ITerminalInstance , IWindowsShellHelper , IShellLaunchConfig , ITerminalChildProcess , IS_WORKSPACE_SHELL_ALLOWED_STORAGE_KEY } from 'vs/workbench/contrib/terminal/common/terminal' ;
88import { WindowsShellHelper } from 'vs/workbench/contrib/terminal/node/windowsShellHelper' ;
99import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation' ;
10- import { IProcessEnvironment , isLinux , isMacintosh , isWindows , platform , Platform } from 'vs/base/common/platform' ;
10+ import { IProcessEnvironment , platform , Platform } from 'vs/base/common/platform' ;
1111import { TerminalProcess } from 'vs/workbench/contrib/terminal/node/terminalProcess' ;
1212import { getSystemShell } from 'vs/workbench/contrib/terminal/node/terminal' ;
1313import { Terminal as XTermTerminal } from 'xterm' ;
1414import { WebLinksAddon as XTermWebLinksAddon } from 'xterm-addon-web-links' ;
1515import { SearchAddon as XTermSearchAddon } from 'xterm-addon-search' ;
16- import { readFile } from 'vs/base/node/pfs' ;
17- import { basename } from 'vs/base/common/path' ;
1816import { IConfigurationService } from 'vs/platform/configuration/common/configuration' ;
1917import { getDefaultShell , getDefaultShellArgs } from 'vs/workbench/contrib/terminal/common/terminalEnvironment' ;
2018import { StorageScope , IStorageService } from 'vs/platform/storage/common/storage' ;
19+ import { getMainProcessParentEnv } from 'vs/workbench/contrib/terminal/node/terminalEnvironment' ;
2120
2221let Terminal : typeof XTermTerminal ;
2322let WebLinksAddon : typeof XTermWebLinksAddon ;
@@ -26,8 +25,6 @@ let SearchAddon: typeof XTermSearchAddon;
2625export class TerminalInstanceService implements ITerminalInstanceService {
2726 public _serviceBrand : any ;
2827
29- private _mainProcessParentEnv : IProcessEnvironment | undefined ;
30-
3128 constructor (
3229 @IInstantiationService private readonly _instantiationService : IInstantiationService ,
3330 @IConfigurationService private readonly _configurationService : IConfigurationService ,
@@ -86,72 +83,7 @@ export class TerminalInstanceService implements ITerminalInstanceService {
8683 return Promise . resolve ( { shell, args } ) ;
8784 }
8885
89- public async getMainProcessParentEnv ( ) : Promise < IProcessEnvironment > {
90- if ( this . _mainProcessParentEnv ) {
91- return this . _mainProcessParentEnv ;
92- }
93-
94- // For Linux use /proc/<pid>/status to get the parent of the main process and then fetch its
95- // env using /proc/<pid>/environ.
96- if ( isLinux ) {
97- const mainProcessId = process . ppid ;
98- const codeProcessName = basename ( process . argv [ 0 ] ) ;
99- let pid : number = 0 ;
100- let ppid : number = mainProcessId ;
101- let name : string = codeProcessName ;
102- do {
103- pid = ppid ;
104- const status = await readFile ( `/proc/${ pid } /status` , 'utf8' ) ;
105- const splitByLine = status . split ( '\n' ) ;
106- splitByLine . forEach ( line => {
107- if ( line . indexOf ( 'Name:' ) === 0 ) {
108- name = line . replace ( / ^ N a m e : \s + / , '' ) ;
109- }
110- if ( line . indexOf ( 'PPid:' ) === 0 ) {
111- ppid = parseInt ( line . replace ( / ^ P P i d : \s + / , '' ) ) ;
112- }
113- } ) ;
114- } while ( name === codeProcessName ) ;
115- const rawEnv = await readFile ( `/proc/${ pid } /environ` , 'utf8' ) ;
116- const env = { } ;
117- rawEnv . split ( '\0' ) . forEach ( e => {
118- const i = e . indexOf ( '=' ) ;
119- env [ e . substr ( 0 , i ) ] = e . substr ( i + 1 ) ;
120- } ) ;
121- this . _mainProcessParentEnv = env ;
122- }
123-
124- // For macOS we want the "root" environment as shells by default run as login shells. It
125- // doesn't appear to be possible to get the "root" environment as `ps eww -o command` for
126- // PID 1 (the parent of the main process when launched from the dock/finder) returns no
127- // environment, because of this we will fill in the root environment using a whitelist of
128- // environment variables that we have.
129- if ( isMacintosh ) {
130- this . _mainProcessParentEnv = { } ;
131- // This list was generated by diffing launching a terminal with {} and the system
132- // terminal launched from finder.
133- const rootEnvVars = [
134- 'SHELL' ,
135- 'SSH_AUTH_SOCK' ,
136- 'Apple_PubSub_Socket_Render' ,
137- 'XPC_FLAGS' ,
138- 'XPC_SERVICE_NAME' ,
139- 'HOME' ,
140- 'LOGNAME' ,
141- 'TMPDIR'
142- ] ;
143- rootEnvVars . forEach ( k => {
144- if ( process . env [ k ] ) {
145- this . _mainProcessParentEnv ! [ k ] = process . env [ k ] ! ;
146- }
147- } ) ;
148- }
149-
150- // TODO: Windows should return a fresh environment block, might need native code?
151- if ( isWindows ) {
152- this . _mainProcessParentEnv = process . env as IProcessEnvironment ;
153- }
154-
155- return this . _mainProcessParentEnv ! ;
86+ public getMainProcessParentEnv ( ) : Promise < IProcessEnvironment > {
87+ return getMainProcessParentEnv ( ) ;
15688 }
15789}
0 commit comments