From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: kobarity Newsgroups: gmane.emacs.bugs Subject: bug#72849: [PATCH] Keep project's exec-path during with-temp-buffer call Date: Fri, 30 Aug 2024 23:43:57 +0900 Message-ID: References: <87v7zlo8yy.fsf@lipklim.org> <86y14ghmm1.fsf@gnu.org> <87ed677xji.fsf@lipklim.org> Mime-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="328"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?Q?Goj=C5=8D?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/30.0.50 (x86_64-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO) Cc: 72849@debbugs.gnu.org To: Evgenii Klimov , Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Aug 30 16:46:28 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 1sk2tP-000AVq-Vg for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 30 Aug 2024 16:46:28 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sk2t6-0000nr-Lj; Fri, 30 Aug 2024 10:46:13 -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 1sk2t3-0000ni-UX for bug-gnu-emacs@gnu.org; Fri, 30 Aug 2024 10:46:06 -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 1sk2t3-0003Xz-Gw for bug-gnu-emacs@gnu.org; Fri, 30 Aug 2024 10:46:05 -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:References:In-Reply-To:From:Date:To:Subject; bh=i03LE1baFoPXpr4nHRE+Th6Ed0gTijEzY3qIv8NJGjw=; b=sZ/4GFEW/0xyKgDZ7Aqb9EM5orEhkeP2FjWhcRuXCcbnVvQoLJgswED11ZnVqkTZpHIPpXGBCelWLwSadMxQsPFDwyEBJi41lU16MaN41JC1x/ox4+pOG1Mq1lpMo21XNqyPSCaT2IiV7rzNZG0os3ny+JkJkjYnnUXQ/MCQMqjdV4Dc2k0wUxcXgO2+zx51ucVzt9PIPD7I1NAV+DaoKuBhmSvuez9VOWNl4jQXdLtHgjUa/reyp/ToQyJQJjHqA+I9WQH8I+8YVcwuj+Dah2al+egnbWoam2+6GzreKC9aO113nuKjrIYi6hIjPuX9zoXdLH3bV6SJKdP5FbGVVQ==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1sk2tx-0000qq-Uz for bug-gnu-emacs@gnu.org; Fri, 30 Aug 2024 10:47:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: kobarity Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 30 Aug 2024 14:47:01 +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.17250291943228 (code B ref 72849); Fri, 30 Aug 2024 14:47:01 +0000 Original-Received: (at 72849) by debbugs.gnu.org; 30 Aug 2024 14:46:34 +0000 Original-Received: from localhost ([127.0.0.1]:53329 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sk2tV-0000pz-N5 for submit@debbugs.gnu.org; Fri, 30 Aug 2024 10:46:34 -0400 Original-Received: from mail-pl1-f180.google.com ([209.85.214.180]:56345) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sk2tS-0000pk-Cb for 72849@debbugs.gnu.org; Fri, 30 Aug 2024 10:46:32 -0400 Original-Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-20231aa8908so14294275ad.0 for <72849@debbugs.gnu.org>; Fri, 30 Aug 2024 07:45:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725029067; x=1725633867; darn=debbugs.gnu.org; h=mime-version:user-agent:references:in-reply-to:subject:cc:to:from :message-id:date:from:to:cc:subject:date:message-id:reply-to; bh=i03LE1baFoPXpr4nHRE+Th6Ed0gTijEzY3qIv8NJGjw=; b=UQHc/WXUnzs2pt0peBNqm8mKxUlSw8DI0Y/qp5rn6I2wQEUBMbUv2rmZb1ZXn/DMz2 HTcEpzoBxeXSXmCZlCq2PH8xl4fck/svWUocpwA13TnsfzFP04rZ0/WKOvLU0CM+mf/p LxLtoyfXYjNibA091snZ0MDEZx1HK99+KiO46wtNm+iqM2ldNGpMvyD/uytWGNHHncwX F8SpY/fCl9Xpg43rVo62TGQ5PT4Ibx+/gSCWCS67SAp7/60+K8hSbQXFyZj/PVMpfW1P Kd9Pn3fBmGxQpVcBnXrCopfoMQqdlV0Xde+B8iPp33VBBQWRFz8+lUD5WOzRAM3KHtvE OeYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725029067; x=1725633867; h=mime-version:user-agent:references:in-reply-to:subject:cc:to:from :message-id:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=i03LE1baFoPXpr4nHRE+Th6Ed0gTijEzY3qIv8NJGjw=; b=uo4uk3Qa+sLD79RqIHUgFUm4qGJtN2Ih6aJRAzvzfmk3BKIv1IjEStmB2f+3Pak1jT ELVJn7zvRY9hRX2KYISEqkxEUUCluptSX1vL7eHB3gASRP1oHP7C6sbxVKOR+bQAqh1D pI5By7ujI4Z/DY9JdPz2AlNiqrsIKh1/i/Ho4Mzn/K+dyxjoLlfDIEHIwgm7LcdFk8b9 kid876F152S5AKiMqBMTXs8Lo5vtcgVhLVGZhoPTblYSBS5kU7NMcKPVkH3CvU/d7q0d ObwBno5v04PzH/9JdOfBjavv0Cma/oixFOSNn6lwyUPG2igXunUbNtdLzvksVl5y+Lpp zPmg== X-Gm-Message-State: AOJu0Yy+ArOmJTNB6KyTkcNhN6p9rMee7qJyiGqu0EkA67sMX33VTr6v KALtrQTClsPss2TlZCUPgYAOS98PTIUpiUbIprU3yEywAsLxPRHO X-Google-Smtp-Source: AGHT+IHuBWIEeLlIewfpJZwLV0QR1YZW8I0hPQYfYoehp6yj4tGYRv8A6oH7t95ikMi8wrFyeX4Dsw== X-Received: by 2002:a17:902:f54f:b0:202:26d:146c with SMTP id d9443c01a7336-2050c34f040mr64792325ad.5.1725029067209; Fri, 30 Aug 2024 07:44:27 -0700 (PDT) Original-Received: from localhost (58x12x133x161.ap58.ftth.ucom.ne.jp. [58.12.133.161]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20515534334sm27687185ad.134.2024.08.30.07.44.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Aug 2024 07:44:26 -0700 (PDT) In-Reply-To: <87ed677xji.fsf@lipklim.org> 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:290966 Archived-At: 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.