Skip to content

Commit

Permalink
app: Fix for MacOS auth plugins
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
illume committed May 11, 2024
1 parent 861cdcb commit 7006109
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions app/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { IpcMainEvent, MenuItemConstructorOptions } from 'electron/main';
import log from 'electron-log';
import find_process from 'find-process';
import fs from 'fs';
import { spawnSync } from 'node:child_process';
import { userInfo } from 'node:os';
import open from 'open';
import path from 'path';
import url from 'url';
Expand All @@ -16,6 +18,42 @@ import windowSize from './windowSize';

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

/**
* On MacOS apps do not get the same environment variables as the terminal.
*
* However we want the same PATH as the shell to run the users terminal programs.
*/
function addPathFromShellToEnvOnMac() {
if (process.platform !== 'darwin') {
return;
}

let defaultShell;
try {
defaultShell = userInfo().shell || '/bin/zsh';
} catch (error) {
defaultShell = '/bin/zsh';
}

// login interactive shell
// f option is to prevent menu on zshell when user has no config.
// DISABLE_AUTO_UPDATE is to prevent the shell from updating.
const env = { ...process.env, DISABLE_AUTO_UPDATE: 'true' };
const result = spawnSync(defaultShell, ['--login', '-fic', 'echo $PATH'], {
env: env,
encoding: 'utf-8',
timeout: 8000, // in case it's stuck
});

if (result.status === 0) {
const path = result.stdout.toString();
process.env.PATH = path;
} else {
console.error('Failed to get shell PATH, just using process.env.PATH');
}
}
addPathFromShellToEnvOnMac();

const args = yargs
.command('$0 [kubeconfig]', '', yargs => {
yargs
Expand Down

0 comments on commit 7006109

Please sign in to comment.