From 87b218a40d109bc8ebbaeb83688d7bf4cd0a14bd Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 23 Apr 2025 22:14:06 +0530 Subject: [PATCH] Preserve env vars from onvoking env when creating new os window via single instance --- docs/changelog.rst | 3 +++ kitty/boss.py | 4 +++- kitty/child.py | 4 ++-- kitty/session.py | 3 ++- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index fc26f7091..df659b688 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -128,6 +128,9 @@ Detailed list of changes - Fix a regression in 0.36.0 that caused using = with single letter options to no longer work correctly (:iss:`8556`) +- Single instance: Preserve environment variables from invoking environment in + newly created window (:disc:`8567`) + 0.41.1 [2025-04-03] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/kitty/boss.py b/kitty/boss.py index 78dc52cd0..02c415d6f 100644 --- a/kitty/boss.py +++ b/kitty/boss.py @@ -870,8 +870,10 @@ class Boss: args.session = '' if not os.path.isabs(args.directory): args.directory = os.path.join(data['cwd'], args.directory) + from .child import process_env + clean_env = process_env(data['environ']) focused_os_window = os_window_id = 0 - for session in create_sessions(opts, args, respect_cwd=True): + for session in create_sessions(opts, args, respect_cwd=True, env_when_no_session=clean_env): if not session.has_non_background_processes: # background only do not create an OS Window from .launch import LaunchSpec, launch diff --git a/kitty/child.py b/kitty/child.py index cfe62b03b..a958a7130 100644 --- a/kitty/child.py +++ b/kitty/child.py @@ -145,8 +145,8 @@ def environ_of_process(pid: int) -> dict[str, str]: return parse_environ_block(_environ_of_process(pid)) -def process_env() -> dict[str, str]: - ans = dict(os.environ) +def process_env(env: dict[str, str] | None = None) -> dict[str, str]: + ans = dict(os.environ if env is None else env) ssl_env_var = getattr(sys, 'kitty_ssl_env_var', None) if ssl_env_var is not None: ans.pop(ssl_env_var, None) diff --git a/kitty/session.py b/kitty/session.py index 337f31a3f..00b8342ef 100644 --- a/kitty/session.py +++ b/kitty/session.py @@ -241,6 +241,7 @@ def create_sessions( cwd_from: Optional['CwdRequest'] = None, respect_cwd: bool = False, default_session: str | None = None, + env_when_no_session: dict[str, str] | None = None, ) -> Iterator[Session]: if args and args.session: if args.session == "none": @@ -276,6 +277,6 @@ def create_sessions( cmd = args.args if args and args.args else resolved_shell(opts) from kitty.tabs import SpecialWindow cwd: str | None = args.directory if respect_cwd and args else None - special_window = SpecialWindow(cmd, cwd_from=cwd_from, cwd=cwd, hold=bool(args and args.hold)) + special_window = SpecialWindow(cmd, cwd_from=cwd_from, cwd=cwd, env=env_when_no_session, hold=bool(args and args.hold)) ans.add_special_window(special_window) yield ans