Remove inheritEnv, don't use shell env

Part of #117257
Part of #121947
This commit is contained in:
Daniel Imms 2021-05-03 22:34:58 -07:00
parent de4e9067e8
commit 3ef66a464b
10 changed files with 5 additions and 117 deletions

View file

@ -26,7 +26,6 @@ export interface ICompleteTerminalConfiguration {
'terminal.integrated.env.windows': ISingleTerminalConfiguration<ITerminalEnvironment>;
'terminal.integrated.env.osx': ISingleTerminalConfiguration<ITerminalEnvironment>;
'terminal.integrated.env.linux': ISingleTerminalConfiguration<ITerminalEnvironment>;
'terminal.integrated.inheritEnv': boolean;
'terminal.integrated.cwd': string;
'terminal.integrated.detectLocale': 'auto' | 'off' | 'on';
}

View file

@ -6,7 +6,6 @@
import { Codicon } from 'vs/base/common/codicons';
import { Event } from 'vs/base/common/event';
import { IDisposable } from 'vs/base/common/lifecycle';
import { IProcessEnvironment } from 'vs/base/common/platform';
import { URI } from 'vs/base/common/uri';
import { FindReplaceState } from 'vs/editor/contrib/find/findState';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
@ -38,7 +37,6 @@ export interface ITerminalInstanceService {
getXtermSearchConstructor(): Promise<typeof XTermSearchAddon>;
getXtermUnicode11Constructor(): Promise<typeof XTermUnicode11Addon>;
getXtermWebglConstructor(): Promise<typeof XTermWebglAddon>;
getMainProcessParentEnv(): Promise<IProcessEnvironment>;
}
export interface IBrowserTerminalConfigHelper extends ITerminalConfigHelper {

View file

@ -9,7 +9,6 @@ import type { Terminal as XTermTerminal } from 'xterm';
import type { SearchAddon as XTermSearchAddon } from 'xterm-addon-search';
import type { Unicode11Addon as XTermUnicode11Addon } from 'xterm-addon-unicode11';
import type { WebglAddon as XTermWebglAddon } from 'xterm-addon-webgl';
import { IProcessEnvironment } from 'vs/base/common/platform';
import { Emitter } from 'vs/base/common/event';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { Disposable } from 'vs/base/common/lifecycle';
@ -52,10 +51,6 @@ export class TerminalInstanceService extends Disposable implements ITerminalInst
}
return WebglAddon;
}
public async getMainProcessParentEnv(): Promise<IProcessEnvironment> {
return {};
}
}
registerSingleton(ITerminalInstanceService, TerminalInstanceService, true);

View file

