unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Augusto Stoffel <arstoffel@gmail.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: Michael Albinus <michael.albinus@gmx.de>, emacs-devel@gnu.org
Subject: Re: Buffer-local process environments
Date: Thu, 29 Apr 2021 19:26:05 +0200	[thread overview]
Message-ID: <8735v99f4i.fsf@gmail.com> (raw)
In-Reply-To: <jwvmtthdwuv.fsf-monnier+emacs@gnu.org> (Stefan Monnier's message of "Thu, 29 Apr 2021 10:02:50 -0400")

[-- Attachment #1: Type: text/plain, Size: 3392 bytes --]

On Thu, 29 Apr 2021 at 10:02, Stefan Monnier <monnier@iro.umontreal.ca> wrote:

>> So why not make buffer-local process environments an official thing?
>
> Sounds fine to me.
>
> To fix cases like `compile` or `run-python`, I think patches would be
> welcome regardless of what is decided in this discussion: those
> behaviors look like plain bugs to me.

Great!

>
>> # Local Variables:
>> # path: "~/path/to/some/virtualenv/bin"
>> # env: "VIRTUAL_ENV=$HOME/path/to/some/virtualenv"
>> # env: "LANG=C"
>> # End:
>
> A few comments:
> - I don't much like "special local vars" (like `eval` and `mode`), so if
>   we can find a more general solution (i.e. one that can be useful for
>   other settings), that would be better.  Maybe
>
>     # push exec-path: "~/path/to/some/virtualenv/bin"

Some notation equivalent to

# eval: (add-to-list (make-local-variable var) value)

might be handy, as well as checkable for safety.  But in the env var
case it would be important to run `substitute-env-vars', so it's a
special case.

>
> - both `exec-path` and `process-environment` are "dangerous" variables,
>   so encouraging such uses sounds rather risky.

True, but setting the environment for buffers in a project is something
one needs to do all the time.  After confirming once, it has to get out
of the way and just happen automatically.  The details of how this works
of course are worth discussing.

>
> - I'd write `path` above as `exec-path` or `PATH`, or even `$PATH`,
>   otherwise it's unclear which "path" is meant.
>
>> --- a/lisp/progmodes/compile.el
>> +++ b/lisp/progmodes/compile.el
>> @@ -1779,6 +1779,8 @@ compilation-start
>>  	    (replace-regexp-in-string "-mode\\'" "" (symbol-name mode))))
>>  	 (thisdir default-directory)
>>  	 (thisenv compilation-environment)
>> +         (this-process-environment process-environment)
>> +         (env-buffer (when (local-variable-p 'process-environment) (buffer-name)))
>>  	 outwin outbuf)
>>      (with-current-buffer
>>  	(setq outbuf
>> @@ -1856,6 +1858,9 @@ compilation-start
>>  		"; default-directory: "
>>                  (prin1-to-string (abbreviate-file-name default-directory))
>>  		" -*-\n"
>> +                (if env-buffer
>> +                    (format "Process environment is local to buffer `%s'\n" env-buffer)
>> +                  "")
>>  		(format "%s started at %s\n\n"
>>  			mode-name
>>  			(substring (current-time-string) 0 19))
>> @@ -1875,7 +1880,7 @@ compilation-start
>>                (and (derived-mode-p 'comint-mode)
>>                     (comint-term-environment))
>>  	      (list (format "INSIDE_EMACS=%s,compile" emacs-version))
>> -	      (copy-sequence process-environment))))
>> +	      (copy-sequence this-process-environment))))
>>          (setq-local compilation-arguments
>>                      (list command mode name-function highlight-regexp))
>>          (setq-local revert-buffer-function 'compilation-revert-buffer)
>
> Does this work correctly when you `M-x recompile`?

I wasn't aware of recompile.  The attached patch works with it.  The
attached version breaks with Tramp, but I think this is a Tramp bug.

In fact, even without the patch, if I make my `process-environment'
buffer local, then `M-x cd' to a ssh path, then do `M-! env', I see a
mixture of my machine's environment and the remote machine's environment.
For instance, PATH has the local machine value.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Make-compile-respect-buffer-local-process-environmen.patch --]
[-- Type: text/x-patch, Size: 1508 bytes --]

