terminalEnvironment.ts 9.1 KB
Newer Older
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import * as path from 'vs/base/common/path';
7
import * as platform from 'vs/base/common/platform';
8
import { URI as Uri } from 'vs/base/common/uri';
9
import { IWorkspaceFolder } from 'vs/platform/workspace/common/workspace';
10
import { IShellLaunchConfig, ITerminalEnvironment } from 'vs/workbench/contrib/terminal/common/terminal';
11
import { IConfigurationResolverService } from 'vs/workbench/services/configurationResolver/common/configurationResolver';
12
import { sanitizeProcessEnvironment } from 'vs/base/common/processes';
13 14 15 16 17

/**
 * This module contains utility functions related to the environment, cwd and paths.
 */

D
Daniel Imms 已提交
18
export function mergeEnvironments(parent: platform.IProcessEnvironment, other: ITerminalEnvironment | undefined): void {
19 20 21 22 23 24 25
	if (!other) {
		return;
	}

	// On Windows apply the new values ignoring case, while still retaining
	// the case of the original key.
	if (platform.isWindows) {
D
Daniel Imms 已提交
26
		for (const configKey in other) {
27
			let actualKey = configKey;
D
Daniel Imms 已提交
28
			for (const envKey in parent) {
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
				if (configKey.toLowerCase() === envKey.toLowerCase()) {
					actualKey = envKey;
					break;
				}
			}
			const value = other[configKey];
			_mergeEnvironmentValue(parent, actualKey, value);
		}
	} else {
		Object.keys(other).forEach((key) => {
			const value = other[key];
			_mergeEnvironmentValue(parent, key, value);
		});
	}
}

45
function _mergeEnvironmentValue(env: ITerminalEnvironment, key: string, value: string | null): void {
46 47 48 49 50 51 52
	if (typeof value === 'string') {
		env[key] = value;
	} else {
		delete env[key];
	}
}

D
Daniel Imms 已提交
53
export function addTerminalEnvironmentKeys(env: platform.IProcessEnvironment, version: string | undefined, locale: string | undefined, setLocaleVariables: boolean): void {
54
	env['TERM_PROGRAM'] = 'vscode';
D
Daniel Imms 已提交
55 56 57
	if (version) {
		env['TERM_PROGRAM_VERSION'] = version;
	}
D
Daniel Imms 已提交
58 59 60
	if (setLocaleVariables) {
		env['LANG'] = _getLangEnvVariable(locale);
	}
61 62
}

63
function mergeNonNullKeys(env: platform.IProcessEnvironment, other: ITerminalEnvironment | NodeJS.ProcessEnv | undefined) {
D
Daniel Imms 已提交
64 65 66 67 68 69 70 71 72 73 74
	if (!other) {
		return;
	}
	for (const key of Object.keys(other)) {
		const value = other[key];
		if (value) {
			env[key] = value;
		}
	}
}

75
function resolveConfigurationVariables(configurationResolverService: IConfigurationResolverService, env: ITerminalEnvironment, lastActiveWorkspaceRoot: IWorkspaceFolder | null): ITerminalEnvironment {
76
	Object.keys(env).forEach((key) => {
77 78 79
		const value = env[key];
		if (typeof value === 'string' && lastActiveWorkspaceRoot !== null) {
			env[key] = configurationResolverService.resolve(lastActiveWorkspaceRoot, value);
80 81 82 83 84
		}
	});
	return env;
}

