From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Sean Devlin Newsgroups: gmane.emacs.bugs Subject: bug#58784: 28.2; project-buffers incorrect under let-bound default-directory Date: Mon, 31 Oct 2022 13:47:20 -0400 Message-ID: <938BB7EA-3902-4025-98B5-914D514B4A63@toadstyle.org> References: <0b56cc0a-b8d4-86dc-4b67-217387aeb1b2@yandex.ru> Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.300.51\)) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="17063"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 58784@debbugs.gnu.org To: Dmitry Gutov Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Oct 31 18:48:15 2022 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 1opYtT-0004C0-8d for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 31 Oct 2022 18:48:15 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1opYtK-0002if-4h; Mon, 31 Oct 2022 13:48:06 -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 1opYtH-0002hM-27 for bug-gnu-emacs@gnu.org; Mon, 31 Oct 2022 13:48:04 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1opYtG-0002SW-Qj for bug-gnu-emacs@gnu.org; Mon, 31 Oct 2022 13:48:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1opYtG-0004hB-8g for bug-gnu-emacs@gnu.org; Mon, 31 Oct 2022 13:48:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Sean Devlin Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 31 Oct 2022 17:48:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 58784 X-GNU-PR-Package: emacs Original-Received: via spool by 58784-submit@debbugs.gnu.org id=B58784.166723846017912 (code B ref 58784); Mon, 31 Oct 2022 17:48:02 +0000 Original-Received: (at 58784) by debbugs.gnu.org; 31 Oct 2022 17:47:40 +0000 Original-Received: from localhost ([127.0.0.1]:41706 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1opYsu-0004ep-9Z for submit@debbugs.gnu.org; Mon, 31 Oct 2022 13:47:40 -0400 Original-Received: from mail-qk1-f169.google.com ([209.85.222.169]:39533) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1opYsr-0004eY-V8 for 58784@debbugs.gnu.org; Mon, 31 Oct 2022 13:47:38 -0400 Original-Received: by mail-qk1-f169.google.com with SMTP id g10so1518373qkl.6 for <58784@debbugs.gnu.org>; Mon, 31 Oct 2022 10:47:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=toadstyle-org.20210112.gappssmtp.com; s=20210112; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=u78KM0W0+iPsv8KnoW4GcIzZB5i7nqwezj41eCQ8qag=; b=2RSBYcFjCH/Qi5Ta2jEjrOmTAKBVyJrMJ4YirlV8aSaC1giX/iGakrAaUS3uFnkSi6 AK1PbZ2/PHIgA7biaFrNerbKIbRZpj4MVGrAO0+FUyyvyCGW3CK04j/bVHlPz3Ht77oc uZjiYf7VaOZ3IEVJyPphgHk1xj8Na8XQ60gxiZ5BMzNdvEIGeIWkK1JxrpGYaYf8V+Ya BlxTvhnuFXm+PhGJgMwAedLGhtwrrcR+0Pe52OEDqyXmVYZA6t0I4HkgY3O86BEUCw7r bAOTMu21rJzAApqBn4is8Z+2sUG2Qis7poY209z0m+qqF92xaVKYFEfChqqKpeK9qazB to1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=u78KM0W0+iPsv8KnoW4GcIzZB5i7nqwezj41eCQ8qag=; b=UDzc8Z4CsyuNTy76yy4nL+fUCMcLNJcfgl/i15TAR3Nt1yI3nyQt6GFiVTgwxBRzm0 jDvyCieeYREZVBF35l271M/E+5G706cREAMtxX7Fcz6IL9M3/VH2HbOoUbXvXXI4n982 4EmMbUUG1qcwHFpp7tDNJ6UUZTjN537+WI6C1CKsG+6qsiFQFzVu2sdWgP2xxhVFbpEk GAFhYAKi2MtVt1gFpJTxo3GyqYdH2jOxzE1O7b/DJs2ROjr31md7BmGomxosnT5esZHQ MHBcFU5xmfPxLyLT1g2e/1mDKLwBW9qg8pKF8EfOgxucCHePsN24umFTnqF458tdDHDu Qedg== X-Gm-Message-State: ACrzQf1CizOSBwePWj1Tx0YYUd1sMsA7J4aDi1ps8X7r+E6LOE0/1dLV kfld71HFn50djjDVWZ50TFWZsHegDLsAmkZG X-Google-Smtp-Source: AMsMyM4S4c/GvGiG83YpVqe7JNExyeAtJSWk659l6X2sXZo1YtymNR5ZgGddeWrcRHb/8n+o3Ks1vQ== X-Received: by 2002:a05:620a:1667:b0:6fa:1b5e:b9b4 with SMTP id d7-20020a05620a166700b006fa1b5eb9b4mr7957466qko.141.1667238452344; Mon, 31 Oct 2022 10:47:32 -0700 (PDT) Original-Received: from smtpclient.apple (pool-173-56-106-162.nycmny.ftas.verizon.net. [173.56.106.162]) by smtp.gmail.com with ESMTPSA id l10-20020a37f90a000000b006b9c9b7db8bsm5076262qkj.82.2022.10.31.10.47.31 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 31 Oct 2022 10:47:31 -0700 (PDT) In-Reply-To: <0b56cc0a-b8d4-86dc-4b67-217387aeb1b2@yandex.ru> X-Mailer: Apple Mail (2.3731.300.51) 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: , Original-Sender: "bug-gnu-emacs" Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:246708 Archived-At: Hi Dmitry, > On Oct 28, 2022, at 8:49 PM, Dmitry Gutov wrote: >=20 > Hi! >=20 > On 26.10.2022 03:13, Sean Devlin wrote: >> The project.el function project-buffers can return incorrect results >> when invoked while a let-binding for default-directory is in >> effect. This is because project-buffers (both the default and = vc-based >> implementations) does its work by inspecting the local value of >> default-directory in each buffer, and the let-binding temporarily >> affects this value. >> To see this in action, start emacs with -Q and evaluate the following >> forms in order: >> (require 'project) >> (find-file-noselect "/tmp/tmpfile") >> (setq my-project '(transient . "/tmp/")) >> ;; just the tmpfile >> (project-buffers my-project) >> ;; both tmpfile and scratch >> (let ((default-directory "/tmp/")) >> (project-buffers my-project)) >=20 > Thanks for the report, I can see the problem. >=20 >> In the last form, project-buffers includes the current buffer (i.e. = the >> scratch buffer in our example) with the results. (This is true even = if >> the current buffer is visiting a file in some unrelated directory.) >> This matters because the command project-switch-project let-binds >> default-directory before calling project-switch-commands. This means >> that if you set project-switch-commands to some function that calls >> project-buffers, you will get incorrect results. >> For example, evaluate the following forms in order: >> (defun my-list-project-buffers () >> "List the current project's buffers." >> (interactive) >> (let ((buffer-list (project-buffers (project-current t))) >> (buffer-name (project-prefixed-buffer-name = "my-project-buffer-list"))) >> (with-current-buffer (get-buffer-create buffer-name) >> (erase-buffer) >> (save-excursion >> (dolist (buffer buffer-list) >> (insert (buffer-name buffer)) >> (insert ?\n)))) >> (switch-to-buffer buffer-name))) >> (setq project-switch-commands #'my-list-project-buffers) >=20 > Looks like a reimplementation of projectile-ibuffer, seems useful. Yeah, in my real configuration, I defined this as an ibuffer filter, but = I thought a simpler example would be better for the bug report. >=20 >> ;; list tmpfile but also scratch >> (project-switch-project "/tmp/") >=20 > Not sure how to fix this, though. In bug#53626 we discussed a somewhat = similar problem, and a let-binding seems impossible to "escape". >=20 > What else can we do? One option is to change the signature of every = compatible command to take the project object as its first argument. = Might have been more realistic when the package was first written, too = much breakage now, probably. >=20 > Another would be to add a new var to help override the project choice = without touch default-directory. >=20 > Something like the attached. Please try it = out. I agree, the fix is not obvious. A workaround I used in my configuration is to add an advice to = project-switch-project to wrap it in a with-temp-buffer form. This way, = it=E2=80=99s only the default-directory of the temp buffer that gets = corrupted. Not a very clean solution, but it seems to work. = Unfortunately, you need to remember to do this in many places; for = example, I had to do the same thing in my project-ibuffer command. Your solution looks cleaner. I gave it a try (along with disabling my = advice), and it seems to work pretty well. Thanks for the fix! There might be some more places where it needs to be applied. For = example, project-prefixed-buffer-name still inspects the = default-directory. (Maybe project-prefixed-buffer-name should just call = project-root or similar?) I think there=E2=80=99s still some fragility in the project-buffers = function, since any callers need to be careful not to bind = default-directory. It might be useful to call this out in the doc string = or in the manual. Thanks for your help!=