Skip to content

Commit 7006109

Browse files
committed
app: Fix for MacOS auth plugins
On at least MacOS, GUI apps do not take on the shell paths. However they are needed for kubectl auth plugins, and perhaps other binaries like minikube Fixes #1885 Signed-off-by: René Dudfield <[email protected]>
1 parent 861cdcb commit 7006109

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

app/electron/main.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { IpcMainEvent, MenuItemConstructorOptions } from 'electron/main';
77
import log from 'electron-log';
88
import find_process from 'find-process';
99
import fs from 'fs';
10+
import { spawnSync } from 'node:child_process';
11+
import { userInfo } from 'node:os';
1012
import open from 'open';
1113
import path from 'path';
1214
import url from 'url';
@@ -16,6 +18,42 @@ import windowSize from './windowSize';
1618

1719
dotenv.config({ path: path.join(process.resourcesPath, '.env') });
1820

21+
/**
22+
* On MacOS apps do not get the same environment variables as the terminal.
23+
*
24+
* However we want the same PATH as the shell to run the users terminal programs.
25+
*/
26+
function addPathFromShellToEnvOnMac() {
27+
if (process.platform !== 'darwin') {
28+
return;
29+
}
30+
31+
let defaultShell;
32+
try {
33+
defaultShell = userInfo().shell || '/bin/zsh';
34+
} catch (error) {
35+
defaultShell = '/bin/zsh';
36+
}
37+
38+
// login interactive shell
39+
// f option is to prevent menu on zshell when user has no config.
40+
// DISABLE_AUTO_UPDATE is to prevent the shell from updating.
41+
const env = { ...process.env, DISABLE_AUTO_UPDATE: 'true' };
42+
const result = spawnSync(defaultShell, ['--login', '-fic', 'echo $PATH'], {
43+
env: env,
44+
encoding: 'utf-8',
45+
timeout: 8000, // in case it's stuck
46+
});
47+
48+
if (result.status === 0) {
49+
const path = result.stdout.toString();
50+
process.env.PATH = path;
51+
} else {
52+
console.error('Failed to get shell PATH, just using process.env.PATH');
53+
}
54+
}
55+
addPathFromShellToEnvOnMac();
56+
1957
const args = yargs
2058
.command('$0 [kubeconfig]', '', yargs => {
2159
yargs

0 commit comments

Comments
 (0)