orgmode icon indicating copy to clipboard operation
orgmode copied to clipboard

Support nvim-treesitter's parser_install_dir configuration

Open dslinger-a2 opened this issue 8 months ago • 32 comments

Does this feature exist in Emacs orgmode core?

N/A

Orgmode link

No response

Feature value

Currently, the orgmode plugin always installs the org parser to its own package directory ([package_root]/parser/org.so). It would be beneficial to respect nvim-treesitter's parser_install_dir configuration when available, which would allow users to keep all parsers in a single consistent location. This is especially useful in setups where the plugin is being installed by nix.

Additional context

No response

dslinger-a2 avatar Apr 17 '25 22:04 dslinger-a2

I'm trying to use this plugin but it fails to install the treesitter org parser. The nix store is read only so nvim-orgmode fails at the install. Allowing us to handle this dependency on our own would be great!

Also I looked at what I think is the org parser but it is archived? Is this the current parser that is used by orgmode?

rynplynch avatar Apr 20 '25 18:04 rynplynch

This is the official parser being used: https://github.com/nvim-orgmode/tree-sitter-org/tree/next

I'm not sure how nix works, so I'm open for suggestions, as long as it does not involve adding a dependency on nvim-treesitter.

How are you installing other parsers?

kristijanhusak avatar Apr 20 '25 19:04 kristijanhusak

I've been installing tree-sitters parsers via nixpkgs; available parsers found here

We don't have to use the nixpkgs repo though, we could build the parser as a flake output. Then anyone who wants to use the orgmode parser could bring it in as a flake input. They would handle the parser dependency on their own.

Then we would set an environment variable for orgmode to look for. If it finds that environment variable it would use the parser at that location instead of attempting to install on its own. Or we could allow for a global variable to be set. I'm not good with Lua, working on learning it.

I can try writing a flake.nix file for the tree-sitter-org repo. That would be the first step.

rynplynch avatar Apr 20 '25 19:04 rynplynch

I'm ok with using nixpkg. We probably need a github action to publish to nixpkg. Since I have no idea how it works, can you create a PR on https://github.com/nvim-orgmode/tree-sitter-org with the setup? You can follow luarocks release to figure out how to get necessary data for publishing, like version, etc.

Once we do that, I'll figure out a way to handle this separately installed parser, since now I expect it to be installed in the orgmode/parser/ folder.

Does that sound ok?

kristijanhusak avatar Apr 21 '25 09:04 kristijanhusak

That sounds great! I'll see if I can get that done today. I'm looking into norg's implementation and using it to see what a proper implementation looks like.

rynplynch avatar Apr 21 '25 18:04 rynplynch

Any updates on this?

llakala avatar May 02 '25 00:05 llakala

There is now a pull request to update the tree-sitter grammar to the correct version here

I also plan on creating a pull request that introduces an overlay to the parser. None of this will resolve the issue with orgmode attempting to install the parser on it's own though. Will need to point the plugin to the already compiled parser somehow

rynplynch avatar May 02 '25 03:05 rynplynch

Once the version is updated I'll update the code to not require a parser to be located in the orgmode folder. That should solve the issue. With nixos, are you able to load the parser before orgmode? Because orgmode will attempt to install the parser if it's missing while loading the setup.

kristijanhusak avatar May 02 '25 17:05 kristijanhusak

In my configuration I am able to build tree-sitter with the grammars I would like to use. This means that by the time the setup function for orgmode gets called there is already a compiled org.so library.

This path on my system contains all the parsers installed on my system.

/nix/store/rh6sba98r48dkzpxwh8lj34cqb18zhhg-vim-pack-dir/pack/myNeovimGrammars/start/vimplugin-treesitter-grammar-ALL-INCLUDED/parser/

If I could point orgmode to that directory it would find the org.so file.

Perhaps a new configuration option in the setup function for orgmode could achieve this?

rynplynch avatar May 02 '25 18:05 rynplynch

This path on my system contains all the parsers installed on my system

Is this path part of your rtp ? If yes, then there's no need the configuration option. It should already read the parser from there. The problem is that there's a check to expect the parser to in orgmode/parser folder, which I plan to remove. As long as there is an org parser, we should be fine.

kristijanhusak avatar May 02 '25 18:05 kristijanhusak

That's a good question. I'm new to Lua and neovim so take what I say with a grain of salt. All I know is that when I perform a healthcheck on tree-sitter it shows the org parser as OK.

