macOS: import the user's login shell environment

I wrote this patch because when I use Kate on macOS, I've been annoyed with how environment variables are not carried into the terminal and LSP servers. I looked around and found a related bug that just enabled the ability to customize the PATH variable to fix, however there are other environment variables that are not carried in I'd like to have such as GOPATH and JAVA_HOME. I don't want to have to manually edit the PATH in Kate's preferences, and I believe most people downloading Kate would expect variables from their environment to be available. That's how it works on Linux, its just a difference on macOS due to the isolation that Apple has introduced.

GUI apps launched from Finder/launchd only inherit a minimal environment (PATH is just /usr/bin:/bin:/usr/sbin:/sbin). Tools resolved via PATH - LSP servers, git, the built-in terminal, ... - then cannot be found, and variables a user configures for their toolchains (GOPATH, JAVA_HOME, proxies, ...) are missing as well.

This builds on the earlier macOS PATH handling [1] by sourcing the value from the user's shell instead. Run the user's $SHELL as a login + interactive shell and import its environment. A login shell (-l) sources e.g. /etc/zprofile (path_helper) and ~/.zprofile, while an interactive shell (-i) sources ~/.zshrc; relevant variables commonly live in either. The shell is a child of this process, so it inherits launchd's variables and only adds/overrides the user's own, which makes re-applying its environment safe (only pure shell bookkeeping such as SHLVL or PWD is skipped). Unique markers delimit the output so it survives any banner a shell rc file may print, the child's stdin/stderr are silenced, and a 5s timeout keeps a misbehaving rc file from hanging startup.

[1] MR#1621

CCBUG: #490926

Merge request reports

Loading