unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Philip Kaludercic <philipk@posteo.net>
To: "João Távora" <joaotavora@gmail.com>
Cc: Manuel Uberti <manuel.uberti@inventati.org>,
	58839@debbugs.gnu.org, Dmitry Gutov <dgutov@yandex.ru>
Subject: bug#58839: [Patch] Re: bug#58839: 29.0.50; project-kill-buffer fails when Eglot is running
Date: Fri, 28 Oct 2022 17:28:50 +0000	[thread overview]
Message-ID: <87edur3lil.fsf@posteo.net> (raw)
In-Reply-To: <CALDnm53uLpMHTCwBVrh4nWj3rzp+ycQcMxZ7HAob3SPiO2urTA@mail.gmail.com> ("João Távora"'s message of "Fri, 28 Oct 2022 18:17:18 +0100")

João Távora <joaotavora@gmail.com> writes:

> On Fri, Oct 28, 2022 at 1:58 PM Philip Kaludercic <philipk@posteo.net>
> wrote:
>
>> When wanting to clean up behind a project I like to use C-x p k to get
>> rid of everything I have opened related to it.  If I was using Eglot and
>> there is still an active LSP server running in the background, killing
>> the project fails with these messages:
>
> Thanks Philip.  This was discussed at
>
> https://github.com/joaotavora/eglot/discussions/822
>
> Some more information is needed:
>
> 1. The error only happens when eglot-autoshutdown has been set to t by
>    the user.
>
> 2. When it has not been set to t, then the behavior is still not
>    correct, but the user may not notice it.
>
> 3. According to Manuel Uberti, the problem also happens with CIDER, a
>    Clojure IDE for Emacs.  So it seems it is not exclusive to Eglot.
>
> The problem happens because `project-kill-buffers` uses project.el's
> sense of a project buffer, and then endeavours to kill all such buffers.
>
> So far so good, but the determination of project buffers according
> to `project-buffers` considers all buffers whose buffer-local default
> directory starts with a given root of some project.
>
> This is subtly wrong because it also considers buffers whose name starts
> with space and without buffer-file-names, so-called "hidden buffers" which
> are deemed "uninteresting" to the user (according to the Elisp manual).
> They commonly function as implementation details of other packages, such
> as Eglot (and possibly CIDER).  These buffers are not normally visible
> to the user in M-x ibuffer, switch-to-buffer, etc.
>
> In Eglot's case, the buffer whose name is " EGLOT process..." is
> created by eglot.el and then handed over to jsonrpc.el, which becomes
> responsible for it.
>
> Killing this buffer from Lisp using `kill-buffer` is incorrect because
> it contradicts Eglot's user preferences eglot-autoreconnect and
> eglot-autoshutdown:
>
> 1. If eglot-autoshutdown is t, killing the buffer from Lisp kills the
>    process and confuses the LSP shutdown logic, which is a polite
>    "teardown" conversation with the LSP server.  This is Philip's error.

> 2. If eglot-autoshutdown is nil but eglot-autoreconnect is non-nil (in
>    fact, these are the defaults), killing the buffer has the effect of
>    immediately restarting the connection, and thus re-creating the
>    buffer.  The best that can happen is that nothing was achieved
>    and only time was wasted.
>
> The fact is that the buffer in question is an internal Eglot implementation
> detail that other packages should stay clear of.
>
> In fact, I think that all hidden buffers can be considered thusly.
> They're just like `--` symbols in obarray or in other symbol's plists:
> they're visible to all Lisp packages but they are implementation details
> that shouldn't be messed with except by the owner of such details.
>
> Dmitry tells me that there was some discussion where it was determined
> that it's somehow useful in project-kill-buffers to also target buffers
> that the
> user isn't aware of.
>
> But I've not seen evidence of this usefulness.  If there is indeed some,
> I propose we come up with some convention so that it is possible for
> packages to create buffers which are "definitely hidden and private and
> not to me tinkered with".  Such a convention could be starting the
> buffer name with two spaces.
>
> Whatever the convention, currently I think that the patch after my
> signature is the correct approach to fix this bug.
>
> Thanks,
> João
>
> diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
> index ac278edd40..4f542137a8 100644
> --- a/lisp/progmodes/project.el
> +++ b/lisp/progmodes/project.el
> @@ -352,14 +352,18 @@ project--remote-file-names
>                  (concat remote-id file))
>                local-files))))
>
> +(defun project--buffer-uninteresting-p (buf)
> +  (and (string-prefix-p " " (buffer-name buf)) (null (buffer-file-name
> buf))))
> +
>  (cl-defgeneric project-buffers (project)
>    "Return the list of all live buffers that belong to PROJECT."
>    (let ((root (expand-file-name (file-name-as-directory (project-root
> project))))
>          bufs)
>      (dolist (buf (buffer-list))
> -      (when (string-prefix-p root (expand-file-name
> -                                   (buffer-local-value 'default-directory
> buf)))
> -        (push buf bufs)))
> +      (unless (project--buffer-uninteresting-p buf)
> +        (when (string-prefix-p root (expand-file-name
> +                                     (buffer-local-value
> 'default-directory buf)))
> +          (push buf bufs))))
>      (nreverse bufs)))
>
>  (defgroup project-vc nil
> @@ -680,11 +684,12 @@ project-buffers
>           dd
>           bufs)
>      (dolist (buf (buffer-list))
> -      (setq dd (expand-file-name (buffer-local-value 'default-directory
> buf)))
> -      (when (and (string-prefix-p root dd)
> -                 (not (cl-find-if (lambda (module) (string-prefix-p module
> dd))
> -                                  modules)))
> -        (push buf bufs)))
> +      (unless (project--buffer-uninteresting-p buf)
> +        (setq dd (expand-file-name (buffer-local-value 'default-directory
> buf)))
> +        (when (and (string-prefix-p root dd)
> +                   (not (cl-find-if (lambda (module) (string-prefix-p
> module dd))
> +                                    modules)))
> +          (push buf bufs))))
>      (nreverse bufs)))

