GNU bug report logs - #79363
31.0.50; Font locking issue in go-ts-mode when tree-sitter grammar is automatically installed

Previous Next

Package: emacs;

Reported by: Roi Martin <jroi.martin <at> gmail.com>

Date: Mon, 1 Sep 2025 21:33:02 UTC

Severity: normal

Found in version 31.0.50

To reply to this bug, email your comments to 79363 AT debbugs.gnu.org.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#79363; Package emacs. (Mon, 01 Sep 2025 21:33:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Roi Martin <jroi.martin <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Mon, 01 Sep 2025 21:33:03 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Roi Martin <jroi.martin <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: 31.0.50; Font locking issue in go-ts-mode when tree-sitter grammar
 is automatically installed
Date: Mon, 01 Sep 2025 23:31:52 +0200
Steps to reproduce:

1. Run
     emacs -Q --init-directory=/tmp/go-ts-mode-bug
2. Eval
     (setopt treesit-enabled-modes '(go-ts-mode))
3. Open .go file
4. Answer y to the question
     Tree-sitter grammar for `go' is missing; install it?  (y or n)
5. go-ts-mode emits the following warning and definitions are not
   highlighted correctly.

     ⛔ Warning (treesit-font-lock-rules-mismatch): Emacs cannot compile
     every font-lock rules because a mismatch between the grammar and the
     rules.  This is most likely due to a mismatch between the font-lock
     rules defined by the major mode and the tree-sitter grammar.
     
     This error can be fixed by either downgrading the grammar
     (tree-sitter-go) on your system, or upgrading the major mode package.
     The following are the temporarily disabled features:
     
     - `definition' for go.

In GNU Emacs 31.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
 3.24.49, cairo version 1.18.2)
Repository revision: 3c94ae5a37eac0191fe5d8bd85164b190ac10244
Repository branch: master

I took a quick look and I think this is caused by the following issue:

1. `go-ts-mode--font-lock-settings' depends on the following functions:

     (defun go-ts-mode--iota-query-supported-p ()
       "Return t if the iota query is supported by the tree-sitter-go grammar."
       (ignore-errors
         (or (treesit-query-string "" '((iota) @font-lock-constant-face) 'go) t)))
     
     (defun go-ts-mode--method-elem-supported-p ()
       "Return t if Go grammar uses `method_elem' instead of `method_spec'."
       (ignore-errors
         (or (treesit-query-string "" '((method_elem) @cap) 'go) t)))

2. In this sceneario we don't have the Go grammar installed yet.  So,
   `treesit-query-string' returns a `not-found' error.
