eddiebergman / nvim-treesitter-pyfold

Some sane Python folding with nvim-treesitter
33 stars 3 forks source link

`bad argument #1 to 'open' (string expected, got nil)` #12

Open dir01 opened 7 months ago

dir01 commented 7 months ago

The plugin is installed with https://github.com/folke/lazy.nvim as following:

    {
        "eddiebergman/nvim-treesitter-pyfold",
        ft = "python",
    },

When opening a python file, the following error happens

Error detected while processing function <SNR>38_NetrwBrowseChgDir[197]..BufReadPost Autocommands for "*":
Error executing lua callback: ...ocal/Cellar/neovim/0.9.5/share/nvim/runtime/filetype.lua:24: Error executing lua: ...ocal/Cellar/neovim/0.9.5/share/nvim/runtime/filetype.lua:25: function <
SNR>38_NetrwBrowseChgDir[197]..BufReadPost Autocommands for "*"..FileType Autocommands for "python"..FileType Autocommands for "*": Vim(append):Error executing lua callback: ...treesitter-p
yfold/lua/nvim-treesitter-pyfold/folding.lua:24: bad argument #1 to 'open' (string expected, got nil)
stack traceback:
        [C]: in function 'open'
        ...treesitter-pyfold/lua/nvim-treesitter-pyfold/folding.lua:24: in function 'readfile'
        ...treesitter-pyfold/lua/nvim-treesitter-pyfold/folding.lua:34: in function 'attach'
        ...vim/lazy/nvim-treesitter/lua/nvim-treesitter/configs.lua:509: in function 'attach_module'
        ...vim/lazy/nvim-treesitter/lua/nvim-treesitter/configs.lua:532: in function 'reattach_module'
        ...vim/lazy/nvim-treesitter/lua/nvim-treesitter/configs.lua:133: in function <...vim/lazy/nvim-treesitter/lua/nvim-treesitter/configs.lua:132>
        [C]: in function 'nvim_exec_autocmds'
        ...hare/nvim/lazy/lazy.nvim/lua/lazy/core/handler/event.lua:157: in function <...hare/nvim/lazy/lazy.nvim/lua/lazy/core/handler/event.lua:156>
        [C]: in function 'xpcall'
        .../.local/share/nvim/lazy/lazy.nvim/lua/lazy/core/util.lua:113: in function 'try'
        ...hare/nvim/lazy/lazy.nvim/lua/lazy/core/handler/event.lua:156: in function 'trigger'
        ...hare/nvim/lazy/lazy.nvim/lua/lazy/core/handler/event.lua:87: in function <...hare/nvim/lazy/lazy.nvim/lua/lazy/core/handler/event.lua:72>
        [C]: in function 'nvim_cmd'
        ...ocal/Cellar/neovim/0.9.5/share/nvim/runtime/filetype.lua:25: in function <...ocal/Cellar/neovim/0.9.5/share/nvim/runtime/filetype.lua:24>
        [C]: in function 'nvim_buf_call'
        ...ocal/Cellar/neovim/0.9.5/share/nvim/runtime/filetype.lua:24: in function <...ocal/Cellar/neovim/0.9.5/share/nvim/runtime/filetype.lua:10>
stack traceback:
        [C]: in function 'nvim_cmd'
        ...ocal/Cellar/neovim/0.9.5/share/nvim/runtime/filetype.lua:25: in function <...ocal/Cellar/neovim/0.9.5/share/nvim/runtime/filetype.lua:24>
        [C]: in function 'nvim_buf_call'
        ...ocal/Cellar/neovim/0.9.5/share/nvim/runtime/filetype.lua:24: in function <...ocal/Cellar/neovim/0.9.5/share/nvim/runtime/filetype.lua:10>
stack traceback:
        [C]: in function 'nvim_buf_call'
        ...ocal/Cellar/neovim/0.9.5/share/nvim/runtime/filetype.lua:24: in function <...ocal/Cellar/neovim/0.9.5/share/nvim/runtime/filetype.lua:10>
