I’m using Emacs version 29.1 on macOS, installed by way of Home Manager, with the package overridden to “emacs29”. Within Emacs, if I visit a TypeScript file in a buffer and invoke typescript-ts-mode, Emacs presents a “Warnings” buffer with the following complaints:
Warning (treesit): Cannot activate tree-sitter, because language grammar for typescript is unavailable (not-found): (libtree-sitter-typescript.so libtree-sitter-typescript.so.0 libtree-sitter-typescript.so.0.0 libtree-sitter-typescript.dylib libtree-sitter-typescript.dylib.0 libtree-sitter-typescript.dylib.0.0) No such file or directory Warning (treesit): Cannot activate tree-sitter, because language grammar for tsx is unavailable (not-found): (libtree-sitter-tsx.so libtree-sitter-tsx.so.0 libtree-sitter-tsx.so.0.0 libtree-sitter-tsx.dylib libtree-sitter-tsx.dylib.0 libtree-sitter-tsx.dylib.0.0) No such file or directory Warning (treesit): Cannot activate tree-sitter, because language grammar for typescript is unavailable (not-found): (libtree-sitter-typescript.so libtree-sitter-typescript.so.0 libtree-sitter-typescript.so.0.0 libtree-sitter-typescript.dylib libtree-sitter-typescript.dylib.0 libtree-sitter-typescript.dylib.0.0) No such file or directory
Clearly more things need to be installed. However, searching for Nix packages related to tree-sitter, Emacs, and TypeScript yield many hits, and it’s not clear which of them overlap and are pertinent to this problem. Here are a few candidates that sound related:
tree-sitter
tree-sitter-grammars.tree-sitter-tsx
tree-sitter-grammars.tree-sitter-typescript
emacsPackages.tree-sitter
emacsPackages.tree-sitter-langs
This last package’s source code mentions dealing with file names ending with “dylib” on macOS, so that might address the missing files mentioned in the error messages above.
Can anyone share a working recipe for some use of these packages—or any others I didn’t find—that allow proper syntax-based highlighting to work within Emacs?
Thank you. I do have both of those installed already.
At present, the treesit-language-source-alist variable remains empty. I thought that there would be some Nix package that would populate it with entries automatically. So far I have not figured out which—if any—package takes care of that.
Without that populated, I can’t invoke the treesit-install-language-grammar function. That function downloads and builds the grammar source. Similarly, I thought that there might be a Nix package that would include those artifacts.
Oh, I’m sorry about mentioning the overriding part. I meant to say that I’m winding up with Emacs version 29.1 by way of using the “emacs29” Nix package.
Regardless, I’ll study that Emacs package that you mentioned to see if it may help.
You definitely should NOT install the external emacsPackages.tree-sitter unless you really know what you’re doing. This last package exposes a minor mode unrelated to the major mode typescript-ts-mode that is included in base Emacs.
Oh, I see. Thank you for pointing to the source. I tried it, and it works, addressing my original concern.
While there, though, I noticed that with no other configuration Emacs doesn’t know that a file with an extension of “.tsx” should use a TypeScript-related mode. Which package do you recommend installing and activating to get that entry into the file mode alist?
Funny enough, the upstream guys deliberately don’t autoload the auto-mode-alist updates for the treesit-based major modes; I am guessing this is because they don’t want to step on the toes of the existing non-treesit-based major modes for which treesit-based variants now exist. Contrast this
I decided to also install the emacsPackages.treesit-auto package for now, anticipating that it may no longer be necessary once we can use Emacs version 30 and later.
With this configuration, the treesit-auto package takes care of borrowing the auto-mode-alist entries from the sibling non-treesit modes.
I also use MacOS and Emacs29 can’t find those library files even I use treesit-grammars.with-all-grammars, which is created by pkgs.linkFarm "emacs-treesit-grammars"
I figure out a way to fix the problem
let
buildEmacs = (pkgs.emacsPackagesFor pkgs.emacs29-macport).emacsWithPackages;
treesitGrammars =
(pkgs.emacsPackagesFor pkgs.emacs29-macport).treesit-grammars.with-all-grammars;
emacs = buildEmacs (epkgs: with epkgs; [ vterm treesitGrammars ]);
in {
home.packages = [ emacs ];
home.file = {
# tree-sitter subdirectory of the directory specified by user-emacs-directory
".config/emacs/.local/cache/tree-sitter".source =
"${emacs29.treesitGrammars}/lib";
};
}