From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.bugs Subject: bug#72849: [PATCH] Keep project's exec-path during with-temp-buffer call Date: Sat, 31 Aug 2024 13:17:58 +0300 Message-ID: <86r0a5atd5.fsf@gnu.org> References: <87v7zlo8yy.fsf@lipklim.org> <86y14ghmm1.fsf@gnu.org> <87ed677xji.fsf@lipklim.org> Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="7361"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 72849-done@debbugs.gnu.org, eugene.dev@lipklim.org To: kobarity Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Aug 31 12:19:20 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 1skLCS-0001lK-2v for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 31 Aug 2024 12:19:20 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1skLCF-0000Ls-EE; Sat, 31 Aug 2024 06:19:08 -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 1skLCC-0000LB-Ja for bug-gnu-emacs@gnu.org; Sat, 31 Aug 2024 06:19:04 -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 1skLCC-0002C1-8a for bug-gnu-emacs@gnu.org; Sat, 31 Aug 2024 06:19:04 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=References:In-Reply-To:From:Date:To:Subject; bh=MLQ+fj/B74vpoFdB5V5EpajHITtYxKbHVzuuMMx8Hz0=; b=dHgJsB+1JY3pmt1RmZvavhQ1i4HrzybKzzbioSmKs7wVgb+W5KhEzZDabEYWGbHWB3P33EMaKdD+18Fy64eA74K4e+TSjlUKW0yDSuoCkcKq9zmmsnd3c+PHCkEev9oGgC80g3j4gsd8SJvY/nRYO8mX+u48r50WkMF1nnBPSz8CYBB93KHHCL0TGsvTeTeLlxxD6OlFlywtbOgCimsZ5NTPwydCwdDJZxheGc5kfEgqSfLVwBmV1qpPk99GEznFVgbrb5a9FNxsABsWHFAzQbu4LY13w2hGfxNI3VGi+84uj5zZW/QuK7rDeNBFXERI8wci6y5ulNIVkeAbkk9ziQ==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1skLD8-0001oY-5b for bug-gnu-emacs@gnu.org; Sat, 31 Aug 2024 06:20:02 -0400 Resent-From: Eli Zaretskii Original-Sender: "Debbugs-submit" Resent-To: bug-gnu-emacs@gnu.org Resent-Date: Sat, 31 Aug 2024 10:20:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: cc-closed 72849 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Mail-Followup-To: 72849@debbugs.gnu.org, eliz@gnu.org, eugene.dev@lipklim.org Original-Received: via spool by 72849-done@debbugs.gnu.org id=D72849.17250995466883 (code D ref 72849); Sat, 31 Aug 2024 10:20:01 +0000 Original-Received: (at 72849-done) by debbugs.gnu.org; 31 Aug 2024 10:19:06 +0000 Original-Received: from localhost ([127.0.0.1]:54017 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1skLCD-0001mx-Oq for submit@debbugs.gnu.org; Sat, 31 Aug 2024 06:19:06 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:58474) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1skLCB-0001mQ-Sl for 72849-done@debbugs.gnu.org; Sat, 31 Aug 2024 06:19:04 -0400 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1skLBA-00028y-Ks; Sat, 31 Aug 2024 06:18:00 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date: mime-version; bh=MLQ+fj/B74vpoFdB5V5EpajHITtYxKbHVzuuMMx8Hz0=; b=WJLUzPkBWSg0 7An5HGNbsaVwiA3MEZB/YvmQ38OEgFO0kccKJKx/q0QpADvXSNVP1EJ2AwkR1PWECCHV8R8trhVFi I3We0OAlRhcpM5l2aVK4g2lC9lcpgj/iJZuQ/UcGnabgRdZhoYn81jj3YQ3CFRRT6gPRml3YPoOf+ H87Q6dFXI6KZuGDCjAXcQ+jmgu8JSaJcFqZ/6kLUjw2z4c13Yvs27xyp/AJHaG6KGUCUh4EKjiaj+ YaLoqoKFj/L4VXaeL/gFvcHr2QMUqJLHLhaK9FgWgKWYObM0wQb3NrEVu6QQyoyxSFg8KR9xhzzOn XtTyeU63IF3sB3gCs67QaQ==; In-Reply-To: (message from kobarity on Fri, 30 Aug 2024 23:43:57 +0900) 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:291003 Archived-At: > Date: Fri, 30 Aug 2024 23:43:57 +0900 > From: kobarity > Cc: 72849@debbugs.gnu.org > > Evgenii Klimov wrote: > > 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 > > Thank you for your explanation and the new patch. It helped me to > understand the problem correctly. I also confirmed that the new patch > resolves the issue. I don't think it's a special use case, as I > sometimes set `exec-path' locally using .dir-locals.el. I agree with > the new patch. Thanks to both of you. I've now installed this on the emacs-30 release branch, and I'm therefore closing this bug.