Subject: Re: 回复: bug#61289: 30.0.50; Cannot reinstall language grammar after running `treesit-ready-p'

From: Eli Zaretskii <[email-masked]>
Date: Sun, 05 Feb 2023 12:24:46 +0200

> From: 牟 桐 <[email-masked]>
> CC: "[email-masked]" <[email-masked]>
> Date: Sun, 5 Feb 2023 09:58:49 +0000
> 
> Yes, after applying your patch, I believe this problem has been solved.

Thanks.  I installed the change on the emacs-29 branch, and I'm
closing this bug.

> But I noticed the `libtree-sitter-xxx.dll.old` in the tree-sitter dir
> hadn't been delelted.

It cannot be deleted as long as the Emacs session which uses it is up
and running.  Windows doesn't let us delete a DLL that is being used,
that's why the patch renames it instead.

The next time you install a new version of the same DLL, the .old file
will be deleted, if it still exists.  Or you can delete it by hand.

> If I remove the `ignore-errors` function in your patch, which seems like
> this (sorry, I'm not a programmer and don't know how to use diff):
> 
> -          (copy-file lib-name (file-name-as-directory out-dir) t t)
> +          (let* ((library-fname (expand-file-name lib-name out-dir))
> +                 (old-fname (concat library-fname ".old")))
> +            ;; Rename the existing shared library, if any, then
> +            ;; install the new one, and try deleting the old one.
> +            ;; This is for Windows systems, where we cannot simply
> +            ;; overwrite a DLL that is being used.
> +            (if (file-exists-p library-fname)
> +                (rename-file library-fname old-fname t))
> +            (copy-file lib-name (file-name-as-directory out-dir) t t)
> +            ;; Ignore errors, in case the old version is still used.
> +            (delete-file old-fname))
> 
>              ^
>              I deleted the outside `ignore-errors` to know why the `xxx.old` hadn’t been deleted.
> 
> After applying the change, I got the same error as I mentioned before:
> 
> ```
> ⛔ Warning (treesit): Error encountered when installing language grammar: (permission-denied Removing old name Permission denied c:/Users/redacted/.emacs.d/tree-sitter/libtree-sitter-c.dll.old)
> ```

Of course: that's why the patch uses ignore-errors in the first
place.  The DLL cannot be deleted as long as it is used.

> Btw, can we make the variable ` treesit-language-source-alist`
> customizable? (Use `defcustom` instead of `defvar`.) I'm using
> `use-package` to manage my config. When I'm configuring treesit, I'd
> like to use `:custom` instead of `:init` or `:config` since it can let
> me set variable's value without writing `setq`.

This was considered, but we decided not to do that, for several good
reasons.  One reason is that Emacs itself changes the value, which is
not appropriate with defcustoms.