danderson / templ-ts-mode

Emacs major mode for editing Templ files
GNU General Public License v3.0
13 stars 2 forks source link

"Node type error" in font-lock treesit queries #6

Open IT-Luka opened 9 months ago

IT-Luka commented 9 months ago

GNU Emacs 29.2 Doom Emacs

Initially it was complaining about not finding libtree-sitter-gomod.so, the tree-sitter-module creates libtree-sitter-go-mod.so so i just copied it with the gomod name.

There's no highlighting, indents are 8 spaces wide, output of the message buffer after opening a .templ file:

Note: standard-indent, tab-width, evil-shift-width adjusted to 4 Error during redisplay: (jit-lock-function 1) signaled (treesit-query-error "Node type error at" 2 "(function name: (identifier) @font-lock-function-name-face) (class_declaration name: (identifier) @font-lock-type-face) (function_declaration name: (identifier) @font-lock-function-name-face) (method_definition name: (property_identifier) @font-lock-function-name-face) (formal_parameters [(identifier) @font-lock-variable-name-face (array_pattern (identifier) @font-lock-variable-name-face) (object_pattern (shorthand_property_identifier_pattern) @font-lock-variable-name-face)]) (variable_declarator name: (identifier) @font-lock-variable-name-face) (variable_declarator name: (identifier) @font-lock-function-name-face value: [(function) (arrow_function)]) (variable_declarator name: [(array_pattern (identifier) @font-lock-variable-name-face) (object_pattern (shorthand_property_identifier_pattern) @font-lock-variable-name-face)]) (import_clause (identifier) @font-lock-variable-name-face) (import_clause (named_imports (import_specifier alias: (identifier) @font-lock-variable-name-face))) (import_clause (named_imports (import_specifier !alias name: (identifier) @font-lock-variable-name-face))) (import_clause (namespace_import (identifier) @font-lock-variable-name-face))" "Debug the query with `treesit-query-validate'")

danderson commented 9 months ago

Sounds like you might have an older version of a grammar installed, or your tree-sitter installation is broken in some other way. All font-locking queries work for me with current versions of the grammars, so there's nothing I can do.

IT-Luka commented 8 months ago

Sounds like you might have an older version of a grammar installed

The templ grammar was installed through (treesit-install-language-grammar) after installing templ-ts-mode.

or your tree-sitter installation is broken in some other way

When I run (tree-sitter-mode) in a .templ file, the response is "if: No language registered for major mode ‘templ-ts-mode’". It works without a problem for other languages.

After installing templ-ts-mode, when opening .templ files, I was getting errors about no grammars for go, gomod, and js. Those grammars were installed through (tree-sitter-langs-install-latest-grammar), and were working in other modes.