I'm guessing that if tree-sitter can find it all right then it should work fine with orgmode?

rynplynch avatar May 02 '25 21:05 rynplynch

You can check by doing :echo &rtp and finding the path in the comma separated list. Most likely it's there since other languages are working fine.

I'll be updating the logic around this in the following days to not require the parser to be in the orgmode folder. It will still notify if there is more than 1 org parser found, but that should not happen if you load the parser before the orgmode is loaded.

kristijanhusak avatar May 03 '25 07:05 kristijanhusak

Another nix user here 😄 there are several issues I stumbled on while making orgmode usable.

I patched the TS grammar in my flake:

              (nvim-treesitter.withPlugins (_:
                 nvim-treesitter.allGrammars ++
                [(pkgs.tree-sitter.buildGrammar {
                  language = "org";
                  version = "2.0.0";
                  src = pkgs.fetchFromGitHub {
                    owner = "nvim-orgmode";
                    repo = "tree-sitter-org";
                    rev = "2.0.0";
                    hash = "sha256-+eixAPvEq7LPylaovrSeT18h6FKnhVoYS68B1B5xhPs=";
                  };
                  meta.homepage = "https://github.com/nvim-orgmode/tree-sitter-org";
                })]))

and made nvim-treesitter aware of it:

      local parser_config = require "nvim-treesitter.parsers".get_parser_configs()
      parser_config.org = {
        filetype = "org",
        install_info = {
        },
      }