From f803ce025b4161bea68f115ac50ba1174ef863e3 Mon Sep 17 00:00:00 2001
From: Augusto Stoffel <arstoffel@gmail.com>
Date: Thu, 29 Apr 2021 12:45:04 +0200
Subject: [PATCH] Make `compile' respect buffer-local process environment

* lisp/progmodes/compile.el (compilation-start): Use
`process-environment' from original buffer in the compilation process.
---
 lisp/progmodes/compile.el | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/lisp/progmodes/compile.el b/lisp/progmodes/compile.el
index 7a02c3a896..52873d2f29 100644
--- a/lisp/progmodes/compile.el
+++ b/lisp/progmodes/compile.el
@@ -1779,6 +1779,9 @@ compilation-start
 	    (replace-regexp-in-string "-mode\\'" "" (symbol-name mode))))
 	 (thisdir default-directory)
 	 (thisenv compilation-environment)
+         (bufferenv (if (local-variable-p 'process-environment)
+                        process-environment
+                      'global))
 	 outwin outbuf)
     (with-current-buffer
 	(setq outbuf
@@ -1846,6 +1849,9 @@ compilation-start
         ;; NB: must be done after (funcall mode) as that resets local variables
         (setq-local compilation-directory thisdir)
         (setq-local compilation-environment thisenv)
+        (if (eq bufferenv 'global)
+            (kill-local-variable 'process-environment)
+          (setq-local process-environment bufferenv))
 	(if highlight-regexp
             (setq-local compilation-highlight-regexp highlight-regexp))
         (if (or compilation-auto-jump-to-first-error
-- 
2.30.2


  reply	other threads:[~2021-04-29 17:26 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-29 10:56 Buffer-local process environments Augusto Stoffel
2021-04-29 12:30 ` Eli Zaretskii
2021-04-29 12:40   ` Augusto Stoffel
2021-04-29 12:52     ` Eli Zaretskii
2021-04-29 13:06       ` Augusto Stoffel
2021-04-29 14:02 ` Stefan Monnier
2021-04-29 17:26   ` Augusto Stoffel [this message]
2021-04-29 17:34     ` Michael Albinus
2021-04-30  7:29       ` Augusto Stoffel
2021-04-30  7:48         ` Michael Albinus
2021-04-30 15:19           ` Augusto Stoffel
2021-04-30 15:51             ` Michael Albinus
2021-05-02  6:13               ` Augusto Stoffel
2021-05-08 17:51                 ` Michael Albinus
2021-05-09  5:06                   ` Augusto Stoffel
2021-05-09 16:38                     ` Michael Albinus
2021-08-28 12:28                       ` [PATCH] " Augusto Stoffel
2021-08-28 12:37                         ` Eli Zaretskii
2021-08-28 12:55                           ` Augusto Stoffel
2021-09-01 10:42                             ` Stephen Leake
2021-09-01 10:56                               ` Augusto Stoffel
2021-09-01 22:38                                 ` Stephen Leake
2021-09-02  7:14                                   ` Augusto Stoffel
2021-09-06 15:17                                     ` Stephen Leake
2021-08-28 14:06                           ` Arthur Miller
2021-08-28 14:33                             ` Eli Zaretskii
2021-08-28 15:27                               ` Arthur Miller
2021-08-28 15:38                                 ` Eli Zaretskii
2021-08-28 16:48                                   ` Arthur Miller
2021-08-28 15:39                                 ` Augusto Stoffel
2021-08-28 16:43                                   ` Arthur Miller
2021-08-28 12:47                         ` Michael Albinus
2021-08-28 12:59                           ` Augusto Stoffel
2021-08-28 13:18                             ` Michael Albinus
2021-08-28 13:54                               ` Augusto Stoffel
2021-08-28 14:05                               ` Stefan Monnier
2021-08-28 15:19                                 ` Augusto Stoffel
2021-04-30 15:32           ` Augusto Stoffel
2021-04-30 15:55             ` Michael Albinus
2021-04-29 15:37 ` Michael Albinus
2021-04-29 17:31   ` Augusto Stoffel
2021-04-29 17:44     ` Michael Albinus
2021-04-30  7:00       ` Augusto Stoffel
2021-04-30  7:25         ` Michael Albinus
2021-05-02 13:45 ` Stephen Leake

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8735v99f4i.fsf@gmail.com \
    --to=arstoffel@gmail.com \
    --cc=emacs-devel@gnu.org \
    --cc=michael.albinus@gmx.de \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).