Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/node/shell.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { userInfo } from 'os';
7
import * as platform from '../common/platform.js';
8
import { getFirstAvailablePowerShellInstallation } from './powershell.js';
9
import * as processes from './processes.js';
10
11
/**
12
* Gets the detected default shell for the _system_, not to be confused with VS Code's _default_
13
* shell that the terminal uses by default.
14
* @param os The platform to detect the shell of.
15
*/
16
export async function getSystemShell(os: platform.OperatingSystem, env: platform.IProcessEnvironment): Promise<string> {
17
if (os === platform.OperatingSystem.Windows) {
18
if (platform.isWindows) {
19
return getSystemShellWindows();
20
}
21
// Don't detect Windows shell when not on Windows
22
return processes.getWindowsShell(env);
23
}
24
25
return getSystemShellUnixLike(os, env);
26
}
27
28
let _TERMINAL_DEFAULT_SHELL_UNIX_LIKE: string | null = null;
29
function getSystemShellUnixLike(os: platform.OperatingSystem, env: platform.IProcessEnvironment): string {
30
// Only use $SHELL for the current OS
31
if (platform.isLinux && os === platform.OperatingSystem.Macintosh || platform.isMacintosh && os === platform.OperatingSystem.Linux) {
32
return '/bin/bash';
33
}
34
35
if (!_TERMINAL_DEFAULT_SHELL_UNIX_LIKE) {
36
let unixLikeTerminal: string | undefined | null;
37
if (platform.isWindows) {
38
unixLikeTerminal = '/bin/bash'; // for WSL
39
} else {
40
unixLikeTerminal = env['SHELL'];
41
42
if (!unixLikeTerminal) {
43
try {
44
// It's possible for $SHELL to be unset, this API reads /etc/passwd. See https://github.com/github/codespaces/issues/1639
45
// Node docs: "Throws a SystemError if a user has no username or homedir."
46
unixLikeTerminal = userInfo().shell;
47
} catch (err) { }
48
}
49
50
if (!unixLikeTerminal) {
51
unixLikeTerminal = 'sh';
52
}
53
54
// Some systems have $SHELL set to /bin/false which breaks the terminal
55
if (unixLikeTerminal === '/bin/false') {
56
unixLikeTerminal = '/bin/bash';
57
}
58
}
59
_TERMINAL_DEFAULT_SHELL_UNIX_LIKE = unixLikeTerminal;
60
}
61
return _TERMINAL_DEFAULT_SHELL_UNIX_LIKE;
62
}
63
64
let _TERMINAL_DEFAULT_SHELL_WINDOWS: string | null = null;
65
async function getSystemShellWindows(): Promise<string> {
66
if (!_TERMINAL_DEFAULT_SHELL_WINDOWS) {
67
_TERMINAL_DEFAULT_SHELL_WINDOWS = (await getFirstAvailablePowerShellInstallation())!.exePath;
68
}
69
return _TERMINAL_DEFAULT_SHELL_WINDOWS;
70
}
71
72