From eae96d9fcb8ea44f94eaf9e4511739a7f93aadf8 Mon Sep 17 00:00:00 2001 From: Michael Albinus Date: Wed, 6 May 2026 16:06:50 +0200 Subject: [PATCH] Handle long environment variables in Tramp oricesses * lisp/net/tramp-sh.el (tramp-sh-handle-make-process): Handle loooong environment variables. (Bug#80783) * test/lisp/net/tramp-tests.el (tramp-test33-environment-variables): Adapt test. --- lisp/net/tramp-sh.el | 14 ++++++++++++++ test/lisp/net/tramp-tests.el | 15 +++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lisp/net/tramp-sh.el b/lisp/net/tramp-sh.el index 8cade9ce095..92002b854b3 100644 --- a/lisp/net/tramp-sh.el +++ b/lisp/net/tramp-sh.el @@ -3093,6 +3093,15 @@ will be used." (setq uenv (cons elt uenv)))))) (env (setenv-internal env "INSIDE_EMACS" (tramp-inside-emacs) 'keep)) + ;; Environment is too large. Keep it here. + (eenv (and (> (apply #'+ (length env) (seq-map #'length env)) 2000) + env)) + (env (if (not eenv) env + `(,(concat + "INSIDE_EMACS=" (getenv-internal "INSIDE_EMACS" env)) + ,(concat "PS1=" (getenv-internal "PS1" env))))) + (eenv (setenv-internal eenv "INSIDE_EMACS" nil nil)) + (eenv (setenv-internal eenv "PS1" nil nil)) (command (when (stringp program) (format "cd %s && %s exec %s %s env %s %s" @@ -3207,6 +3216,11 @@ will be used." (widen) (delete-region mark (point-max)) (narrow-to-region (point-max) (point-max)) + ;; Send delayed environment. + (dolist (entry eenv) + (tramp-send-command + v (format + "export %s" (tramp-shell-quote-argument entry)))) ;; Now do it. (if command ;; Send the command. diff --git a/test/lisp/net/tramp-tests.el b/test/lisp/net/tramp-tests.el index 05c3f72229d..c76d6d54c3d 100644 --- a/test/lisp/net/tramp-tests.el +++ b/test/lisp/net/tramp-tests.el @@ -6519,7 +6519,7 @@ INPUT, if non-nil, is a string sent to the process." ;; (should (= 11 (point))))))))))))) ))))))))) -;; This test is inspired by Bug#23952. +;; This test is inspired by Bug#23952 and Bug#80783. (ert-deftest tramp-test33-environment-variables () "Check that remote processes set / unset environment variables properly." :tags '(:expensive-test) @@ -6611,7 +6611,18 @@ INPUT, if non-nil, is a string sent to the process." ;; We must suppress "_=VAR...". (funcall this-shell-command-to-string - "printenv | grep -v PS1 | grep -v _="))))))))) + "printenv | grep -v PS1 | grep -v _=")))))) + + ;; Handle looooong environment variables. Bug#80783. + ;; FIXME: Make it also work in the synchronous case. + (unless (or (eq this-shell-command-to-string 'shell-command-to-string) + (tramp-direct-async-process-p)) + (let* ((bad (concat envvar "=" (make-string 2024 ?x))) + (process-environment + (cl-list* bad bad bad bad process-environment))) + (should + (string-match-p + "foo" (funcall this-shell-command-to-string "echo foo")))))))) (tramp--test-deftest-direct-async-process tramp-test33-environment-variables)