R-nvim / cmp-r

Completion source for nvim-cmp using R.nvim as backend.
GNU General Public License v3.0
19 stars 1 forks source link

Error executing lua [string ":lua"]:1: module 'cmp' not found #4

Closed maitra closed 8 months ago

maitra commented 8 months ago

I have the following in my .config/nvim/init.vim file:

call plug#begin('~/.vim/plugged')

Plug 'bluz71/vim-nightfly-colors'
Plug 'ryanoasis/vim-devicons'
Plug 'tpope/vim-rsi'
Plug 'mhartington/formatter.nvim'
Plug 'nvim-treesitter/nvim-treesitter', {'do': ':TSUpdate'}

if has('nvim')
  Plug 'Shougo/deoplete.nvim', { 'do': ':UpdateRemotePlugins' }
else
  Plug 'Shougo/deoplete.nvim'
  Plug 'roxma/nvim-yarp'
  Plug 'roxma/vim-hug-neovim-rpc'
endif
let g:deoplete#enable_at_startup = 1

Plug 'lervag/vimtex'
" Track the engine.
Plug 'SirVer/ultisnips'

" Snippets are separated from the engine. Add this if you want them:
Plug 'honza/vim-snippets'

Plug 'hrsh7th/nvim-cmp'
Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'R-nvim/R.nvim'
Plug 'R-nvim/cmp-r'

:lua << EOF
require'cmp'.setup {
  sources = {
    { name = 'cmp_r' },
  }
}
EOF

call plug#end()

However, when I load a file (say code.R), I get the following errors:

E5108: Error executing lua [string ":lua"]:1: module 'cmp' not found:
        no field package.preload['cmp']
        no file './cmp.lua'
        no file '/usr/share/luajit-2.1/cmp.lua'
        no file '/usr/local/share/lua/5.1/cmp.lua'
        no file '/usr/local/share/lua/5.1/cmp/init.lua'
        no file '/usr/share/lua/5.1/cmp.lua'
        no file '/usr/share/lua/5.1/cmp/init.lua'
        no file './cmp.so'
        no file '/usr/local/lib/lua/5.1/cmp.so'
        no file '/usr/lib64/lua/5.1/cmp.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
        [C]: in function 'require'
        [string ":lua"]:1: in main chunk
Error detected while processing BufNewFile Autocommands for "*":
Error executing lua callback: /usr/share/nvim/runtime/filetype.lua:24: Error executing lua: /usr/share/nvim/runtime/filetype.lua:25: BufNewFile Autocommands for "*"..FileType Autocommands for "*"..function <SNR>1_LoadFTPlugin[19]..script /home/maitra/.vim/plugged/R.nvim/ftplugin/r_rnvim.lua: Vim(runtime):E5113: Error while calling lua chunk: /usr/share/nvim/runtime/lua/vim/treesitter/language.lua:93: no parser for 'r' language, see :help treesitter-parsers 
stack traceback:
        [C]: in function 'error'
        /usr/share/nvim/runtime/lua/vim/treesitter/language.lua:93: in function 'add'
        /usr/share/nvim/runtime/lua/vim/treesitter/query.lua:252: in function 'parse'
        /home/maitra/.vim/plugged/R.nvim/lua/r/send.lua:642: in main chunk
        [C]: in function 'require'
        /home/maitra/.vim/plugged/R.nvim/lua/r/run.lua:7: in main chunk
        [C]: in function 'require'
        /home/maitra/.vim/plugged/R.nvim/lua/r/config.lua:739: in function 'global_setup'
        /home/maitra/.vim/plugged/R.nvim/lua/r/config.lua:803: in function 'real_setup'
        /home/maitra/.vim/plugged/R.nvim/ftplugin/r_rnvim.lua:12: in main chunk
        [C]: in function 'nvim_cmd'
        /usr/share/nvim/runtime/filetype.lua:25: in function </usr/share/nvim/runtime/filetype.lua:24>
        [C]: in function 'nvim_buf_call'
        /usr/share/nvim/runtime/filetype.lua:24: in function </usr/share/nvim/runtime/filetype.lua:10>
stack traceback:
        [C]: in function 'nvim_buf_call'
        /usr/share/nvim/runtime/filetype.lua:24: in function </usr/share/nvim/runtime/filetype.lua:10>

What is causing the error? How do I get around this? I am on Fedora 39 and neovim 0.9.5. Thanks!

jalvesaq commented 8 months ago

Your code seems to be correct, but lazy.nvim calls the setup() function of plugins automatically. You could try to call the setup of R.nvim and cmp-r manually:

:lua << EOF
require("cmp").setup({ sources = {{ name = "cmp_r" }}})
require("cmp_r").setup()
require("r").setup()
EOF
maitra commented 8 months ago