This, however, doesn't stop orgmode from trying to install the ts plugin again. I had to monkeypatch orgmode's installer to not do anything:

    config = function()
      -- Setup orgmode
      local ts_install = require('orgmode.utils.treesitter.install')
      ts_install.install = function() return true end

      require('orgmode').setup({
        ...

The problem is that despite all this and TS clearly working (as seen in :InspectTree below), the highlighting does not. Actually, no "nicer" features work, e.g. the links don't hide the extra formatting symbols by default as seen in the demo gifs.

Image

I'm not sure if that's because how I shoveled the TS grammar in or there's some obscure integration bug.

farcaller avatar May 07 '25 18:05 farcaller

Oddly enough, if I symlink the orgmode's queries into my neovim config folder, then it seems to work. 🤔

farcaller avatar May 07 '25 22:05 farcaller

I'll update the installation during the weekend to allow for any location for the parser. That should solve most of these issues

kristijanhusak avatar May 08 '25 05:05 kristijanhusak

I updated the treesitter installation logic to not require the parser to be installed in orgmode folder. Released in 0.6.0 version. Let me know if that fixes it for you.

kristijanhusak avatar May 11 '25 19:05 kristijanhusak

@kristijanhusak still doesn't work:

...s/start/orgmode/lua/orgmode/utils/treesitter/install.lua:117: EACCES: permission denied: /nix/store/3297n9ymij2xh2123x3s269mr0433j28-vim-pack -dir/pack/myNeovimPackages/start/orgmode/.org-ts-lock.json

It seems some codepath still tries to write a lockfile?

And just for the record, that's with org grammar installed as per :checkhealth vim.treesitter:

  • ✅ OK Parser: org ABI: 14, path: /nix/store/kjzz6565l7ij4zmj3ya3v0w1n3qmiqhk-vimplugin-treesitter-grammar-ALL-INCLUDED/parser/org.so

farcaller avatar May 11 '25 20:05 farcaller

@farcaller what about :checkhealth orgmode ?

kristijanhusak avatar May 11 '25 20:05 kristijanhusak

Orgmode ~

  • ❌ ERROR Failed to run healthcheck for "orgmode" plugin. Exception: ...s/start/orgmode/lua/orgmode/utils/treesitter/install.lua:117: EACCES: permission denied: /nix/store/3297n9ymij2xh2123x3s269mr0433j28-vim-pack-dir/pack/myNeovimPackages/start/orgmode/.org-ts-lock.json

farcaller avatar May 11 '25 20:05 farcaller

The problem here is because you expect the orgmode's plugin root directory is in user's $HOME somewhere, but in nix it's in the global and very read-only store.

farcaller avatar May 11 '25 20:05 farcaller

It seems some codepath still tries to write a lockfile?

Yeah it did. Just pushed fixed to master. Can you give it another test?

kristijanhusak avatar May 11 '25 20:05 kristijanhusak

It loads without issues now! But now we're back to this problem: https://github.com/nvim-orgmode/orgmode/issues/967#issuecomment-2860491966. There's no syntax highlighting whatsoever.

farcaller avatar May 11 '25 20:05 farcaller

Do you get anything from this command when you run it from the opened org file?

:lua= vim.treesitter.query.get_files('org', 'highlights')

Also, do you have to do something around queries for nvim-treesitter?

kristijanhusak avatar May 11 '25 21:05 kristijanhusak

I do:

{ "/nix/store/kjzz6565l7ij4zmj3ya3v0w1n3qmiqhk-vimplugin-treesitter-grammar-ALL-INCLUDED/queries/org/highlights.scm" }

Which seemingly comes from nvim-orgmode/tree-sitter-org.

farcaller avatar May 11 '25 21:05 farcaller

I released 2.0.1 version of tree-sitter-org that removes those queries. Those are just the examples. Can you try using version 2.0.1 and see if it works?

kristijanhusak avatar May 12 '25 07:05 kristijanhusak

That seems to do the trick, thanks! And thank you so much for all the debugging effort on this!

farcaller avatar May 12 '25 14:05 farcaller

Awesome! So there's no need for any monkey patching? I'm still open to having a custom nix packages for both orgmode and tree-sitter-org, but I'm not a nix user so no idea how that works.

kristijanhusak avatar May 12 '25 14:05 kristijanhusak

Yep, it all works as expected now and the highlights are pulled from the correct place:

{ "/nix/store/76wnqzh7lviz0kczqaljacnhgysqx3sx-vim-pack-dir/pack/myNeovimPackages/start/orgmode/queries/org/highlights.scm" }

It's still somewhat strange they both aren't being pulled in, but whatever, it all works as intended now.

I am currently not in a position to submit new packages towards nixpkgs (it's complicated), but I expect that to change soon-ish, so if no one else beats me to it I'll work on adding both.

farcaller avatar May 12 '25 14:05 farcaller

Is there anything else that needs to be done over on nixpkgs for the issue to be fully resolved?

NovaViper avatar Jun 16 '25 00:06 NovaViper

With:

  programs.neovim = {
    enable = true;
    defaultEditor = true;
    viAlias = true;
    vimAlias = true;
    plugins = with pkgs.vimPlugins; [
      (nvim-treesitter.withPlugins (p: [
        p.tree-sitter-org-nvim
      ]))
      orgmode
    ];
  };

I am getting the following error:

Error detected while processing BufNewFile Autocommands for "*":
Error executing lua callback: ...-neovim-unwrapped-0.11.3/share/nvim/runtime/filetype.lua:36: BufNewFile Autocommands for "*"..FileType Autocommands for "*"..function <SNR>1_LoadFTPlugin[20]..script /nix/store/j9qp815g7q3kp7cphmq6qs71kr9mx5hr-vimplugin-luajit2.1-orgmode-0.5.3-1-unstable-0.5.3-1/ftplugin/org.lua: Vim(runtime):E5113: Error while calling lua chunk: ...wr
apped-0.11.3/share/nvim/runtime/lua/vim/treesitter.lua:431: Parser could not be created for buffer 1 and language "org"
stack traceback:
        [C]: in function 'assert'
        ...wrapped-0.11.3/share/nvim/runtime/lua/vim/treesitter.lua:431: in function 'start'
        ...jit2.1-orgmode-0.5.3-1-unstable-0.5.3-1/ftplugin/org.lua:9: in main chunk
        [C]: in function 'nvim_cmd'
        ...-neovim-unwrapped-0.11.3/share/nvim/runtime/filetype.lua:36: in function <...-neovim-unwrapped-0.11.3/share/nvim/runtime/filetype.lua:35>
        [C]: in function 'pcall'
        vim/shared.lua: in function <vim/shared.lua:0>
        [C]: in function '_with'
        ...-neovim-unwrapped-0.11.3/share/nvim/runtime/filetype.lua:35: in function <...-neovim-unwrapped-0.11.3/share/nvim/runtime/filetype.lua:10>
stack traceback:
        [C]: in function '_with'
        ...-neovim-unwrapped-0.11.3/share/nvim/runtime/filetype.lua:35: in function <...-neovim-unwrapped-0.11.3/share/nvim/runtime/filetype.lua:10>

Could anyone help me fix the issue?

font44 avatar Sep 05 '25 04:09 font44