From e41eae39ad6accdd1b02dedcb36e48c2b463d0b8 Mon Sep 17 00:00:00 2001 From: Liu Hui Date: Fri, 12 Sep 2025 12:43:54 +0200 Subject: [PATCH] Tramp: Refactor environment variable filtering to a separate function * lisp/net/tramp.el (tramp-local-environment-variable-p): New function. (Bug#79413) (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 @@ FUNCTION." (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 @@ will be used." ;; `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 @@ will be used." (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 @@ should be set connection-local.") (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 @@ should be set connection-local.") (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)