@ -15,7 +15,7 @@ import { Schemas } from 'vs/base/common/network';
import { getRemoteAuthority } from 'vs/platform/remote/common/remoteHosts';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { IProductService } from 'vs/platform/product/common/productService';
import { IRemoteTerminalService, ITerminalInstanceService } from 'vs/workbench/contrib/terminal/browser/terminal';
import { IRemoteTerminalService } from 'vs/workbench/contrib/terminal/browser/terminal';
import { IRemoteAgentService } from 'vs/workbench/services/remote/common/remoteAgentService';
import { Disposable, dispose, IDisposable, toDisposable } from 'vs/base/common/lifecycle';
import { withNullAsUndefined } from 'vs/base/common/types';
@ -120,7 +120,6 @@ export class TerminalProcessManager extends Disposable implements ITerminalProce
@IConfigurationResolverService private readonly _configurationResolverService: IConfigurationResolverService,
@IWorkbenchEnvironmentService private readonly _workbenchEnvironmentService: IWorkbenchEnvironmentService,
@IProductService private readonly _productService: IProductService,
@ITerminalInstanceService private readonly _terminalInstanceService: ITerminalInstanceService,
@IRemoteAgentService private readonly _remoteAgentService: IRemoteAgentService,
@IPathService private readonly _pathService: IPathService,
@IEnvironmentVariableService private readonly _environmentVariableService: IEnvironmentVariableService,
@ -251,7 +250,6 @@ export class TerminalProcessManager extends Disposable implements ITerminalProce
'terminal.integrated.env.windows': this._terminalProfileResolverService.getSafeConfigValueFullKey(TERMINAL_SETTING_ID.EnvWindows) as ITerminalEnvironment,
'terminal.integrated.env.osx': this._terminalProfileResolverService.getSafeConfigValueFullKey(TERMINAL_SETTING_ID.EnvMacOs) as ITerminalEnvironment,
'terminal.integrated.env.linux': this._terminalProfileResolverService.getSafeConfigValueFullKey(TERMINAL_SETTING_ID.EnvLinux) as ITerminalEnvironment,
'terminal.integrated.inheritEnv': terminalConfig.inheritEnv,
'terminal.integrated.cwd': this._terminalProfileResolverService.getSafeConfigValueFullKey(TERMINAL_SETTING_ID.Cwd) as string,
'terminal.integrated.detectLocale': terminalConfig.detectLocale
};
@ -358,9 +356,7 @@ export class TerminalProcessManager extends Disposable implements ITerminalProce
// this._configurationService.getValue<ITerminalEnvironment | undefined>(`terminal.integrated.env.${platformKey}`);
const envFromConfigValue = this._terminalProfileResolverService.getSafeConfigValue('env', OS) as ITerminalEnvironment | undefined;
this._configHelper.showRecommendations(shellLaunchConfig);
const baseEnv = await (this._configHelper.config.inheritEnv
? this._terminalProfileResolverService.getShellEnvironment(this.remoteAuthority)
: this._terminalInstanceService.getMainProcessParentEnv());
const baseEnv = await this._terminalProfileResolverService.getShellEnvironment(this.remoteAuthority);
const env = terminalEnvironment.createTerminalEnvironment(shellLaunchConfig, envFromConfigValue, variableResolver, this._productService.version, this._configHelper.config.detectLocale, baseEnv);
if (!shellLaunchConfig.strictEnv && !shellLaunchConfig.hideFromUser) {
@ -410,7 +406,6 @@ export class TerminalProcessManager extends Disposable implements ITerminalProce
const useConpty = this._configHelper.config.windowsEnableConpty && !isScreenReaderModeEnabled;
const shouldPersist = this._configHelper.config.enablePersistentSessions && !shellLaunchConfig.isFeatureTerminal;
return await localTerminalService.createProcess(shellLaunchConfig, initialCwd, cols, rows, env, useConpty, shouldPersist);
}

View file

@ -37,7 +37,6 @@ export interface ICompleteTerminalConfiguration {
'terminal.integrated.env.windows': ITerminalEnvironment;
'terminal.integrated.env.osx': ITerminalEnvironment;
'terminal.integrated.env.linux': ITerminalEnvironment;
'terminal.integrated.inheritEnv': boolean;
'terminal.integrated.cwd': string;
'terminal.integrated.detectLocale': 'auto' | 'off' | 'on';
}

View file