Press ENTER or type command to continue
dir01 commented 7 months ago
:set rtp?
  runtimepath=~/.config/nvim,~/.local/share/nvim/lazy/lazy.nvim,~/.local/share/nvim/lazy/LuaSnip,~/.local/share/nvim/lazy/nvim-cmp,~/.local/share/nvim/lazy/telescope-live-grep-args.nvim,~/.
local/share/nvim/lazy/nvim-web-devicons,~/.local/share/nvim/lazy/telescope.nvim,~/.local/share/nvim/lazy/nvim-treesitter-pyfold,~/.local/share/nvim/lazy/lsp-zero.nvim,~/.local/share/nvim/la
zy/mason-lspconfig.nvim,~/.local/share/nvim/lazy/cmp-nvim-lsp,~/.local/share/nvim/lazy/nvim-lspconfig,~/.local/share/nvim/lazy/vim-tmux-navigator,~/.local/share/nvim/lazy/git-blame.nvim,~/.
local/share/nvim/lazy/vim-repeat,~/.local/share/nvim/lazy/vim-surround,~/.local/share/nvim/lazy/which-key.nvim,~/.local/share/nvim/lazy/auto-save.nvim,~/.local/share/nvim/lazy/plenary.nvim,
~/.local/share/nvim/lazy/harpoon,~/.local/share/nvim/lazy/vim-gitgutter,~/.local/share/nvim/lazy/copilot.vim,~/.local/share/nvim/lazy/mason.nvim,~/.local/share/nvim/lazy/nvim-treesitter,~/.
local/share/nvim/lazy/dressing.nvim,~/.local/share/nvim/lazy/auto-dark-mode.nvim,~/.local/share/nvim/lazy/catppuccin,/usr/local/Cellar/neovim/0.9.5/share/nvim/runtime,/usr/local/Cellar/neov
im/0.9.5/share/nvim/runtime/pack/dist/opt/matchit,/usr/local/Cellar/neovim/0.9.5/lib/nvim,~/.local/state/nvim/lazy/readme,~/.local/share/nvim/lazy/cmp-nvim-lsp/after,~/.local/share/nvim/laz
y/catppuccin/after
dir01 commented 7 months ago
:lua =vim.api.nvim_get_runtime_file('**/nvim-treesitter-pyfold/queries/python/folds.scm', false)
{}
:lua =vim.api.nvim_get_runtime_file('queries/python/folds.scm', false)
{ "/Users/foo/.local/share/nvim/lazy/nvim-treesitter-pyfold/queries/python/folds.scm" }
dir01 commented 7 months ago

So looks like the issue is with rtp_search_str, however, I'm not sure what the proper fix would be

dir01 commented 7 months ago

For now I worked around the issue by configuring the plugin as

    {
        "eddiebergman/nvim-treesitter-pyfold",
        ft = "python",
        config = function()
            vim.opt.runtimepath:append('~/.local/share/nvim')
        end
    },

but now first opened python file takes 4-5 seconds to load (old intel mac)

not sure if it's a side effect of a glob search over ~/.local/share/nvim, or just how long it takes the plugin to do its thing, will investigate further

dir01 commented 7 months ago

Yeah, no, manually changing my local copy of folding.lua to spell local rtp_search_str = 'queries/python/folds.scm' fixes the issue, and the file now opens instantly.

The only fix that comes to mind is to add local lazy_rtp_search_str = 'queries/python/folds.scm' and add a fallback case, but I know very little of nvim plugins and whether there is another, more robust way to solve this, so not going to prepare a pull request.

dir01 commented 7 months ago

My final workaround is to set it up like this:

    {
        "eddiebergman/nvim-treesitter-pyfold",
        ft = "python",
        config = function()
            require('nvim-treesitter-pyfold/folding').cache['fold_path'] = vim.api.nvim_get_runtime_file('queries/python/folds.scm', false)[1]
        end
    },
dir01 commented 7 months ago

Ah, no, scratch that, it didn't work: for some reason, table that I mutate and table that is being used when python file is opened are different tables with different memory addresses 🤷

I guess forking is now the only option I have left

dir01 commented 7 months ago

Checked existing forks and found https://github.com/MPCodeWriter21/nvim-treesitter-pyfold