From eefb865e6eebbf31354ecd2db75f82af78788cdb Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 25 Oct 2023 09:49:23 +0530 Subject: [PATCH] kitten @ ls: Return environ of foreground process This is needed on macOS where we now run the shell via login and we aren't allowed to read the environ of login because its setuid. Fixes #6749 --- docs/changelog.rst | 2 ++ kitty/child.py | 7 ++++--- kitty/window.py | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index d49e930d4..731bb7e54 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -78,6 +78,8 @@ Detailed list of changes - Render Private Use Unicode symbols using two cells if the second cell contains a non-breaking space as well as a normal space +- kitten @ ls: Return the environment of the foreground process instead of the initial child process (:iss:`6749`) + 0.30.1 [2023-10-05] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/kitty/child.py b/kitty/child.py index 4669d0f98..bca6a49b4 100644 --- a/kitty/child.py +++ b/kitty/child.py @@ -214,10 +214,11 @@ def __init__( self.cwd = os.path.abspath(cwd) self.stdin = stdin self.env = env or {} + self.final_env:Dict[str, str] = {} self.is_default_shell = bool(self.argv and self.argv[0] == shell_path) self.should_run_via_run_shell_kitten = is_macos and self.is_default_shell - def final_env(self) -> Dict[str, str]: + def get_final_env(self) -> Dict[str, str]: from kitty.options.utils import DELETE_ENV_VAR env = default_env().copy() opts = fast_data_types.get_options() @@ -273,8 +274,7 @@ def fork(self) -> Optional[int]: os.set_inheritable(stdin_read_fd, True) else: stdin_read_fd = stdin_write_fd = -1 - final_env = self.final_env() - env = tuple(f'{k}={v}' for k, v in final_env.items()) + self.final_env = self.get_final_env() argv = list(self.argv) cwd = self.cwd if self.should_run_via_run_shell_kitten: @@ -311,6 +311,7 @@ def fork(self) -> Optional[int]: argv = ['/usr/bin/login', '-f', '-l', '-p', user] + argv self.final_exe = which(argv[0]) or argv[0] self.final_argv0 = argv[0] + env = tuple(f'{k}={v}' for k, v in self.final_env.items()) pid = fast_data_types.spawn( self.final_exe, cwd, tuple(argv), env, master, slave, stdin_read_fd, stdin_write_fd, ready_read_fd, ready_write_fd, tuple(handled_signals), kitten_exe(), opts.forward_stdio) diff --git a/kitty/window.py b/kitty/window.py index 95c90c45e..f9cc734b7 100644 --- a/kitty/window.py +++ b/kitty/window.py @@ -654,7 +654,7 @@ def as_dict(self, is_focused: bool = False, is_self: bool = False, is_active: bo 'pid': self.child.pid, 'cwd': self.child.current_cwd or self.child.cwd, 'cmdline': self.child.cmdline, - 'env': self.child.environ, + 'env': self.child.foreground_environ or self.child.environ or self.child.final_env, 'foreground_processes': self.child.foreground_processes, 'is_self': is_self, 'at_prompt': self.at_prompt,