Source: python-libtmux
Version: 0.30.2-1
Severity: normal
Tags: patch upstream
X-Debbugs-Cc: [email protected]

If new_session() is invoked with both the window_command and environment
parameters specified, it will pass the environment variables in a way
that makes tmux interpret them as arguments to the program to start,
not as arguments to the `new-session` command itself.

I have sent the patch to the author via private e-mail.

-- System Information:
Debian Release: trixie/sid
  APT prefers testing
  APT policy: (990, 'testing')
Architecture: amd64 (x86_64)
Foreign Architectures: i386

Kernel: Linux 6.11.10-amd64 (SMP w/16 CPU threads; PREEMPT)
Kernel taint flags: TAINT_WARN
Locale: LANG=bg_BG.UTF-8, LC_CTYPE=bg_BG.UTF-8 (charmap=UTF-8), LANGUAGE not set
Shell: /bin/sh linked to /usr/bin/dash
Init: systemd (via /run/systemd/system)
LSM: AppArmor: enabled
Description: Fix passing both window command and environment
 If the `-e this=that` options are passed after a window command,
 tmux (correctly, IMHO) interprets them as options to be passed to
 the command itself, does not change the environment, and passes them
 through to the command, which does not usually expect to get
 a whole lot of `-e this=that -e other=something` options.
 .
 For a demonstration, see https://gitlab.com/-/snippets/4782141
Origin: 
https://github.com/ppentchev/libtmux/commit/5b312f44974d06a57c4c7c6a4c3ccf60d2b52c78
Forwarded: yes
Author: Peter Pentchev <[email protected]>
Last-Update: 2024-12-16

--- a/src/libtmux/server.py
+++ b/src/libtmux/server.py
@@ -499,9 +499,6 @@
         if y is not None:
             tmux_args += ("-y", y)
 
-        if window_command:
-            tmux_args += (window_command,)
-
         if environment:
             if has_gte_version("3.2"):
                 for k, v in environment.items():
@@ -511,6 +508,9 @@
                     "Environment flag ignored, tmux 3.2 or newer required.",
                 )
 
+        if window_command:
+            tmux_args += (window_command,)
+
         proc = self.cmd("new-session", *tmux_args)
 
         if proc.stderr:
--- a/tests/test_server.py
+++ b/tests/test_server.py
@@ -1,7 +1,9 @@
 """Test for libtmux Server object."""
 
 import logging
+import os
 import subprocess
+import time
 
 import pytest
 
@@ -124,6 +126,28 @@
 
     pane_start_command = pane.pane_start_command
     assert pane_start_command is not None
+
+    if has_gte_version("3.2"):
+        assert pane_start_command.replace('"', "") == cmd
+    else:
+        assert pane_start_command == cmd
+
+
+def test_new_session_shell_env(server: Server) -> None:
+    """Verify ``Server.new_session`` creates valid session running w/ 
command."""
+    cmd = "sleep 1m"
+    env = dict(os.environ)
+    mysession = server.new_session(
+        "test_new_session_env", window_command=cmd, environment=env
+    )
+    time.sleep(0.1)
+    window = mysession.windows[0]
+    pane = window.panes[0]
+    assert mysession.session_name == "test_new_session_env"
+    assert server.has_session("test_new_session_env")
+
+    pane_start_command = pane.pane_start_command
+    assert pane_start_command is not None
 
     if has_gte_version("3.2"):
         assert pane_start_command.replace('"', "") == cmd

Attachment: signature.asc
Description: PGP signature

Reply via email to