@ -171,7 +171,6 @@ export interface ITerminalConfiguration {
cwd: string;
confirmOnExit: boolean;
enableBell: boolean;
inheritEnv: boolean;
env: {
linux: { [key: string]: string };
osx: { [key: string]: string };
@ -479,7 +478,6 @@ export const enum TERMINAL_SETTING_ID {
CommandsToSkipShell = 'terminal.integrated.commandsToSkipShell',
AllowChords = 'terminal.integrated.allowChords',
AllowMnemonics = 'terminal.integrated.allowMnemonics',
InheritEnv = 'terminal.integrated.inheritEnv',
EnvMacOs = 'terminal.integrated.env.osx',
EnvLinux = 'terminal.integrated.env.linux',
EnvWindows = 'terminal.integrated.env.windows',

View file

@ -526,11 +526,6 @@ export const terminalConfiguration: IConfigurationNode = {
type: 'boolean',
default: false
},
[TERMINAL_SETTING_ID.InheritEnv]: {
markdownDescription: localize('terminal.integrated.inheritEnv', "Whether new shells should inherit their environment from VS Code. This is not supported on Windows."),
type: 'boolean',
default: true
},
[TERMINAL_SETTING_ID.EnvMacOs]: {
restricted: true,
markdownDescription: localize('terminal.integrated.env.osx', "Object with environment variables that will be added to the VS Code process to be used by the terminal on macOS. Set to `null` to delete the environment variable."),

View file

@ -4,13 +4,10 @@
*--------------------------------------------------------------------------------------------*/
import { ITerminalInstanceService } from 'vs/workbench/contrib/terminal/browser/terminal';
import { Disposable } from 'vs/base/common/lifecycle';
import { IProcessEnvironment } from 'vs/base/common/platform';
import { getMainProcessParentEnv } from 'vs/workbench/contrib/terminal/node/terminalEnvironment';
import type { Terminal as XTermTerminal } from 'xterm';
import type { SearchAddon as XTermSearchAddon } from 'xterm-addon-search';
import type { Unicode11Addon as XTermUnicode11Addon } from 'xterm-addon-unicode11';
import type { WebglAddon as XTermWebglAddon } from 'xterm-addon-webgl';
import { IShellEnvironmentService } from 'vs/workbench/services/environment/electron-sandbox/shellEnvironmentService';
let Terminal: typeof XTermTerminal;
let SearchAddon: typeof XTermSearchAddon;
@ -20,12 +17,6 @@ let WebglAddon: typeof XTermWebglAddon;
export class TerminalInstanceService extends Disposable implements ITerminalInstanceService {
public _serviceBrand: undefined;
constructor(
@IShellEnvironmentService private readonly _shellEnvironmentService: IShellEnvironmentService
) {
super();
}
public async getXtermConstructor(): Promise<typeof XTermTerminal> {
if (!Terminal) {
Terminal = (await import('xterm')).Terminal;
@ -53,8 +44,4 @@ export class TerminalInstanceService extends Disposable implements ITerminalInst
}
return WebglAddon;
}
public async getMainProcessParentEnv(): Promise<IProcessEnvironment> {
return getMainProcessParentEnv(await this._shellEnvironmentService.getShellEnv());
}
}

View file

@ -34,9 +34,10 @@ export class ElectronTerminalProfileResolverService extends BaseTerminalProfileR
},
getShellEnvironment: (remoteAuthority) => {
if (remoteAuthority) {
remoteTerminalService.getShellEnvironment();
return remoteTerminalService.getShellEnvironment();
} else {
return localTerminalService.getShellEnvironment();
}
return shellEnvironmentService.getShellEnv();
}
},
configurationService,

View file

@ -1,79 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as fs from 'fs';
import * as path from 'vs/base/common/path';
import { IProcessEnvironment, isLinux, isMacintosh, isWindows } from 'vs/base/common/platform';
let mainProcessParentEnv: IProcessEnvironment | undefined;
export async function getMainProcessParentEnv(baseEnvironment: IProcessEnvironment): Promise<IProcessEnvironment> {
if (mainProcessParentEnv) {
return mainProcessParentEnv;
}
// For Linux use /proc/<pid>/status to get the parent of the main process and then fetch its
// env using /proc/<pid>/environ.
if (isLinux) {
const mainProcessId = process.ppid;
const codeProcessName = path.basename(process.argv[0]);
let pid: number = 0;
let ppid: number = mainProcessId;
let name: string = codeProcessName;
do {
pid = ppid;
const status = await fs.promises.readFile(`/proc/${pid}/status`, 'utf8');
const splitByLine = status.split('\n');
splitByLine.forEach(line => {
if (line.indexOf('Name:') === 0) {
name = line.replace(/^Name:\s+/, '');
}
if (line.indexOf('PPid:') === 0) {
ppid = parseInt(line.replace(/^PPid:\s+/, ''));
}
});
} while (name === codeProcessName);
const rawEnv = await fs.promises.readFile(`/proc/${pid}/environ`, 'utf8');
const env: IProcessEnvironment = {};
rawEnv.split('\0').forEach(e => {
const i = e.indexOf('=');
env[e.substr(0, i)] = e.substr(i + 1);
});
mainProcessParentEnv = env;
}
// For macOS we want the "root" environment as shells by default run as login shells. It
// doesn't appear to be possible to get the "root" environment as `ps eww -o command` for
// PID 1 (the parent of the main process when launched from the dock/finder) returns no
// environment, because of this we will fill in the root environment using a allowlist of
// environment variables that we have.
if (isMacintosh) {
mainProcessParentEnv = {};
// This list was generated by diffing launching a terminal with {} and the system
// terminal launched from finder.
const rootEnvVars = [
'SHELL',
'SSH_AUTH_SOCK',
'Apple_PubSub_Socket_Render',
'XPC_FLAGS',
'XPC_SERVICE_NAME',
'HOME',
'LOGNAME',
'TMPDIR'
];
rootEnvVars.forEach(k => {
if (baseEnvironment[k]) {
mainProcessParentEnv![k] = baseEnvironment[k]!;
}
});
}
// TODO: Windows should return a fresh environment block, might need native code?
if (isWindows) {
mainProcessParentEnv = baseEnvironment;
}
return mainProcessParentEnv!;
}