I still don't agree that this is the right interpretation of the issue
or solution, but wouldn't it be better to add this to
`project-kill-buffer-conditions'?

The solution I would prefer is if project.el would define a sort of
project-kill-hook, that Eglot would modify and make sure that
`eglot-shutdown' is invoked before any buffer is killed.





  reply	other threads:[~2022-10-28 17:28 UTC|newest]

Thread overview: 86+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-28 12:56 bug#58839: 29.0.50; project-kill-buffer fails when Eglot is running Philip Kaludercic
2022-10-28 17:17 ` bug#58839: [Patch] " João Távora
2022-10-28 17:28   ` Philip Kaludercic [this message]
2022-10-28 17:36     ` João Távora
2022-10-28 18:14     ` Dmitry Gutov
2022-10-28 18:20       ` Philip Kaludercic
2022-10-28 18:30         ` João Távora
2022-10-28 18:40         ` Dmitry Gutov
2022-10-29  0:15           ` João Távora
2022-10-29  1:09             ` Dmitry Gutov
2022-10-29  1:39               ` João Távora
2022-10-29 11:27                 ` Dmitry Gutov
2022-10-29 12:16                   ` João Távora
2022-10-29 14:32                     ` Philip Kaludercic
2022-10-29 20:38                       ` João Távora
2022-10-29 22:01                         ` Philip Kaludercic
2022-10-29 22:49                           ` João Távora
2022-10-30  6:28                             ` Eli Zaretskii
2022-10-30 12:40                               ` João Távora
2022-10-30 15:58                               ` Dmitry Gutov
2022-10-30 16:39                                 ` Eli Zaretskii
2022-10-30 19:13                                   ` Dmitry Gutov
2022-10-30 19:54                                     ` Eli Zaretskii
2022-10-30 21:15                                       ` Dmitry Gutov
2022-10-31  9:53                                 ` João Távora
2022-10-31 11:56                                   ` João Távora
2022-10-31 17:11                                     ` Dmitry Gutov
2022-10-31 20:36                                       ` João Távora
2022-10-31 22:26                                         ` Dmitry Gutov
2022-10-31 22:51                                           ` João Távora
2022-10-31 14:35                                   ` Philip Kaludercic
2022-10-31 17:33                                     ` Dmitry Gutov
2022-10-31 23:19                                     ` João Távora
2022-11-01 10:51                                       ` Philip Kaludercic
2022-11-01 13:22                                       ` Dmitry Gutov
2022-11-01 13:39                                         ` João Távora
2022-10-31 17:24                                   ` Dmitry Gutov
2022-10-31 20:58                                     ` João Távora
2022-10-31 22:51                                       ` Dmitry Gutov
2022-11-01 10:48                                         ` Philip Kaludercic
2022-11-01 10:59                                           ` João Távora
2022-11-01 11:23                                             ` Dmitry Gutov
2022-11-01 11:39                                               ` João Távora
2022-11-01 15:27                                                 ` Dmitry Gutov
2022-11-01 16:23                                                   ` João Távora
2022-11-01 22:24                                                     ` Dmitry Gutov
2022-11-02  7:40                                                       ` João Távora
2022-11-01 11:27                                             ` Philip Kaludercic
2022-11-01 11:59                                               ` João Távora
2022-11-01 13:03                                                 ` Philip Kaludercic
2022-11-01 13:37                                                   ` João Távora
2022-11-01 14:00                                                     ` Philip Kaludercic
2022-11-01 14:11                                                       ` João Távora
2022-11-01 14:36                                                         ` Philip Kaludercic
2022-11-02  7:19                                                           ` João Távora
2022-11-02  7:29                                                             ` Philip Kaludercic
2022-11-02  7:48                                                               ` João Távora
2022-11-02  8:21                                                                 ` Philip Kaludercic
2022-11-02  8:41                                                                   ` João Távora
2022-11-02  9:06                                                                     ` Philip Kaludercic
2022-11-02  9:52                                                                       ` João Távora
2022-11-02 11:31                                                                         ` Philip Kaludercic
2022-11-01 15:26                                               ` Dmitry Gutov
2022-11-01 18:44                                                 ` Philip Kaludercic
2022-11-01 19:50                                                   ` Dmitry Gutov
2022-11-01 20:10                                                     ` Philip Kaludercic
2022-11-01 22:40                                                       ` Dmitry Gutov
2022-11-01 11:36                                         ` João Távora
2022-11-01 22:23                                           ` Dmitry Gutov
2022-11-02  7:34                                             ` João Távora
2022-11-02  8:36                                               ` Philip Kaludercic
2022-11-02  8:50                                                 ` João Távora
2022-11-02  9:13                                                   ` Philip Kaludercic
2022-11-02 14:00                                                     ` João Távora
2022-11-02 14:42                                                       ` Philip Kaludercic
2022-11-02 17:32                                                         ` Juri Linkov
2022-11-03 17:30                                                           ` Juri Linkov
2022-11-03 18:19                                                             ` João Távora
2022-11-02 18:16                                                         ` João Távora
2022-11-04  1:13                                           ` Dmitry Gutov
2022-11-04 11:21                                     ` Basil L. Contovounesios via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-11-05  0:53                                       ` Dmitry Gutov
2022-10-29  6:38               ` Philip Kaludercic
2022-10-29 10:59                 ` Dmitry Gutov
2022-10-29 11:12                   ` João Távora
2022-10-29 11:05                 ` João Távora

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87edur3lil.fsf@posteo.net \
    --to=philipk@posteo.net \
    --cc=58839@debbugs.gnu.org \
    --cc=dgutov@yandex.ru \
    --cc=joaotavora@gmail.com \
    --cc=manuel.uberti@inventati.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).