Tree sitter grammars collide with each other

I’m trying to install treesitter grammars with home manager. Added

home.packages = with pkgs.tree-sitter-grammars; [
    tree-sitter-bash
    tree-sitter-latex
    tree-sitter-nix
    tree-sitter-ocaml
    tree-sitter-ocaml-interface
    tree-sitter-python
    tree-sitter-rust
    tree-sitter-toml
];

Unfortunately, when I try to rebuild, I get the following error

error: collision between `/nix/store/gk8fghjz1dyc6vh00kxca87wg370dsi4-tree-sitter-latex-grammar-0.20.9/parser' and `/nix/store/kcr5qfzrk1vlmhd8qj8l3k9nfj8m5n2q-tree-sitter-bash-grammar-0.20.9/parser'

I’m not sure how to install these grammars. Since I use Emacs, I cannot use the available prepackaged neovim dedicated plugins.

this is almost certainly not the correct way to install the grammars, all grammars have the same filename and will collide with eachother as you noticed. There must be some emacs infrastructure to do so, like for neovim.

Indeed, but I couldn’t find it. I’ve searched for “tree sitter” in the Emacs overlay and in nixpkgs’ definition of Emacs, but besides a compile flag that controls native support for tree sitter (which is on in my config), there doesn’t seem to be anything related to tree sitter. Querying Emacs to know whether a grammar is available (eg. Rust’s grammar) yields that the grammar is not availble:

(treesit-language-available-p 'rust) ;; nil

What about emacsWithPackages (ps: with ps; [ tree-sitter tree-sitter-langs ]) ?
https://search.nixos.org/packages?channel=unstable&from=0&size=50&sort=relevance&type=packages&query=emacsPackages.tree-sitter-langs

I had already installed these packages, but it doesn’t seem to work. For instance, launching the TOML mode with tree sitter support yields

⛔ Warning (treesit): Cannot activate tree-sitter, because language grammar for toml is unavailable (not-found):
/nix/store/wd9jym9m1gvsccq71zipmr0imr0pncvh-emacs-packages-deps/lib/libtree-sitter-toml: cannot open shared object file: No such file or directory
/nix/store/wd9jym9m1gvsccq71zipmr0imr0pncvh-emacs-packages-deps/lib/libtree-sitter-toml.0: cannot open shared object file: No such file or directory
/nix/store/wd9jym9m1gvsccq71zipmr0imr0pncvh-emacs-packages-deps/lib/libtree-sitter-toml.0.0: cannot open shared object file: No such file or directory
/nix/store/wd9jym9m1gvsccq71zipmr0imr0pncvh-emacs-packages-deps/lib/libtree-sitter-toml.so: cannot open shared object file: No such file or directory
/nix/store/wd9jym9m1gvsccq71zipmr0imr0pncvh-emacs-packages-deps/lib/libtree-sitter-toml.so.0: cannot open shared object file: No such file or directory
/nix/store/wd9jym9m1gvsccq71zipmr0imr0pncvh-emacs-packages-deps/lib/libtree-sitter-toml.so.0.0: cannot open shared object file: No such file or directory
/home/<redacted>/.emacs.d/tree-sitter/libtree-sitter-toml: cannot open shared object file: No such file or directory
/home/<redacted>/.emacs.d/tree-sitter/libtree-sitter-toml.0: cannot open shared object file: No such file or directory
/home/<redacted>/.emacs.d/tree-sitter/libtree-sitter-toml.0.0: cannot open shared object file: No such file or directory
/home/<redacted>/.emacs.d/tree-sitter/libtree-sitter-toml.so: cannot open shared object file: No such file or directory
/home/<redacted>/.emacs.d/tree-sitter/libtree-sitter-toml.so.0: cannot open shared object file: No such file or directory
/home/<redacted>/.emacs.d/tree-sitter/libtree-sitter-toml.so.0.0: cannot open shared object file: No such file or directory
libtree-sitter-toml: cannot open shared object file: No such file or directory
libtree-sitter-toml.0: cannot open shared object file: No such file or directory
libtree-sitter-toml.0.0: cannot open shared object file: No such file or directory
libtree-sitter-toml.so: cannot open shared object file: No such file or directory
libtree-sitter-toml.so.0: cannot open shared object file: No such file or directory
libtree-sitter-toml.so.0.0: cannot open shared object file: No such file or directory

which clearly shows that the grammar was not installed.

True, that doesn’t install the parsers, treesit-grammars is missing:

emacsWithPackages (ps: with ps; [ tree-sitter tree-sitter-langs treesit-grammars.with-all-grammars ])

With that, C-x C-e over (treesit-language-available-p 'rust) returns true when using nixpkgs-unstable with emacs 29.2

To install just the grammars I mentioned in the OP, I adapted your snippet to

(treesit-grammars.with-grammars (grammars: with grammars; [
   tree-sitter-bash
   tree-sitter-latex
   tree-sitter-nix
   tree-sitter-ocaml
   tree-sitter-ocaml-interface
   tree-sitter-python
   tree-sitter-rust
   tree-sitter-toml
]))

and that works! I wonder how you discovered that package, though. It doesn’t seem to appear when I query for it on search.nixos.org.

Well, actually, I tested it for Nix (as I was editing a Nix file) and it worked; but as soon as I opened a Rust projected, I realized it didn’t work…

When trying to have tree-sitter support for a TOML file, it prints

tree-sitter-load: Cannot find shared library for language: toml

and on a Rust file

tree-sitter-load: Language’s ABI is too new: 14, (13 . 13), "/nix/store/ylfqz9vy0kxn6sdy00h3dk7drdgnqvx7-emacs-tree-sitter-grammars/langs/bin/rust.so"

the first issue is something I ran into in past as well. I’ve created a PR, but not sure if it will fix the issue:

1 Like

I’m sorry that it didn’t work. It seems like you’re not the only one: emacsPackages.tree-sitter-langs: Python grammar uses incompatible ABI version · Issue #209114 · NixOS/nixpkgs · GitHub

I’m not an emacs user myself, maybe Nix emacs users are using something else (GitHub - nix-community/emacs-overlay: Bleeding edge emacs overlay [maintainer=@adisbladis] perhaps), or we are missing something.

The issue that I linked shows how to create an overlay with a lower ABI version, which, at least, should solve the rust issue.

Ideally, emacs should have a package to install tree-sitter with the correct ABI versions, like we have vimPlugins.nvim-treesitter.