Subject: Re: bug#79413: [PATCH] Fix path and environment in remote Python shell

From: Liu Hui <[email-masked]>
Date: Fri, 12 Sep 2025 17:58:04 +0800

Michael Albinus <[email-masked]> writes:

> Liu Hui <[email-masked]> writes:
>
> Hi,
>
>> How about the following change? It doesn't change the default behavior
>> and allows the caller to override tramp-local-environment-variable-p
>> when necessary.
>
> Looks OK. Pls provide a complete patch, including commit
> messages. tramp-androidsu-handle-make-process in tramp-android.el must
> be changed as well.
>
> Perhaps add also a comment on top of tramp-local-environment-variable-p,
> explaining the reason for this function. I'm not decided yet whether we
> shall document it in the Tramp manual. If we do, we would need a more
> general approach I guess.

Patch is attached. Thanks.

>From cd1248abcef17298330318ce9de707bdcebb0233 Mon Sep 17 00:00:00 2001
From: Liu Hui <[email-masked]>
Date: Fri, 12 Sep 2025 12:55:11 +0800
Subject: [PATCH] Tramp: Refactor environment variable filtering to a separate
 function

* lisp/net/tramp.el (tramp-local-environment-variable-p): New
function.
(tramp-handle-make-process):
* lisp/net/tramp-sh.el (tramp-sh-handle-make-process)
(tramp-sh-handle-process-file):
* lisp/net/tramp-androidsu.el
(tramp-androidsu-handle-make-process): Use
`tramp-local-environment-variable-p'.
---
 lisp/net/tramp-androidsu.el |  4 +---
 lisp/net/tramp-sh.el        |  5 ++---
 lisp/net/tramp.el           | 16 +++++++++++++---
 3 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/lisp/net/tramp-androidsu.el b/lisp/net/tramp-androidsu.el
index a593833a836..6cc3f14381d 100644
--- a/lisp/net/tramp-androidsu.el
+++ b/lisp/net/tramp-androidsu.el
@@ -311,9 +311,7 @@ tramp-androidsu-handle-make-process
 		  (when
 		      (and
 		       (string-search "=" elt)
-		       (not
-			(member
-			 elt (default-toplevel-value 'process-environment))))
+		       (not (tramp-local-environment-variable-p elt)))
 		    (setq env (cons elt env)))))
 	   ;; Add remote path if exists.
 	   (env (let ((remote-path (string-join (tramp-get-remote-path v) ":")))
diff --git a/lisp/net/tramp-sh.el b/lisp/net/tramp-sh.el
index 9d13cdc3a2d..0a454fed69b 100644
--- a/lisp/net/tramp-sh.el
+++ b/lisp/net/tramp-sh.el
@@ -3052,8 +3052,7 @@ tramp-sh-handle-make-process
 	     ;; `process-environment'.
 	     env uenv
 	     (env (dolist (elt (cons prompt process-environment) env)
-		    (or (member
-			 elt (default-toplevel-value 'process-environment))
+		    (or (tramp-local-environment-variable-p elt)
 			(if (string-search "=" elt)
 			    (setq env (append env `(,elt)))
 			  (setq uenv (cons elt uenv))))))
@@ -3288,7 +3287,7 @@ tramp-sh-handle-process-file
 			       (cons program args) " "))
       ;; We use as environment the difference to toplevel `process-environment'.
       (dolist (elt process-environment)
-        (or (member elt (default-toplevel-value 'process-environment))
+        (or (tramp-local-environment-variable-p elt)
             (if (string-search "=" elt)
                 (setq env (append env `(,elt)))
               (setq uenv (cons elt uenv)))))
diff --git a/lisp/net/tramp.el b/lisp/net/tramp.el
index ad768f9e038..a27e8b8fbe8 100644
--- a/lisp/net/tramp.el
+++ b/lisp/net/tramp.el
@@ -5347,6 +5347,18 @@ tramp-direct-async-process-p
 	 (or (not (stringp buffer)) (not (tramp-tramp-file-p buffer)))
 	 (or (not (stringp stderr)) (not (tramp-tramp-file-p stderr))))))
 
+;; This function is used by tramp-*-handle-make-process and
+;; tramp-sh-handle-process-file to filter local environment variables
+;; that should not be propagated remotely.  Users can override this
+;; function if necessary, for example, to ensure that a specific
+;; environment variable is applied to remote processes, whether it
+;; exists locally or not.
+(defun tramp-local-environment-variable-p (arg)
+  "Return non-nil if ARG exists in default `process-environment'.
+Tramp does not propagate local environment variables in remote
+processes."
+  (member arg (default-toplevel-value 'process-environment)))
+
 (defun tramp-handle-make-process (&rest args)
   "An alternative `make-process' implementation for Tramp files."
   (tramp-skeleton-make-process args nil nil
@@ -5365,9 +5377,7 @@ tramp-handle-make-process
 	   (env (dolist (elt process-environment env)
 		  (when (and
 			 (string-search "=" elt)
-			 (not
-			  (member
-			   elt (default-toplevel-value 'process-environment))))
+			 (not (tramp-local-environment-variable-p elt)))
 		    (setq env (cons elt env)))))
 	   ;; Add remote path if exists.
 	   (env (if-let* ((sh-file-name-handler-p)
-- 
2.25.1