From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Evgenii Klimov via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#72849: [PATCH] Keep project's exec-path during with-temp-buffer call Date: Thu, 29 Aug 2024 23:51:29 +0100 Message-ID: <87ed677xji.fsf@lipklim.org> References: <87v7zlo8yy.fsf@lipklim.org> <86y14ghmm1.fsf@gnu.org> Reply-To: Evgenii Klimov 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="34989"; mail-complaints-to="usenet@ciao.gmane.io" Cc: kobarity , 72849@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Aug 30 00:53:35 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@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 1sjo1F-0008xl-13 for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 30 Aug 2024 00:53:33 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sjo0r-00084M-PL; Thu, 29 Aug 2024 18:53:09 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sjo0p-000845-UH for bug-gnu-emacs@gnu.org; Thu, 29 Aug 2024 18:53:08 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sjo0p-0007ZV-LP for bug-gnu-emacs@gnu.org; Thu, 29 Aug 2024 18:53:07 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:Date:References:In-Reply-To:From:To:Subject; bh=Ip7cSZheQO2n06m7S65Y9sPrF+YrBLc8VquQGTWEwd0=; b=fBfvCc8aditDSrgqHY8o+H5o7+nizgaUr2w/AEl/Oslek+LBaQvsm18B41rcUG9s46/kPVeO6WVM4TS6/DYqy101kbwHdmAMFVnCjPRMo67Xx1iWimrIV/XQ5Jb4Fg3iG02oaIJiuZgs9jKPd/yq/v9oV7XMOSsCVCW1QnKQhzlxZnq/KS+44INZNOThO9GDEJ4vTV4TPe9op5wFJehxyDn5s55rFhm98K/7vhVyBpzx4jXxFs1jFBoZJ4cmvfnAks91YPA/GuHGsizhaYKDSt3Hs+sLCJJySo1j8m8CSHRvOT+XzeWxhsYTCunKfRqCtLdcYSPCsy5CjhzsAEGhPQ==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1sjo1i-0007qW-3p for bug-gnu-emacs@gnu.org; Thu, 29 Aug 2024 18:54:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Evgenii Klimov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 29 Aug 2024 22:54:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 72849 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 72849-submit@debbugs.gnu.org id=B72849.172497199030078 (code B ref 72849); Thu, 29 Aug 2024 22:54:02 +0000 Original-Received: (at 72849) by debbugs.gnu.org; 29 Aug 2024 22:53:10 +0000 Original-Received: from localhost ([127.0.0.1]:52238 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sjo0r-0007p3-Bz for submit@debbugs.gnu.org; Thu, 29 Aug 2024 18:53:09 -0400 Original-Received: from out-177.mta0.migadu.com ([91.218.175.177]:65086) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sjo0n-0007oU-Td for 72849@debbugs.gnu.org; Thu, 29 Aug 2024 18:53:07 -0400 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lipklim.org; s=key1; t=1724971893; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Ip7cSZheQO2n06m7S65Y9sPrF+YrBLc8VquQGTWEwd0=; b=WpQtV5QBnCcSjlSmwVsckNpO26uIhH0QPMEOB+Jp19G+cg7JqvDIUcZg8u1NbIQEItYGu0 0k0IwkVYLN7j2YNAvLB77uMU9JXmeNVcvET0DUbPsB9cH0dq2MVgBh64IPhg/GrXHIK8cQ aX6hMTLwvGHMgLz1M6CzZ2+xg1l4RNQ= In-Reply-To: <86y14ghmm1.fsf@gnu.org> (Eli Zaretskii's message of "Wed, 28 Aug 2024 15:12:54 +0300") X-Migadu-Flow: FLOW_OUT X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:290948 Archived-At: --=-=-= Content-Type: text/plain Eli Zaretskii writes: >> Date: Wed, 28 Aug 2024 00:13:25 +0100 >> From: Evgenii Klimov via "Bug reports for GNU Emacs, >> the Swiss army knife of text editors" >> >> `with-temp-buffer' doesn't respect buffer-local environment variables, >> `exec-path' in this case. Which results in executables not being found, >> or the wrong versions of executables being picked up. E.g. if >> environment variable is modified via .dir-local file or direnv/envrc >> package. > > Hmm, this doesn't look clean to me: exec-path is just one variable, > what makes it special here? > > Moreover, it sounds like python-shell-with-environment, which > python-shell-prompt-detect calls, already attempts to have > buffer-local value of exec-path to be available to Python, so why > isn't that working for you? And if it isn't work, I think we should > amend python-shell-with-environment to do this, so we don't need to do > it "by hand". Indeed, my initial approach is too manual. Here the problem that I have: I don't use Python's "venv" module to create virtual environment for the project. Instead, I use GNU Guix's "guix shell" command [1] which provides augmented PATH and PYTHONPATH, etc. to link project's dependencies. Then, envrc.el package picks up these environment variables and makes them buffer-local project-wise (`exec-path' and `process-environment'). You are correct that `python-shell-with-environment' provides buffer-local variables, but `with-temp-buffer' treats `exec-path' and `process-environment' variables very specially. I didn't find this behavior in documentation, but look at this example: (setq-default exec-path (list "global" "list")) (setq-local exec-path (cons "local" (default-value 'exec-path))) (setq-default myvar (list "global" "list")) (setq-local myvar (cons "local" (default-value 'myvar))) (let ((exec-path exec-path) ; takes buffer-local (myvar myvar)) ; takes buffer-local (with-temp-buffer (insert (car exec-path) ; uses global "\n" (car myvar)) ; uses `let'-binded (buffer-string))) ;; => "global ;; local" (require 'cl-lib) (let ((myvar myvar)) ;; temporarily binds buffer-local value to global symbol (cl-letf (((default-value 'exec-path) exec-path)) (with-temp-buffer ;; global variable is used, but it's value is temporarily equal ;; to buffer-local value (insert (car exec-path) "\n" (car myvar)) (buffer-string)))) ;; => "local ;; local" It's a simplified and expanded version of `python-shell-with-environment' and `python-shell-prompt-detect'. As you can see, `exec-path' is treated differently inside of `with-temp-buffer' and `cl-letf' is needed to force `with-temp-buffer' to use buffer-local value of `exec-path'. In the new patch attached I show how this can be overcome. Don't know if you'll consider my use case too narrow and specific, but I'll be glad to hear your thoughts on this. [1] https://guix.gnu.org/en/manual/en/guix.html#Invoking-guix-environment --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Make-python-shell-with-environment-respect-buffer-lo.patch >From 6db90f6087d0d3270e1d7a7d77d8830eac1f0154 Mon Sep 17 00:00:00 2001 From: Evgenii Klimov Date: Thu, 29 Aug 2024 23:36:12 +0100 Subject: [PATCH] Make python-shell--with-environment respect buffer-local variables * lisp/progmodes/python.el (python-shell--with-environment): Make `with-temp-buffer' respect buffer-local values of `process-environment' and `exec-path', if set. --- lisp/progmodes/python.el | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 7193cc19425..3e543442dba 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -3030,11 +3030,11 @@ (defun python-shell--with-environment (extraenv bodyfun) (tramp-dissect-file-name default-directory 'noexpand))))) (if vec (python-shell--tramp-with-environment vec extraenv bodyfun) - (let ((process-environment - (append extraenv process-environment)) - (exec-path - ;; FIXME: This is still Python-specific. - (python-shell-calculate-exec-path))) + (cl-letf (((default-value 'process-environment) + (append extraenv process-environment)) + ((default-value 'exec-path) + ;; FIXME: This is still Python-specific. + (python-shell-calculate-exec-path))) (funcall bodyfun))))) (defun python-shell--tramp-with-environment (vec extraenv bodyfun) -- 2.45.2 --=-=-=--