Sorry, but the errors are still there. Pretty much the same errors:


Error detected while processing /home/maitra/.config/nvim/init.vim[5]:
line   45:
E5108: Error executing lua [string ":lua"]:1: module 'cmp' not found:
        no field package.preload['cmp']
        no file './cmp.lua'
        no file '/usr/share/luajit-2.1/cmp.lua'
        no file '/usr/local/share/lua/5.1/cmp.lua'
        no file '/usr/local/share/lua/5.1/cmp/init.lua'
        no file '/usr/share/lua/5.1/cmp.lua'
        no file '/usr/share/lua/5.1/cmp/init.lua'
        no file './cmp.so'
        no file '/usr/local/lib/lua/5.1/cmp.so'
        no file '/usr/lib64/lua/5.1/cmp.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
        [C]: in function 'require'
        [string ":lua"]:1: in main chunk
Error detected while processing BufNewFile Autocommands for "*":
Error executing lua callback: /usr/share/nvim/runtime/filetype.lua:24: Error executing lua: /usr/share/nvim/runtime/filetype.lua:25: BufNewFile Autocommands for "*"..FileType Autocommands for "*"..function <SNR>1_LoadFTPlugin[19]..script /home/maitra/.vim/plugged/R.nvim/ftplugin/r_rnvim.lua: Vim(runtime):E5113: Error while calling lua chunk: /usr/share/nvim/runtime/lua/vim/treesitter/language.lua:93: no parser for 'r' language, see :help treesitter-parsers
stack traceback:
        [C]: in function 'error'
        /usr/share/nvim/runtime/lua/vim/treesitter/language.lua:93: in function 'add'
        /usr/share/nvim/runtime/lua/vim/treesitter/query.lua:252: in function 'parse'
        /home/maitra/.vim/plugged/R.nvim/lua/r/send.lua:642: in main chunk
        [C]: in function 'require'
        /home/maitra/.vim/plugged/R.nvim/lua/r/run.lua:7: in main chunk
        [C]: in function 'require'
        /home/maitra/.vim/plugged/R.nvim/lua/r/config.lua:739: in function 'global_setup'
        /home/maitra/.vim/plugged/R.nvim/lua/r/config.lua:803: in function 'real_setup'
        /home/maitra/.vim/plugged/R.nvim/ftplugin/r_rnvim.lua:12: in main chunk
        [C]: in function 'nvim_cmd'
        /usr/share/nvim/runtime/filetype.lua:25: in function </usr/share/nvim/runtime/filetype.lua:24>
        [C]: in function 'nvim_buf_call'
        /usr/share/nvim/runtime/filetype.lua:24: in function </usr/share/nvim/runtime/filetype.lua:10>
stack traceback:
        [C]: in function 'nvim_cmd'
        /usr/share/nvim/runtime/filetype.lua:25: in function </usr/share/nvim/runtime/filetype.lua:24>
        [C]: in function 'nvim_buf_call'
        /usr/share/nvim/runtime/filetype.lua:24: in function </usr/share/nvim/runtime/filetype.lua:10>
stack traceback:
        [C]: in function 'nvim_buf_call'
        /usr/share/nvim/runtime/filetype.lua:24: in function </usr/share/nvim/runtime/filetype.lua:10>
jalvesaq commented 8 months ago

Maybe the chunk :lua << EOF ... EOF should be after call plug#end().

I updated the "minimal" example of https://github.com/R-nvim/R.nvim/wiki/Configuration

Then, trying to adapt it to your init.vim:

:lua << EOF
require("nvim-treesitter.configs").setup({
  sync_install = true,
  ensure_installed = {
    "r",
    "markdown",
    "markdown_inline",
    "rnoweb",
  },
})

local cmp = require("cmp")
cmp.setup({
  sources = {{ name = "cmp_r" }},
  mapping = cmp.mapping.preset.insert({
    ['<CR>'] = cmp.mapping.confirm({ select = false }),
    -- During auto-completion, press <Tab> to select the next item.
    ['<Tab>'] = cmp.mapping(function(fallback)
      if cmp.visible() then
        cmp.select_next_item({ behavior = cmp.SelectBehavior.Insert })
      elseif has_words_before() then
        cmp.complete()
      else
        fallback()
      end
    end, { 'i', 's' }),
    ['<S-Tab>'] = cmp.mapping(function(fallback)
      if cmp.visible() then
        cmp.select_prev_item({ behavior = cmp.SelectBehavior.Insert })
      else
        fallback()
      end
    end, { 'i', 's' }),
  }),
})

require("cmp_r").setup({ })
EOF
maitra commented 8 months ago

Thank you for looking into this! I now get a different error:


