From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Augusto Stoffel Newsgroups: gmane.emacs.devel Subject: Buffer-local process environments Date: Thu, 29 Apr 2021 12:56:36 +0200 Message-ID: <87eeets6jf.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="36356"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Thu Apr 29 12:58:29 2021 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lc4NI-0009IG-Gl for ged-emacs-devel@m.gmane-mx.org; Thu, 29 Apr 2021 12:58:28 +0200 Original-Received: from localhost ([::1]:48302 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lc4NH-0001an-Jg for ged-emacs-devel@m.gmane-mx.org; Thu, 29 Apr 2021 06:58:27 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:51222) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lc4Lb-0000CN-FH for emacs-devel@gnu.org; Thu, 29 Apr 2021 06:56:43 -0400 Original-Received: from mail-ed1-x535.google.com ([2a00:1450:4864:20::535]:34570) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lc4LZ-0002l4-Ad for emacs-devel@gnu.org; Thu, 29 Apr 2021 06:56:43 -0400 Original-Received: by mail-ed1-x535.google.com with SMTP id i3so52306104edt.1 for ; Thu, 29 Apr 2021 03:56:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:user-agent:mime-version; bh=dMRzCq2oOZJANlvFdRu9+JuIs/6Lh7HOWtsZ0tBC5uI=; b=CFFEPZHBRWxzksJb0OUrWJBR+a0cvjVdPvyBQrWQVLB1qdFKLG4RkSseObUhmqtuDU atzaJKA5OoHs/eGdm9DL2t3W4vKYACJkWaf0wk1OeuziyCoetCd+YHZ0QqCbbcsv2S0u O8SbMSfjNhu//cg60IAtqxgDn92XUcoSaOKz42djl7Px0D9NdkjrsDZ6nBkYAP2FoxnB gGfqec5kKpMXaj6fUBGSyrQ5DtMqBXduJ+3rfn22HCr+if3LmP2R9mjD5ikjqzocVj/A H9qDX9SrVxTatG3N1bV6+8XH9FD8L58hjuiT2oGdWQ73f1dqCFhVDTeFeFJLp+u3CtXh w7Cw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:user-agent :mime-version; bh=dMRzCq2oOZJANlvFdRu9+JuIs/6Lh7HOWtsZ0tBC5uI=; b=ZblS9XD7vNbAccenXEl12t9pvAFQq/Ppa4gFV2QN/0hr8eLhNVDAl6boo3k0dkqFh2 iso7u5Cyw9gWxbBYuWA3MxD/yGRK7QICLXEXUe+zv3jSZTZlCLN3m2ajkOA3mcGgGwB8 80TCyJTVyJFcPp/1MzO5n5N8CKrAHC3/MyBlP/wBj3j6eDAVjXBo+py/vWJ/JC/Em/L1 sPQZ1ytF6TsjN8kZI5ppGUGuQBSj7ltAcCm49UyeMDNtU1SdR64bRjUcPunmEuzAGHQp 1aMrUJyyQTMCm8cxHeLBK1ybHMYRC8EsrscVVK/z+X6s8E5M7m2A3fwQwTISA2OdWjlk 1kyQ== X-Gm-Message-State: AOAM532BcMTwGwnNYgTiZLBiExZqMDF2EtZEJRsNIh3rqDh9w/4X1mTe yitgFEOqCEHMqsbEuQ5rR6+vhvoOvWc= X-Google-Smtp-Source: ABdhPJxf40Tzy1rawPxtkYG0fdYuszJPIDxCcuJZzjygBXYjfIIdIgLB2jaNV70x2/L7XGP1bBnlIw== X-Received: by 2002:a05:6402:652:: with SMTP id u18mr2207821edx.388.1619693798976; Thu, 29 Apr 2021 03:56:38 -0700 (PDT) Original-Received: from ars3 ([2a02:908:2211:8540::68a]) by smtp.gmail.com with ESMTPSA id d1sm1647439eje.26.2021.04.29.03.56.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Apr 2021 03:56:38 -0700 (PDT) Received-SPF: pass client-ip=2a00:1450:4864:20::535; envelope-from=arstoffel@gmail.com; helo=mail-ed1-x535.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:268609 Archived-At: --=-=-= Content-Type: text/plain 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 --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-Make-compile-respect-buffer-local-process-environmen.patch >From ece87c73bcb5f8e2a59769cfe981c1486e19857b Mon Sep 17 00:00:00 2001 From: Augusto Stoffel 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 --=-=-=--