Subject: Re: bug#52178: 29.0.50; process-environment not used in async-shell-command

From: Lars Ingebrigtsen <[email-masked]>
Date: Mon, 29 Nov 2021 15:38:12 +0100

Damien Merenne <[email-masked]> writes:

> When running an async process, the value of `process-environment' is
> lost, in 'emacs -Q' scratch buffer:
>
>     (let ((process-environment "EDITOR=test"))
>       (shell-command "env &"))

I think you mean

    (let ((process-environment '("EDITOR=test")))
      (shell-command "env &"))

> and the output is my normal environment, not the limited one. When
> removing the &, it works okay. I managed to pinpoint the problem to the
> call to `with-current-buffer' in simple.el:4229. As process-environment
> is buffer local, it is not passed in the process buffer.

process-environment is not buffer-local by default, though, so the test
case works fine normally.  Do you have a complete case to reproduce the
problem, starting from "emacs -Q"?

> This patch fixes it but I have no idea if this is the correct way to
> fix it:

The patch wasn't complete, so I've recreated it and included below for
reference.

It makes sense to me, but I'd like to know where this fixes anything
before applying.

diff --git a/lisp/simple.el b/lisp/simple.el
index 84928caa31..e38988f916 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -4194,7 +4194,8 @@ shell-command
                               (or output-buffer shell-command-buffer-name-async)))
                      (bname (buffer-name buffer))
                      (proc (get-buffer-process buffer))
-                     (directory default-directory))
+                     (directory default-directory)
+                     (process-env process-environment))
 		;; Remove the ampersand.
 		(setq command (substring command 0 (match-beginning 0)))
 		;; Ask the user what to do with already running process.
@@ -4237,7 +4238,7 @@ shell-command
                                 (format "COLUMNS=%d"
                                         async-shell-command-width)))
                           (comint-term-environment)
-                          process-environment)))
+                          process-env)))
 		    (setq proc
 			  (start-process-shell-command "Shell" buffer command)))
 		  (setq mode-line-process '(":%s"))

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no