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: Re: Buffer-local process environments Date: Thu, 29 Apr 2021 19:26:05 +0200 Message-ID: <8735v99f4i.fsf@gmail.com> References: <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="17403"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) Cc: Michael Albinus , emacs-devel@gnu.org To: Stefan Monnier Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Thu Apr 29 19:31:42 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 1lcAVo-0004OB-PC for ged-emacs-devel@m.gmane-mx.org; Thu, 29 Apr 2021 19:31:40 +0200 Original-Received: from localhost ([::1]:46470 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lcAVn-00072F-O3 for ged-emacs-devel@m.gmane-mx.org; Thu, 29 Apr 2021 13:31:39 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:35030) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lcAQX-00024Q-IU for emacs-devel@gnu.org; Thu, 29 Apr 2021 13:26:13 -0400 Original-Received: from mail-ed1-x52a.google.com ([2a00:1450:4864:20::52a]:34612) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lcAQT-0007jL-Sy for emacs-devel@gnu.org; Thu, 29 Apr 2021 13:26:13 -0400 Original-Received: by mail-ed1-x52a.google.com with SMTP id i3so53823449edt.1 for ; Thu, 29 Apr 2021 10:26:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version; bh=JaJvh9HJKhtQI0Z/5cSWNs1/H2AWNO6HjZrYaZ/Gnb4=; b=phMyq5ZZ6jwqYv6tXTTZffGnfPddEilA2DC8NKOA+VWazir+Cv6s8ARDInOcXUU9MV pcfEWzX2B5SI+iHEYGCoKXD7GurJixZ1FzOGgvxBVreBN4nf12eftfiKW+zowTlQm5GR 8PiphmSmWJD6czmzlBUSefRSDzt7exBKFzIo0I+8KrYpmC+tpi8YoEzPNCyMJX4xlaca RBl+G6m3BgZfH+1Q0BmRhHD9Xc0aQfJOinsN9X13vglqucJgK89Ny3yHmFtWfNGUqGO7 4UVWCpWI1LxXxRG/B30833Bb/YhvOQwr1T0F1SqIPAv2nZf7mbU8XpIWvFcL3omY3Ulp on8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version; bh=JaJvh9HJKhtQI0Z/5cSWNs1/H2AWNO6HjZrYaZ/Gnb4=; b=RqtYAmnxANIJwxcnGyGKoOh25vHvaF8H1JdqG9yy15GkspMp0mGwvtPKreM+JNoL8o 3btUNQ0s06SXxrye/1zqX+okUviqvIFkaXdDEtpe1PwR3m/i1HMwVMcCGZMTDonA4WiX AwrlEevup/8xe6GQbGvkMSw5JUCXBVlte2G8FMgAWd5brCFkz1VIgW5qOJYcn7Yav3Ll O51msXGkY8jYsrg+DqPXuHSHuB7ut6MzzZu4WdIzigMLJy6WpqxA8BwcmlX/7Q5GJfDW e+yfnonstP3Z7hvl2H2EroHb3piUtBlIVtqNBpZxQN1/5VQtjZgP0JzXl0n2Oe8kz/ux 78EQ== X-Gm-Message-State: AOAM532k2kNwx1X/5wS1fOwUPcqxJtxy/BlJmy4eq2fOnGgjxX5X0fpw N3g7cpZ3Dvk4vFv8OtP3pPX5sWaFqOiCXg== X-Google-Smtp-Source: ABdhPJzKmKWS7zC/udl5t5fNwLsdBr/zYeUA6dlF4upuWqW7WW2LWyZsK0uV1yeo03D3LddTDk7cpg== X-Received: by 2002:a05:6402:1255:: with SMTP id l21mr811453edw.362.1619717168156; Thu, 29 Apr 2021 10:26:08 -0700 (PDT) Original-Received: from ars3 ([2a02:908:2211:8540::68a]) by smtp.gmail.com with ESMTPSA id y25sm387158ejb.34.2021.04.29.10.26.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Apr 2021 10:26:07 -0700 (PDT) In-Reply-To: (Stefan Monnier's message of "Thu, 29 Apr 2021 10:02:50 -0400") Received-SPF: pass client-ip=2a00:1450:4864:20::52a; envelope-from=arstoffel@gmail.com; helo=mail-ed1-x52a.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:268625 Archived-At: --=-=-= Content-Type: text/plain On Thu, 29 Apr 2021 at 10:02, Stefan Monnier 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. --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-Make-compile-respect-buffer-local-process-environmen.patch >From f803ce025b4161bea68f115ac50ba1174ef863e3 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 | 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 --=-=-=--