To get templ-ts-mode to find grammars for those languages, i had to compile them, and add this line to my config (setq treesit-extra-load-path '("/usr/local/lib/dist")). Setting the extra load path to the one in tree-sitter-langs-grammar-dir didn't help.

I tried copying libtree-sitter-templ.so to both treesit-extra-load-path and tree-sitter-langs-grammar-dir, but that didn't change anything.

hgoes commented 8 months ago

I'm facing exactly the same issue after just installing everything, so I doubt it's due to an older version.

toshism commented 8 months ago

I'm also having the same issue with the exact same error message. I've updated the grammar as well. I'm using the built in treesitter mode in emacs 29.2 if that is helpful. I don't think it's a problem with my treesitter setup in general because I have no problems with anything other than templ.

I'm happy to try to help debug if there is any additional information i can provide.

danderson commented 8 months ago

I just updated my emacs and templ grammar, and everything still works fine. I don't know what to tell you. If someone can figure out what's going on, I welcome patches, but I don't have the time or emacs skills to debug this remotely.

toshism commented 8 months ago

Ok, thanks for looking. I'll try to dig into at some point and update if I find any useful information.

efim commented 8 months ago

I'm experiencing same error,

closed my ticket as a duplicate, but comment here minimal reproduction for me is

M-x version: GNU Emacs 29.1 (build 1, x86_64-pc-linux-gnu, X toolkit, cairo version 1.18.0, Xaw3d scroll bars)

scratch

(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
(package-refresh-contents)
(package-install 'templ-ts-mode)  ; Assuming templ-ts-mode is available in the package repository

;; manual javascript tree-sitter grammar install with
;; M-x treesit-install-language-grammar javascript

then opening test.templ from this repo i get prompt to install templ-tree-sitter from the function that's defined in this major mode after that and checking font-lock and messages

@danderson could you please check whether this emacs -Q case is replicated at your side?

I'm not too proficient to figure out next steps.

efim commented 8 months ago

I tried checking with Emacs 30 - nothing changed, tried manually checking

  (treesit-query-validate 'templ '(["templ" "css" "script"] @font-lock-keyword-face))
  (treesit-query-validate 'templ '((css_property_name) @css-property))

the queries i found in the templ-ts-mode.el

But all seemed to work, and I couldn't figure out from where do some of the queries listed in the error coming from. My only guess was that maybe those are js queries,

I tried commenting out js part of font-lock, rerunning (templ-ts--setup) and i got most of the font-lock back in the test.templ file

2024-03-13_03-02

I will try to figure out more, on where exactly problems coming from, but I'm not at all educated in tree-sitter. Hopefully narrowing it down to js rules helps.

How could we check versions of js parser? Or where do the js rules come from, maybe that is some external place where version could be misaligned?

edit: i seem to have narrowed the problem down to this part

   :language 'javascript
   :feature 'definition
   '((function ...

running

(treesit-query-validate 'javascript '((function
      name: (identifier) @font-lock-function-name-face)

     (class_declaration
      name: (identifier) @font-lock-type-face)

     (function_declaration
      name: (identifier) @font-lock-function-name-face)

     (method_definition
      name: (property_identifier) @font-lock-function-name-face)

     (method_definition
      parameters: (formal_parameters (identifier) @font-lock-variable-name-face))

     (arrow_function
      parameters: (formal_parameters (identifier) @font-lock-variable-name-face))

     (function_declaration
      parameters: (formal_parameters (identifier) @font-lock-variable-name-face))

     (variable_declarator
      name: (identifier) @font-lock-variable-name-face)

     (variable_declarator
      name: (identifier) @font-lock-function-name-face
      value: [(function) (arrow_function)])

     (variable_declarator
      name: [(array_pattern (identifier) @font-lock-variable-name-face)
             (object_pattern
              (shorthand_property_identifier_pattern) @font-lock-variable-name-face)])

     ;; full module imports
     (import_clause (identifier) @font-lock-variable-name-face)
     ;; named imports with aliasing
     (import_clause (named_imports (import_specifier
                                    alias: (identifier) @font-lock-variable-name-face)))
     ;; named imports without aliasing
     (import_clause (named_imports (import_specifier
                                    !alias
                                    name: (identifier) @font-lock-variable-name-face)))

     ;; full namespace import (* as alias)
     (import_clause (namespace_import (identifier) @font-lock-variable-name-face))))

results in same error. other queries from js--treesit-font-lock-settings are reported as valid

oh, it comes from js.el.gz, not sure how to check it's version and (treesit-language-abi-version 'javascript) for me returns 14

But! I do actually get the same error when i enable js-ts-mode in a js file it's just there is full font lock coloring, so i didn't check further

2024-03-13_03-43

So @danderson unless you also know what could be done, it seems the problem is not in your package. The only thing that comes to mind is maybe there's some way to make it more robust to errors, so that errors in js part fontification wouldn't lead to templ part fontification. I don't know whether that is possible, but there is something in the js mode that does fallback it seems.

i'll try to figure out how to raise that with the js mode authors, in the meanwhile disabling font lock for js is a workaround

maybe there's a simple way to upgrade \ downgrade the js tree-sitter parser, i've been installing with all defaults by treesit-install-language-grammar RET javascript RET

@danderson what was your way to install the js grammar, so that it works on your end?

danderson commented 8 months ago

Hmm, interesting. Thank you for digging! I very much appreciate it.

So, one very unfortunate thing with tree-sitter, is that the grammars aren't versioned. The ABI is, but that just tells you the version of the tree-sitter framework you're using, not if you're using a buggy grammar from 3 years ago or a fixed one from yesterday. This is an inherent weakness in the tree-sitter ecosystem, not something emacs-specific :(

Narrowing this down to the JS grammar may explain why I'm not seeing it: I'm running NixOS and installing treesit grammars through my OS. I assumed that the grammars were fairly up to date (usually these things get updated in nixos via cronjobs that sync to upstream latest), but it's possible I'm running on an outdated js grammar. The only one I explicitly pin to latest myself is the templ grammar, for obvious reasons.

I will try to find time to set up a more vanilla emacs and install the JS grammar through emacs' builtin mechanism, and see if I can reproduce that way.

Assuming the js grammar changed in an incompatible way, the good news is that I have seen a pattern for dealing with that in other treesit modes: we can probe "hey is this treesit query valid according to the grammar?" during initial setup, and depending on the answer adjust the font-lock rules we define. It's not ideal that we have to do this for the javascript grammar, ideally that would get fixed elsewhere and we would inherit it... But emacs's support for multiple grammars per file is still very new and a bit shaky, so we'll probably have to hack something into templ-ts-mode at least for now.