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

From: 牟 桐 <[email-masked]>
Date: Sun, 5 Feb 2023 09:58:49 +0000

Yes, after applying your patch, I believe this problem has been solved.

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

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)
```

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`.

发件人: Eli Zaretskii<mailto:[email-masked]>
发送时间: 2023年2月5日 17:13
收件人: 牟 桐<mailto:[email-masked]>
抄送: [email-masked]<mailto:[email-masked]>
主题: Re: bug#61289: 30.0.50; Cannot reinstall language grammar after running `treesit-ready-p'

> From: 牟 桐 <[email-masked]>
> Date: Sat, 4 Feb 2023 12:59:53 +0000
>
> I'm using Windows, so I'll use `libtree-sitter-c.dll` as an example:
>
> 1. `emacs -Q` to start a vanilla Emacs.
>
> Run `M-x treesit-install-language-grammar`, then install the c
> parser to `~/.emacs.d/tree-sitter`. If there is already a
> `libtree-sitter-c.dll` there, the new installed library will
> overwrite the old library.
>
> 2. Execute `(treesit-ready-p 'c)`, which returns t.
>
> 3. Run `M-x treesit-install-language-grammar` again, there will be a
> warning:
>
> ```
> ⛔ Warning (treesit): Error encountered when installing language grammar: (file-error Copying file Operation not permitted c:/Users/redacted/AppData/Local/Temp/treesit-workdir5HPGoS/repo/src/libtree-sitter-c.dll c:/Users/redacted/.emacs.d/tree-sitter/libtree-sitter-c.dll)
> ```
>
> I don't know whether this is a bug or not, since I think it's reasonable
> to make the already used library not be able to be overwritten.
>
> But someone reminded me that `treesit-ready-p` will be executed when you
> load the `xxx-ts-mode`. If the execution of `treesit-ready-p` made the
> `treesit-install-language-grammar` unusable, it means the design of that
> function has a little problem.

This is a basic issue on MS-Windows: it won't allow you to overwrite a
DLL that is being used by some program.

Can you try the patch below and see if it solves the problem?

diff --git a/lisp/treesit.el b/lisp/treesit.el
index 948016d..7e31da9 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -2884,7 +2884,17 @@ treesit--install-language-grammar-1
           ;; Copy out.
           (unless (file-exists-p out-dir)
             (make-directory out-dir t))
-          (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.
+            (ignore-errors (delete-file old-fname)))
           (message "Library installed to %s/%s" out-dir lib-name))
       (when (file-exists-p workdir)
         (delete-directory workdir t)))))