3. Because of the previous error, `go-ts-mode--method-elem-supported-p'
   returns `nil'.  Thus, the "definition" rule uses "method_spec" which
   is not supported by the default grammar (commit
   "12fe553fdaaa7449f764bc876fd777704d4fb752").

     (defvar go-ts-mode--font-lock-settings
       (treesit-font-lock-rules
        ...
        :language 'go
        :feature 'definition
        `(...
          (,(if (go-ts-mode--method-elem-supported-p)
                'method_elem
              'method_spec)
           name: (field_identifier) @font-lock-function-name-face)

Please, take this analysis with a pinch of salt.  I'm not familiar with
the code base.  But I hope it makes sense.

        Roi




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#79363; Package emacs. (Tue, 02 Sep 2025 06:35:01 GMT) Full text and rfc822 format available.

Message #8 received at 79363 <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: Roi Martin <jroi.martin <at> gmail.com>
Cc: 79363 <at> debbugs.gnu.org
Subject: Re: bug#79363: 31.0.50; Font locking issue in go-ts-mode when
 tree-sitter grammar is automatically installed
Date: Tue, 02 Sep 2025 09:31:52 +0300
> 1. `go-ts-mode--font-lock-settings' depends on the following functions:
>
>      (defun go-ts-mode--iota-query-supported-p ()
>        "Return t if the iota query is supported by the tree-sitter-go grammar."
>        (ignore-errors
>          (or (treesit-query-string "" '((iota) @font-lock-constant-face) 'go) t)))
>      
>      (defun go-ts-mode--method-elem-supported-p ()
>        "Return t if Go grammar uses `method_elem' instead of `method_spec'."
>        (ignore-errors
>          (or (treesit-query-string "" '((method_elem) @cap) 'go) t)))
>
> 2. In this sceneario we don't have the Go grammar installed yet.  So,
>    `treesit-query-string' returns a `not-found' error.
> 3. Because of the previous error, `go-ts-mode--method-elem-supported-p'
>    returns `nil'.  Thus, the "definition" rule uses "method_spec" which
>    is not supported by the default grammar (commit
>    "12fe553fdaaa7449f764bc876fd777704d4fb752").
>
>      (defvar go-ts-mode--font-lock-settings
>        (treesit-font-lock-rules
>         ...
>         :language 'go
>         :feature 'definition
>         `(...
>           (,(if (go-ts-mode--method-elem-supported-p)
>                 'method_elem
>               'method_spec)
>            name: (field_identifier) @font-lock-function-name-face)
>
> Please, take this analysis with a pinch of salt.  I'm not familiar with
> the code base.  But I hope it makes sense.

Thanks for the analysis, it's absolutely correct.  Manually re-evaluating
`go-ts-mode--font-lock-settings' after installing the grammar fixes this.

Thus we have to find a way to re-evaluate it automatically.
The same problem exists in some other ts-modes,
so the solution should be quite general.

One idea is to turn defvar into defun that memoizes its return value.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#79363; Package emacs. (Tue, 02 Sep 2025 10:19:02 GMT) Full text and rfc822 format available.

Message #11 received at 79363 <at> debbugs.gnu.org (full text, mbox):

From: Roi Martin <jroi.martin <at> gmail.com>
To: Juri Linkov <juri <at> linkov.net>
Cc: 79363 <at> debbugs.gnu.org
Subject: Re: bug#79363: 31.0.50; Font locking issue in go-ts-mode when
 tree-sitter grammar is automatically installed
Date: Tue, 02 Sep 2025 12:17:58 +0200
Juri Linkov <juri <at> linkov.net> writes:

> One idea is to turn defvar into defun that memoizes its return value.

Do you mean something like this?

  (defvar go-ts-mode--font-lock-settings nil
    "Tree-sitter font-lock settings for `go-ts-mode'.")
  
  (defun go-ts-mode--font-lock-settings ()
    "Return tree-sitter font-lock settings for `go-ts-mode'.
  
  Tree-sitter font-lock rules are evaluated the first time this function
  is called.  Subsequent calls return the first evaluated value."
    (or go-ts-mode--font-lock-settings
        (setq go-ts-mode--font-lock-settings
              (treesit-font-lock-rules
               ...))))
  
  (define-derived-mode go-ts-mode prog-mode "Go"
    ...
    (when (treesit-ensure-installed 'go)
      ...
      (setq-local treesit-font-lock-settings (go-ts-mode--font-lock-settings))
      ...))

I tried and I can confirm it works fine.  I can work on a patch for all
the affected ts modes if you want.

BTW is this a common pattern?  If it is, does the Elisp core library
provide something similar?  At the end of the day it is a sort of "lazy
defvar", isn't it?

        Roi




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#79363; Package emacs. (Tue, 02 Sep 2025 11:25:01 GMT) Full text and rfc822 format available.

Message #14 received at 79363 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Roi Martin <jroi.martin <at> gmail.com>, Randy Taylor <dev <at> rjt.dev>
Cc: Yuan Fu <casouri <at> gmail.com>, 79363 <at> debbugs.gnu.org
Subject: Re: bug#79363: 31.0.50;
 Font locking issue in go-ts-mode when tree-sitter grammar is
 automatically installed
Date: Tue, 02 Sep 2025 14:24:30 +0300
> From: Roi Martin <jroi.martin <at> gmail.com>
> Date: Mon, 01 Sep 2025 23:31:52 +0200
> 
> Steps to reproduce:
> 
> 1. Run
>      emacs -Q --init-directory=/tmp/go-ts-mode-bug
> 2. Eval
>      (setopt treesit-enabled-modes '(go-ts-mode))
> 3. Open .go file
> 4. Answer y to the question
>      Tree-sitter grammar for `go' is missing; install it?  (y or n)
> 5. go-ts-mode emits the following warning and definitions are not
>    highlighted correctly.
> 
>      ⛔ Warning (treesit-font-lock-rules-mismatch): Emacs cannot compile
>      every font-lock rules because a mismatch between the grammar and the
>      rules.  This is most likely due to a mismatch between the font-lock
>      rules defined by the major mode and the tree-sitter grammar.
>      
>      This error can be fixed by either downgrading the grammar
>      (tree-sitter-go) on your system, or upgrading the major mode package.
>      The following are the temporarily disabled features:
>      
>      - `definition' for go.
> 
> In GNU Emacs 31.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
>  3.24.49, cairo version 1.18.2)
> Repository revision: 3c94ae5a37eac0191fe5d8bd85164b190ac10244
> Repository branch: master
> 
> I took a quick look and I think this is caused by the following issue:
> 
> 1. `go-ts-mode--font-lock-settings' depends on the following functions:
> 
>      (defun go-ts-mode--iota-query-supported-p ()
>        "Return t if the iota query is supported by the tree-sitter-go grammar."
>        (ignore-errors
>          (or (treesit-query-string "" '((iota) @font-lock-constant-face) 'go) t)))
>      
>      (defun go-ts-mode--method-elem-supported-p ()
>        "Return t if Go grammar uses `method_elem' instead of `method_spec'."
>        (ignore-errors
>          (or (treesit-query-string "" '((method_elem) @cap) 'go) t)))
> 
> 2. In this sceneario we don't have the Go grammar installed yet.  So,
>    `treesit-query-string' returns a `not-found' error.
> 3. Because of the previous error, `go-ts-mode--method-elem-supported-p'
>    returns `nil'.  Thus, the "definition" rule uses "method_spec" which
>    is not supported by the default grammar (commit
>    "12fe553fdaaa7449f764bc876fd777704d4fb752").
> 
>      (defvar go-ts-mode--font-lock-settings
>        (treesit-font-lock-rules
>         ...
>         :language 'go
>         :feature 'definition
>         `(...
>           (,(if (go-ts-mode--method-elem-supported-p)
>                 'method_elem
>               'method_spec)
>            name: (field_identifier) @font-lock-function-name-face)
> 
> Please, take this analysis with a pinch of salt.  I'm not familiar with
> the code base.  But I hope it makes sense.

Thanks.

Randy and Yuan, could you please take a look?




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#79363; Package emacs. (Tue, 02 Sep 2025 17:00:02 GMT) Full text and rfc822 format available.

Message #17 received at 79363 <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: Roi Martin <jroi.martin <at> gmail.com>
Cc: 79363 <at> debbugs.gnu.org
Subject: Re: bug#79363: 31.0.50; Font locking issue in go-ts-mode when
 tree-sitter grammar is automatically installed
Date: Tue, 02 Sep 2025 19:56:51 +0300
>> One idea is to turn defvar into defun that memoizes its return value.
>
> Do you mean something like this?

Exactly.

>   (defvar go-ts-mode--font-lock-settings nil
>     "Tree-sitter font-lock settings for `go-ts-mode'.")
>   
>   (defun go-ts-mode--font-lock-settings ()
>     "Return tree-sitter font-lock settings for `go-ts-mode'.
>   
>   Tree-sitter font-lock rules are evaluated the first time this function
>   is called.  Subsequent calls return the first evaluated value."
>     (or go-ts-mode--font-lock-settings
>         (setq go-ts-mode--font-lock-settings
>               (treesit-font-lock-rules
>                ...))))
>   
>   (define-derived-mode go-ts-mode prog-mode "Go"
>     ...
>     (when (treesit-ensure-installed 'go)
>       ...
>       (setq-local treesit-font-lock-settings (go-ts-mode--font-lock-settings))
>       ...))
>
> I tried and I can confirm it works fine.  I can work on a patch for all
> the affected ts modes if you want.

Thanks, this would be nice.

> BTW is this a common pattern?  If it is, does the Elisp core library
> provide something similar?  At the end of the day it is a sort of
> "lazy defvar", isn't it?

At the moment I don't see a better solution.  For example,
like the existing `with-eval-after-load' we could add
`with-eval-after-grammar-installed' with some hook called
when the grammar gets installed, e.g.:

  (defun go-ts-mode--font-lock-settings ()
    (with-eval-after-grammar-installed 'go
      (setq go-ts-mode--font-lock-settings
            (treesit-font-lock-rules
             ...))))

But even with this additional complexity its usage
is not simpler than you suggested above.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#79363; Package emacs. (Wed, 03 Sep 2025 08:53:02 GMT) Full text and rfc822 format available.

Message #20 received at 79363 <at> debbugs.gnu.org (full text, mbox):

From: Roi Martin <jroi.martin <at> gmail.com>
To: Juri Linkov <juri <at> linkov.net>
Cc: 79363 <at> debbugs.gnu.org
Subject: Re: bug#79363: 31.0.50; Font locking issue in go-ts-mode when
 tree-sitter grammar is automatically installed
Date: Wed, 03 Sep 2025 10:52:25 +0200
Juri Linkov <juri <at> linkov.net> writes:

>> I can work on a patch for all the affected ts modes if you want.
>
> Thanks, this would be nice.

Great.  I'll try to send a patch during this week.

        Roi




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#79363; Package emacs. (Thu, 04 Sep 2025 10:05:01 GMT) Full text and rfc822 format available.

Message #23 received at 79363 <at> debbugs.gnu.org (full text, mbox):

From: Roi Martin <jroi.martin <at> gmail.com>
To: Juri Linkov <juri <at> linkov.net>
Cc: 79363 <at> debbugs.gnu.org
Subject: Re: bug#79363: 31.0.50; Font locking issue in go-ts-mode when
 tree-sitter grammar is automatically installed
Date: Thu, 04 Sep 2025 12:04:34 +0200
Roi Martin <jroi.martin <at> gmail.com> writes:

> Juri Linkov <juri <at> linkov.net> writes:
>
>>> I can work on a patch for all the affected ts modes if you want.
>>
>> Thanks, this would be nice.
>
> Great.  I'll try to send a patch during this week.

Hi Juri,

I'm going through all the tree-sitter modes checking that automatic
grammar installation works as expected, reviewing the relevant code and
fixing the issues I find.

The thing is that merging everything in one single patch and reviewing
everything together seems a bit too much.  For instance, I found issues
in elixir-ts-mode and heex-ts-mode that we would probably want to
discuss separately.  Are you OK with splitting the patch per ts mode?
That would also allow us to fix things incrementally instead of having
to wait until every single mode is fixed.

What do you think?  Can we use this bug to keep track of all this work?
I would really prefer to split it.  So I can also "split" the work
better.  BTW I already have some patches I can send.

Thanks for your help!

        Roi




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#79363; Package emacs. (Thu, 04 Sep 2025 10:36:01 GMT) Full text and rfc822 format available.

Message #26 received at 79363 <at> debbugs.gnu.org (full text, mbox):

From: Roi Martin <jroi.martin <at> gmail.com>
To: Juri Linkov <juri <at> linkov.net>
Cc: 79363 <at> debbugs.gnu.org
Subject: Re: bug#79363: 31.0.50; Font locking issue in go-ts-mode when
 tree-sitter grammar is automatically installed
Date: Thu, 04 Sep 2025 12:35:23 +0200
Roi Martin <jroi.martin <at> gmail.com> writes:

> I would really prefer to split it.

I meant splitting the patch.  Having one single issue in the bug tracker
for me is OK.  Sorry for the noise.

        Roi




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#79363; Package emacs. (Fri, 05 Sep 2025 06:25:03 GMT) Full text and rfc822 format available.

Message #29 received at 79363 <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: Roi Martin <jroi.martin <at> gmail.com>
Cc: 79363 <at> debbugs.gnu.org
Subject: Re: bug#79363: 31.0.50; Font locking issue in go-ts-mode when
 tree-sitter grammar is automatically installed
Date: Fri, 05 Sep 2025 09:10:50 +0300
> I'm going through all the tree-sitter modes checking that automatic
> grammar installation works as expected, reviewing the relevant code and
> fixing the issues I find.
>
> The thing is that merging everything in one single patch and reviewing
> everything together seems a bit too much.  For instance, I found issues
> in elixir-ts-mode and heex-ts-mode that we would probably want to
> discuss separately.  Are you OK with splitting the patch per ts mode?

Splitting the patch per ts mode would be nicer to review.
And you can send all patches in the same current issue.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#79363; Package emacs. (Fri, 05 Sep 2025 08:57:02 GMT) Full text and rfc822 format available.

Message #32 received at 79363 <at> debbugs.gnu.org (full text, mbox):

From: Roi Martin <jroi.martin <at> gmail.com>
To: Juri Linkov <juri <at> linkov.net>
Cc: 79363 <at> debbugs.gnu.org
Subject: Re: bug#79363: 31.0.50; Font locking issue in go-ts-mode when
 tree-sitter grammar is automatically installed
Date: Fri, 05 Sep 2025 10:56:09 +0200
[Message part 1 (text/plain, inline)]
Fix font lock and indentation in cmake-ts-mode when the tree-sitter
grammar is automatically installed.

[0001-Fix-font-lock-and-indentation-in-cmake-ts-mode.patch (text/x-patch, attachment)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#79363; Package emacs. (Fri, 05 Sep 2025 08:58:01 GMT) Full text and rfc822 format available.

Message #35 received at 79363 <at> debbugs.gnu.org (full text, mbox):

From: Roi Martin <jroi.martin <at> gmail.com>
To: Juri Linkov <juri <at> linkov.net>
Cc: 79363 <at> debbugs.gnu.org
Subject: Re: bug#79363: 31.0.50; Font locking issue in go-ts-mode when
 tree-sitter grammar is automatically installed
Date: Fri, 05 Sep 2025 10:57:15 +0200
[Message part 1 (text/plain, inline)]
Fix font lock in go-ts-mode when the tree-sitter grammar is
automatically installed.

[0001-Fix-font-lock-in-go-ts-mode.patch (text/x-patch, attachment)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#79363; Package emacs. (Fri, 05 Sep 2025 08:59:02 GMT) Full text and rfc822 format available.

Message #38 received at 79363 <at> debbugs.gnu.org (full text, mbox):

From: Roi Martin <jroi.martin <at> gmail.com>
To: Juri Linkov <juri <at> linkov.net>
Cc: 79363 <at> debbugs.gnu.org
Subject: Re: bug#79363: 31.0.50; Font locking issue in go-ts-mode when
 tree-sitter grammar is automatically installed
Date: Fri, 05 Sep 2025 10:58:41 +0200
[Message part 1 (text/plain, inline)]
Fix tree-sitter grammar installation issues in elixir-ts-mode and
heex-ts-mode.

[0001-Fix-grammar-installation-in-elixir-ts-mode-and-heex-.patch (text/x-patch, attachment)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#79363; Package emacs. (Sun, 07 Sep 2025 17:48:02 GMT) Full text and rfc822 format available.

Message #41 received at 79363 <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: Roi Martin <jroi.martin <at> gmail.com>
Cc: 79363 <at> debbugs.gnu.org
Subject: Re: bug#79363: 31.0.50; Font locking issue in go-ts-mode when
 tree-sitter grammar is automatically installed
Date: Sun, 07 Sep 2025 20:47:27 +0300
Thanks, now all 3 patches are pushed.

Are there other ts-modes that need the same fix?

I see that treesit-admin--generate-compatibility-report reports a warning for javascript:

  Warning (treesit-font-lock-rules-mismatch): Emacs cannot compile every
  font-lock rules because a mismatch between the grammar and the rules.
  This is most likely due to a mismatch between the font-lock rules
  defined by the major mode and the tree-sitter grammar.
  This error can be fixed by either downgrading the grammar
  (tree-sitter-javascript) on your system, or upgrading the major mode
  package.  The following are the temporarily disabled features:
  - `definition' for javascript.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#79363; Package emacs. (Sun, 07 Sep 2025 18:40:01 GMT) Full text and rfc822 format available.

Message #44 received at 79363 <at> debbugs.gnu.org (full text, mbox):

From: Roi Martin <jroi.martin <at> gmail.com>
To: Juri Linkov <juri <at> linkov.net>
Cc: 79363 <at> debbugs.gnu.org
Subject: Re: bug#79363: 31.0.50; Font locking issue in go-ts-mode when
 tree-sitter grammar is automatically installed
Date: Sun, 07 Sep 2025 20:39:10 +0200
Juri Linkov <juri <at> linkov.net> writes:

> Thanks, now all 3 patches are pushed.

Thanks.

> Are there other ts-modes that need the same fix?

I have not finished reviewing all modes, so probably yes.  I was waiting
for this first batch to be installed in master before continuing to send
new patches.  I'll send the remaining ones in the next few days.

        Roi




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#79363; Package emacs. (Tue, 09 Sep 2025 07:40:01 GMT) Full text and rfc822 format available.

Message #47 received at 79363 <at> debbugs.gnu.org (full text, mbox):

From: Yuan Fu <casouri <at> gmail.com>
To: Roi Martin <jroi.martin <at> gmail.com>
Cc: 79363 <at> debbugs.gnu.org
Subject: Re: bug#79363: 31.0.50; Font locking issue in go-ts-mode when
 tree-sitter grammar is automatically installed
Date: Tue, 9 Sep 2025 00:38:41 -0700

> On Sep 1, 2025, at 2:31 PM, Roi Martin <jroi.martin <at> gmail.com> wrote:
> 
> Steps to reproduce:
> 
> 1. Run
>     emacs -Q --init-directory=/tmp/go-ts-mode-bug
> 2. Eval
>     (setopt treesit-enabled-modes '(go-ts-mode))
> 3. Open .go file
> 4. Answer y to the question
>     Tree-sitter grammar for `go' is missing; install it?  (y or n)
> 5. go-ts-mode emits the following warning and definitions are not
>   highlighted correctly.
> 
>     ⛔ Warning (treesit-font-lock-rules-mismatch): Emacs cannot compile
>     every font-lock rules because a mismatch between the grammar and the
>     rules.  This is most likely due to a mismatch between the font-lock
>     rules defined by the major mode and the tree-sitter grammar.
> 
>     This error can be fixed by either downgrading the grammar
>     (tree-sitter-go) on your system, or upgrading the major mode package.
>     The following are the temporarily disabled features:
> 
>     - `definition' for go.
> 
> In GNU Emacs 31.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
> 3.24.49, cairo version 1.18.2)
> Repository revision: 3c94ae5a37eac0191fe5d8bd85164b190ac10244
> Repository branch: master
> 
> I took a quick look and I think this is caused by the following issue:
> 
> 1. `go-ts-mode--font-lock-settings' depends on the following functions:
> 
>     (defun go-ts-mode--iota-query-supported-p ()
>       "Return t if the iota query is supported by the tree-sitter-go grammar."
>       (ignore-errors
>         (or (treesit-query-string "" '((iota) @font-lock-constant-face) 'go) t)))
> 
>     (defun go-ts-mode--method-elem-supported-p ()
>       "Return t if Go grammar uses `method_elem' instead of `method_spec'."
>       (ignore-errors
>         (or (treesit-query-string "" '((method_elem) @cap) 'go) t)))
> 
> 2. In this sceneario we don't have the Go grammar installed yet.  So,
>   `treesit-query-string' returns a `not-found' error.
> 3. Because of the previous error, `go-ts-mode--method-elem-supported-p'
>   returns `nil'.  Thus, the "definition" rule uses "method_spec" which
>   is not supported by the default grammar (commit
>   "12fe553fdaaa7449f764bc876fd777704d4fb752").
> 
>     (defvar go-ts-mode--font-lock-settings
>       (treesit-font-lock-rules
>        ...
>        :language 'go
>        :feature 'definition
>        `(...
>          (,(if (go-ts-mode--method-elem-supported-p)
>                'method_elem
>              'method_spec)
>           name: (field_identifier) @font-lock-function-name-face)
> 
> Please, take this analysis with a pinch of salt.  I'm not familiar with
> the code base.  But I hope it makes sense.
> 
>        Roi

Thanks for the analysis! Does that mean once the grammar is installed, and you restart Emacs, go-ts-mode would work as normal?

We do need to fix how we’re defining font-lock rules, the test should be done when major mode is enabled, not when the package is loaded. I thought about this problem and wanted to come up with some generic solution, such as letting treesit-font-lock-rules support a predicate function/query for font-lock-rules, or define rules in a fallback list. I didn’t come up with a very satisfying solution and the progress kind of stalled. Anyway, now it seems I need to put some thought into this and come up with something soon.

Yuan



Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#79363; Package emacs. (Tue, 09 Sep 2025 08:26:02 GMT) Full text and rfc822 format available.

Message #50 received at 79363 <at> debbugs.gnu.org (full text, mbox):

From: Roi Martin <jroi.martin <at> gmail.com>
To: Yuan Fu <casouri <at> gmail.com>
Cc: 79363 <at> debbugs.gnu.org, juri <at> linkov.net
Subject: Re: bug#79363: 31.0.50; Font locking issue in go-ts-mode when
 tree-sitter grammar is automatically installed
Date: Tue, 09 Sep 2025 10:24:49 +0200
Yuan Fu <casouri <at> gmail.com> writes:

> Thanks for the analysis! Does that mean once the grammar is installed,
> and you restart Emacs, go-ts-mode would work as normal?

Yes, that is correct.

> We do need to fix how we’re defining font-lock rules, the test should
> be done when major mode is enabled, not when the package is loaded. I
> thought about this problem and wanted to come up with some generic
> solution, such as letting treesit-font-lock-rules support a predicate
> function/query for font-lock-rules, or define rules in a fallback
> list. I didn’t come up with a very satisfying solution and the
> progress kind of stalled. Anyway, now it seems I need to put some
> thought into this and come up with something soon.

After discussing it with Juri, I've sent a few patches that fix some of
the affected tree-sitter modes.  You can read the conversation in the
issue:

  https://debbugs.gnu.org/cgi/bugreport.cgi?bug=79363

However, I'm working on more patches, so you might want to check the
ones I've already sent.  Feedback is more than welcome.

Thanks!

        Roi




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#79363; Package emacs. (Wed, 10 Sep 2025 07:11:01 GMT) Full text and rfc822 format available.

Message #53 received at 79363 <at> debbugs.gnu.org (full text, mbox):

From: Yuan Fu <casouri <at> gmail.com>
To: Roi Martin <jroi.martin <at> gmail.com>
Cc: 79363 <at> debbugs.gnu.org, juri <at> linkov.net
Subject: Re: bug#79363: 31.0.50; Font locking issue in go-ts-mode when
 tree-sitter grammar is automatically installed
Date: Wed, 10 Sep 2025 00:09:38 -0700

> On Sep 9, 2025, at 1:24 AM, Roi Martin <jroi.martin <at> gmail.com> wrote:
> 
> Yuan Fu <casouri <at> gmail.com> writes:
> 
>> Thanks for the analysis! Does that mean once the grammar is installed,
>> and you restart Emacs, go-ts-mode would work as normal?
> 
> Yes, that is correct.
> 
>> We do need to fix how we’re defining font-lock rules, the test should
>> be done when major mode is enabled, not when the package is loaded. I
>> thought about this problem and wanted to come up with some generic
>> solution, such as letting treesit-font-lock-rules support a predicate
>> function/query for font-lock-rules, or define rules in a fallback
>> list. I didn’t come up with a very satisfying solution and the
>> progress kind of stalled. Anyway, now it seems I need to put some
>> thought into this and come up with something soon.
> 
> After discussing it with Juri, I've sent a few patches that fix some of
> the affected tree-sitter modes.  You can read the conversation in the
> issue:
> 
>  https://debbugs.gnu.org/cgi/bugreport.cgi?bug=79363
> 
> However, I'm working on more patches, so you might want to check the
> ones I've already sent.  Feedback is more than welcome.
> 
> Thanks!
> 
>        Roi

Apologies, apparently I didn’t get any of the messages between you and Juri. I’ve cursed at my mail client and will read the conversation on the archive :-)

Yuan



Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#79363; Package emacs. (Wed, 10 Sep 2025 07:21:02 GMT) Full text and rfc822 format available.

Message #56 received at 79363 <at> debbugs.gnu.org (full text, mbox):

From: Yuan Fu <casouri <at> gmail.com>
To: Roi Martin <jroi.martin <at> gmail.com>
Cc: 79363 <at> debbugs.gnu.org, juri <at> linkov.net
Subject: Re: bug#79363: 31.0.50; Font locking issue in go-ts-mode when
 tree-sitter grammar is automatically installed
Date: Wed, 10 Sep 2025 00:19:50 -0700

> On Sep 10, 2025, at 12:09 AM, Yuan Fu <casouri <at> gmail.com> wrote:
> 
> 
> 
>> On Sep 9, 2025, at 1:24 AM, Roi Martin <jroi.martin <at> gmail.com> wrote:
>> 
>> Yuan Fu <casouri <at> gmail.com> writes:
>> 
>>> Thanks for the analysis! Does that mean once the grammar is installed,
>>> and you restart Emacs, go-ts-mode would work as normal?
>> 
>> Yes, that is correct.
>> 
>>> We do need to fix how we’re defining font-lock rules, the test should
>>> be done when major mode is enabled, not when the package is loaded. I
>>> thought about this problem and wanted to come up with some generic
>>> solution, such as letting treesit-font-lock-rules support a predicate
>>> function/query for font-lock-rules, or define rules in a fallback
>>> list. I didn’t come up with a very satisfying solution and the
>>> progress kind of stalled. Anyway, now it seems I need to put some
>>> thought into this and come up with something soon.
>> 
>> After discussing it with Juri, I've sent a few patches that fix some of
>> the affected tree-sitter modes.  You can read the conversation in the
>> issue:
>> 
>> https://debbugs.gnu.org/cgi/bugreport.cgi?bug=79363
>> 
>> However, I'm working on more patches, so you might want to check the
>> ones I've already sent.  Feedback is more than welcome.
>> 
>> Thanks!
>> 
>>       Roi
> 
> Apologies, apparently I didn’t get any of the messages between you and Juri. I’ve cursed at my mail client and will read the conversation on the archive :-)
> 
> Yuan

I read the thread and it looks good to me. Thanks for working on it!

Yuan



This bug report was last modified 4 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.