Error executing vim.schedule lua callback: /home/maitra/.vim/plugged/R.nvim/lua/r/server.lua:73: att
empt to concatenate upvalue 'libd' (a nil value)
stack traceback:
        /home/maitra/.vim/plugged/R.nvim/lua/r/server.lua:73: in function </home/maitra/.vim/plugged
/R.nvim/lua/r/server.lua:71>
jalvesaq commented 8 months ago

This is a bug in R.nvim in a part of the code that has not been tested yet. Did you install R and don't have any package installed yet? R.nvim is trying to create the directory where R packages will be installed. If you open R in the terminal and install any package manually, you should avoid this error.

maitra commented 8 months ago

Thanks! My R is installed on my Fedora 39 system. Packages have all been installed earlier. I just did nvim code.R (where code.R was a new file. How do I get around this error while editing R's script? Thanks very much again!

(Also R works as it should from the terminal.)

jalvesaq commented 8 months ago

I'm trying to fix it...

jalvesaq commented 8 months ago

Is the directory where nvimcom is supposed to be installed writable?

maitra commented 8 months ago

So, my plugin installations are in ~/vimfiles/plugged and that is writable. Am I answering your question here? Thanks!

jalvesaq commented 8 months ago

No. The directory where nvimcom is going to be installed is something like ~/R/x86_64-pc-linux-gnu-library/4.3 or, more precisely, the output of Sys.getenv("R_LIBS_USER").

maitra commented 8 months ago

Oh, I see, sorry. Yes, it is writable: however, I usually install systemwide using sudo R when needed (but that is not relevant to your question).

> Sys.getenv("R_LIBS_USER")
[1] "/home/maitra/R/x86_64-redhat-linux-gnu-library/4.3"

Is there a way to skip this local installation step?

maitra commented 8 months ago

I should add that the directory /home/maitra/R/ exists and is writable, however, there is nothing in there. Perhaps that is an issue?

jalvesaq commented 8 months ago

It's incomplete. The "x86.../4.3" is missing

maitra commented 8 months ago

It's incomplete. The "x86.../4.3" is missing

I see. So, I create the directory, and the error goes away! However, the assignment does not work.

x_2 does not switch to x <- 2 in the edit/insert mode of nvim.

Also, can I install nvimcom systemwide using devtools and use that? I don't really want or need to have local R packages installed.

jalvesaq commented 8 months ago

I have to go out now...

Did you set assign_map?

maitra commented 8 months ago

I have to go out now... Did you set assign_map?

No worries, thanks for all your help! Oh, is that not set by default?

jalvesaq commented 8 months ago

The default has changed.

jalvesaq commented 8 months ago

Where you have:

require("cmp_r").setup({ })

you need:

require("cmp_r").setup({ })
require("r").setup({ assign_map = "_" })
jalvesaq commented 8 months ago

I see. So, I create the directory, and the error goes away!

I have fixed the bug that was preventing R.nvim from creating the directory. Could you, please, manually delete the directory ~/R/x86_64-redhat-linux-gnu-library/4.3 to confirm that R.nvim now succeeds when trying to create it?

maitra commented 8 months ago

I see. So, I create the directory, and the error goes away!

I have fixed the bug that was preventing R.nvim from creating the directory. Could you, please, manually delete the directory ~/R/x86_64-redhat-linux-gnu-library/4.3 to confirm that R.nvim now succeeds when trying to create it?

Yes, this works! Btw, is it possible to install nvimcom systemwide (and not have this directory at all), or is it needed for nvim-R to function?

jalvesaq commented 8 months ago

Thanks for testing it!

Btw, is it possible to install nvimcom systemwide

I have never installed it systemwide, but, yes: https://github.com/jalvesaq/Nvim-R/issues/735

maitra commented 8 months ago

Where you have:

require("cmp_r").setup({ })

you need:

require("cmp_r").setup({ })
require("r").setup({ assign_map = "_" })

So, I tried this, and I was expecting that when I did

x_5 then it would automatically become x <- 5 in my R source code as happens in the case of ESS. However, that is not happening and it says x_5 in my R script.

maitra commented 8 months ago

Btw, I still have the error when I hit 'tab' in my R script file.

E5108: Error executing lua: [string ":lua"]:20: attempt to call global 'has_words_before' (a nil val
ue)
stack traceback:
        [string ":lua"]:20: in function 'on_keymap'
        /home/maitra/.vim/plugged/nvim-cmp/lua/cmp/core.lua:145: in function 'callback'
        /home/maitra/.vim/plugged/nvim-cmp/lua/cmp/utils/keymap.lua:133: in function </home/maitra/.
vim/plugged/nvim-cmp/lua/cmp/utils/keymap.lua:127>

Where does this come from?