85
function _getLangEnvVariable(locale?: string) {
86 87 88 89 90 91 92 93 94 95
	const parts = locale ? locale.split('-') : [];
	const n = parts.length;
	if (n === 0) {
		// Fallback to en_US to prevent possible encoding issues.
		return 'en_US.UTF-8';
	}
	if (n === 1) {
		// app.getLocale can return just a language without a variant, fill in the variant for
		// supported languages as many shells expect a 2-part locale.
		const languageVariants = {
96
			cs: 'CZ',
97 98 99 100 101
			de: 'DE',
			en: 'US',
			es: 'ES',
			fi: 'FI',
			fr: 'FR',
D
Daniel Imms 已提交
102
			hu: 'HU',
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
			it: 'IT',
			ja: 'JP',
			ko: 'KR',
			pl: 'PL',
			ru: 'RU',
			zh: 'CN'
		};
		if (parts[0] in languageVariants) {
			parts.push(languageVariants[parts[0]]);
		}
	} else {
		// Ensure the variant is uppercase
		parts[1] = parts[1].toUpperCase();
	}
	return parts.join('_') + '.UTF-8';
}

120
export function getCwd(shell: IShellLaunchConfig, userHome: string, root?: Uri, customCwd?: string): string {
121
	if (shell.cwd) {
A
Alex Ross 已提交
122
		return (typeof shell.cwd === 'object') ? shell.cwd.fsPath : shell.cwd;
123 124
	}

125
	let cwd: string | undefined;
126 127

	// TODO: Handle non-existent customCwd
128
	if (!shell.ignoreConfigurationCwd && customCwd) {
129
		if (path.isAbsolute(customCwd)) {
130 131
			cwd = customCwd;
		} else if (root) {
132
			cwd = path.join(root.fsPath, customCwd);
133 134 135 136 137
		}
	}

	// If there was no custom cwd or it was relative with no workspace
	if (!cwd) {
138
		cwd = root ? root.fsPath : userHome;
139 140 141 142 143 144 145 146 147 148 149 150
	}

	return _sanitizeCwd(cwd);
}

function _sanitizeCwd(cwd: string): string {
	// Make the drive letter uppercase on Windows (see #9448)
	if (platform.platform === platform.Platform.Windows && cwd && cwd[1] === ':') {
		return cwd[0].toUpperCase() + cwd.substr(1);
	}
	return cwd;
}
151

152 153 154 155
export function escapeNonWindowsPath(path: string): string {
	let newPath = path;
	if (newPath.indexOf('\\') !== 0) {
		newPath = newPath.replace(/\\/g, '\\\\');
156
	}
157 158 159 160
	if (!newPath && (newPath.indexOf('"') !== -1)) {
		newPath = '\'' + newPath + '\'';
	} else if (newPath.indexOf(' ') !== -1) {
		newPath = newPath.replace(/ /g, '\\ ');
161
	}
162
	return newPath;
163
}
D
Daniel Imms 已提交
164

