elisp-tree-sitter icon indicating copy to clipboard operation
elisp-tree-sitter copied to clipboard

Reload shared (grammar) library within an emacs session

Open abradd opened this issue 3 years ago • 3 comments

Is it possible to reload the .so shared library for a grammar within the current emacs session? Nothing else would change besides the shared library being updated from a more recent compile.

abradd avatar Jun 13 '21 00:06 abradd

I've been testing this to attempt to get the shared object to reload.

I've noticed that `tree-sitter-require' is returning the same object between compiles of the grammar (in which the grammar has changed), but different objects when I launch a new emacs session:

Prior to recompile with changes:

#<user-ptr ptr=0x7ff63fef4da0 finalizer=0x7ff6441f9c60>

After recompile with changes:

#<user-ptr ptr=0x7ff63fef4da0 finalizer=0x7ff6441f9c60>

After launching a new emacs session:

#<user-ptr ptr=0x7f072d873da0 finalizer=0x7f072d98ec60>

abradd avatar Jun 16 '21 00:06 abradd

tsc--load-language is also returning the same object even when a new shared object file including a change to the grammar is used.

It seems that the issue is either with the emacs modules interface or the underlying rust code. I found the _load_language function in lang.rs which seems to be responsible for loading the shared object. Unfortunately I'm unfamiliar with both rust and the emacs modules interface.

abradd avatar Jun 17 '21 00:06 abradd

It's possible, by first unloading the old grammar.

  • Run cleanup code in affected tree-sitter-mode buffers.
  • Unload the old shared object, by removing the corresponding language from LANG_INFOS.
  • Load the language again.
  • Re-initialize tree-sitter-mode in affected buffers.

It is a bit unsafe, as the old grammar may still be held by user code somewhere. However, it would mostly affect libraries that manage the lifecycle of the parsers and the syntax trees on their own. Libraries that rely on tree-sitter-mode to do so should be fine.

ubolonton avatar Jul 24 '21 09:07 ubolonton