unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Buffer-local process environments
@ 2021-04-29 10:56 Augusto Stoffel
  2021-04-29 12:30 ` Eli Zaretskii
                   ` (3 more replies)
  0 siblings, 4 replies; 45+ messages in thread
From: Augusto Stoffel @ 2021-04-29 10:56 UTC (permalink / raw)
  To: emacs-devel

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

When developing software, it's often not possible or desirable to
install compilers and related tools globally, so (barring the
availability of fancy build tools) one has to fiddle with environment
variables.

Currently it's quite hard to set up Emacs to interact correctly with
external tools on a per-project basis, because there is no unified way
adjust the environment.  For instance:

- `M-x compile' has the `compilation-environment' variable.
- python-mode uses `python-shell-virtualenv-root' when launching a
  REPL.
- Eglot provides no other mechanism than setting `exec-path' and
  `process-environment' buffer-locally.
- etc.

Of course, the buffer-local variable approach will work with any
command that doesn't switch buffers, even if that command invents its
own method to adjust the environment.

So why not make buffer-local process environments an official thing?
By this I mean:

1. Fix commands that start external processes after switching buffers
   to do what the user means.

2. Provide a convenient way to set the process environment buffer
   locally.

Point 1 is simple.  I've attached a patch for `compile' as an example.
In the Python REPL case, I'm not sure whether it would be better to
just adapt `run-python' or make a change in comint itself.

Proposal for point 2
--------------------

Add two more special keywords to the file local variable mechanism
(besides `mode', `eval', etc):

- `env' to add an entry to `process-environment'.
- `path' to add an entry to `exec-path' and modify the PATH variable at
  the same time.

Hence, the local variables section of a Python file might look like
this:

# Local Variables:
# path: "~/path/to/some/virtualenv/bin"
# env: "VIRTUAL_ENV=$HOME/path/to/some/virtualenv"
# env: "LANG=C"
# End:

Of course, in practice it's more useful to include this in
.dir-locals.el, but the mechanism is the same.

Closing remarks
---------------

As an indication that this is a real problem, let me point out a list
of MELPA packages just to deal with Python virtualenvs:

- auto-virtualenv
- auto-virtualenvwrapper
- conda
- pungi
- python-environment
- pyvenv
- virtualenv
- virtualenvwrapper

I've tried a couple of those and was never satisfied.

My proposal here is inspired by the `inherienv' [1] and `envrc' [2]
MELPA packages.  The former provides an advice to solve point 1, the
latter solves point 2 by means of an external program called direnv.

[1]: https://github.com/purcell/inheritenv/
[2]: https://github.com/purcell/envrc


[-- 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: 1877 bytes --]

From ece87c73bcb5f8e2a59769cfe981c1486e19857b 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 | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/lisp/progmodes/compile.el b/lisp/progmodes/compile.el
index 7a02c3a896..ee73f03c93 100644
--- 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)
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 45+ messages in thread

end of thread, other threads:[~2021-09-06 15:17 UTC | newest]

Thread overview: 45+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

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).