D
Daniel Imms 已提交
165
export function getDefaultShell(
D
Daniel Imms 已提交
166 167
	fetchSetting: (key: string) => { user: string | string[] | undefined, value: string | string[] | undefined, default: string | string[] | undefined },
	isWorkspaceShellAllowed: boolean,
168
	defaultShell: string,
169 170
	isWoW64: boolean,
	windir: string | undefined,
D
Daniel Imms 已提交
171
	platformOverride: platform.Platform = platform.platform
D
Daniel Imms 已提交
172
): string {
D
Daniel Imms 已提交
173 174
	const platformKey = platformOverride === platform.Platform.Windows ? 'windows' : platformOverride === platform.Platform.Mac ? 'osx' : 'linux';
	const shellConfigValue = fetchSetting(`terminal.integrated.shell.${platformKey}`);
D
Daniel Imms 已提交
175
	let executable = (isWorkspaceShellAllowed ? <string>shellConfigValue.value : <string>shellConfigValue.user) || (<string | null>shellConfigValue.default || defaultShell);
D
Daniel Imms 已提交
176 177 178 179

	// Change Sysnative to System32 if the OS is Windows but NOT WoW64. It's
	// safe to assume that this was used by accident as Sysnative does not
	// exist and will break the terminal in non-WoW64 environments.
180 181
	if ((platformOverride === platform.Platform.Windows) && !isWoW64 && windir) {
		const sysnativePath = path.join(windir, 'Sysnative').toLowerCase();
D
Daniel Imms 已提交
182
		if (executable && executable.toLowerCase().indexOf(sysnativePath) === 0) {
183
			executable = path.join(windir, 'System32', executable.substr(sysnativePath.length));
D
Daniel Imms 已提交
184 185 186 187
		}
	}

	// Convert / to \ on Windows for convenience
D
Daniel Imms 已提交
188 189
	if (executable && platformOverride === platform.Platform.Windows) {
		executable = executable.replace(/\//g, '\\');
D
Daniel Imms 已提交
190
	}
D
Daniel Imms 已提交
191 192 193 194

	return executable;
}

D
Daniel Imms 已提交
195
export function getDefaultShellArgs(
D
Daniel Imms 已提交
196 197 198 199 200 201 202 203 204 205
	fetchSetting: (key: string) => { user: string | string[] | undefined, value: string | string[] | undefined, default: string | string[] | undefined },
	isWorkspaceShellAllowed: boolean,
	platformOverride: platform.Platform = platform.platform
): string[] {
	const platformKey = platformOverride === platform.Platform.Windows ? 'windows' : platformOverride === platform.Platform.Mac ? 'osx' : 'linux';
	const shellArgsConfigValue = fetchSetting(`terminal.integrated.shellArgs.${platformKey}`);
	const args = (isWorkspaceShellAllowed ? <string[]>shellArgsConfigValue.value : <string[]>shellArgsConfigValue.user) || <string[]>shellArgsConfigValue.default;
	return args;
}

206 207 208 209 210 211 212
export function createTerminalEnvironment(
	shellLaunchConfig: IShellLaunchConfig,
	lastActiveWorkspace: IWorkspaceFolder | null,
	envFromConfig: { user: ITerminalEnvironment | undefined, value: ITerminalEnvironment | undefined, default: ITerminalEnvironment | undefined },
	configurationResolverService: IConfigurationResolverService | undefined,
	isWorkspaceShellAllowed: boolean,
	version: string | undefined,
213
	setLocaleVariables: boolean,
D
Daniel Imms 已提交
214
	baseEnv: platform.IProcessEnvironment
215 216 217 218 219 220 221 222
): platform.IProcessEnvironment {
	// Create a terminal environment based on settings, launch config and permissions
	let env: platform.IProcessEnvironment = {};
	if (shellLaunchConfig.strictEnv) {
		// strictEnv is true, only use the requested env (ignoring null entries)
		mergeNonNullKeys(env, shellLaunchConfig.env);
	} else {
		// Merge process env with the env from config and from shellLaunchConfig
D
Daniel Imms 已提交
223
		mergeNonNullKeys(env, baseEnv);
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

		// const platformKey = platform.isWindows ? 'windows' : (platform.isMacintosh ? 'osx' : 'linux');
		// const envFromConfigValue = this._workspaceConfigurationService.inspect<ITerminalEnvironment | undefined>(`terminal.integrated.env.${platformKey}`);
		const allowedEnvFromConfig = { ...(isWorkspaceShellAllowed ? envFromConfig.value : envFromConfig.user) };

		// Resolve env vars from config and shell
		if (configurationResolverService) {
			if (allowedEnvFromConfig) {
				resolveConfigurationVariables(configurationResolverService, allowedEnvFromConfig, lastActiveWorkspace);
			}
			if (shellLaunchConfig.env) {
				resolveConfigurationVariables(configurationResolverService, shellLaunchConfig.env, lastActiveWorkspace);
			}
		}

		// Merge config (settings) and ShellLaunchConfig environments
		mergeEnvironments(env, allowedEnvFromConfig);
		mergeEnvironments(env, shellLaunchConfig.env);

		// Sanitize the environment, removing any undesirable VS Code and Electron environment
		// variables
		sanitizeProcessEnvironment(env, 'VSCODE_IPC_HOOK_CLI');

		// Adding other env keys necessary to create the process
		addTerminalEnvironmentKeys(env, version, platform.locale, setLocaleVariables);
	}
	return env;
251
}