moduleluarequireneovimneovim-plugin

Nvim lua cant require lua module with hyphens


So I am trying to create a plugin for nvim. The plugin gets the path and name of a certain module passed into a loader function. Now this works for modules where the module name does not have a hyphen. But when there is a hyphen i cant require the module.

My Code looks like this:

local function loadmodule(modulePath, moduleName)
    vim.opt.rtp:append(modulePath)

    package.loaded[moduleName] = nil
    require(moduleName)
end

loadmodule("path/to/module", "module-name")

When i execute this code on a module without hyphens no errors show up but when the modules name has hyphens in it i get the error:

E5108: Error executing lua ./lua/script.lua:5: module 'module-name' not found:                                                                                                                                                                          
        no field package.preload['module-name']                                                                                                                                                                                                                      
cache_loader: module module-name not found                                                                                                                                                                                                                           
cache_loader_lib: module module-name not found                                                                                                                                                                                                                       
        no file './module-name.lua'                                                                                                                                                                                                                                  
        no file '/usr/share/luajit-2.1/module-name.lua'                                                                                                                                                                                                              
        no file '/usr/local/share/lua/5.1/module-name.lua'                                                                                                                                                                                                           
        no file '/usr/local/share/lua/5.1/module-name/init.lua'                                                                                                                                                                                                      
        no file '/usr/share/lua/5.1/module-name.lua'                                                                                                                                                                                                                 
        no file '/usr/share/lua/5.1/module-name/init.lua'                                                                                                                                                                                                            
        no file './module-name.so'                                                                                                                                                                                                                                   
        no file '/usr/local/lib/lua/5.1/module-name.so'                                                                                                                                                                                                              
        no file '/usr/lib/lua/5.1/module-name.so'                                                                                                                                                                                                                    
        no file '/usr/local/lib/lua/5.1/loadall.so'                                                                                                                                                                                                                          
        no file '/home/user/.config/nvim//lua/lib/lua/5.1/module-name.so'     

obviously I am not just plugging module-name in and the module does exist on the runtimepath but nvim doesn't seem to be able to find it.

I tried plugging in the modules name and it loading the module. What happened was, nvim loading the module when it doesn't have hyphens and not loading the module when it has hyphens.


Solution

  • If anybody runs into this problem in the future what I did to solve this was to source the file using nvim's vim.cmd functionality. So what I did was:

    local function loadmodule(modulePath, moduleName)
        vim.opt.rtp:append(modulePath)
    
        vim.cmd("source " .. modulePath .. "/" .. moduleName)
    end
    
    loadmodule("path/to/module", "module-name")
    

    this works for .vim and .lua files.