mfussenegger / nvim-jdtls

Extensions for the built-in LSP support in Neovim for eclipse.jdt.ls
GNU General Public License v3.0
1.1k stars 62 forks source link

module 'jdtls' not found in after/ftplugin/java.lua #180

Closed c3n21 closed 2 years ago

c3n21 commented 2 years ago

LSP client configuration

local coq = require "coq" -- add this
local jdtls = require "jdtls"

if jdtls ~= nil then
    local config = coq.lsp_ensure_capabilities({
        cmd = {
            '/usr/bin/jdtls',
            os.getenv('HOME') .. '/workspace/' .. vim.fn.fnamemodify(vim.fn.getcwd(),':p:h:t')
        },
        root_dir = jdtls.setup.find_root({'gradle.build', 'pom.xml', ".git"})
    })

    jdtls.start_or_attach(config)
end

Eclipse.jdt.ls version

1.5.0

Steps to Reproduce

EDIT:

There should be no error?

Actual Result

` Error detected while processing /home/zhifan/.config/nvim/after/ftplugin/java.lua:

E5113: Error while calling lua chunk: /home/zhifan/.config/nvim/after/ftplugin/java.lua:2: module 'jdtls' not found:

    no field package.preload['jdtls']

    no file './jdtls.lua'

    no file '/home/runner/work/neovim/neovim/.deps/usr/share/luajit-2.1.0-beta3/jdtls.lua'

    no file '/usr/local/share/lua/5.1/jdtls.lua'

    no file '/usr/local/share/lua/5.1/jdtls/init.lua'

    no file '/home/runner/work/neovim/neovim/.deps/usr/share/lua/5.1/jdtls.lua'

    no file '/home/runner/work/neovim/neovim/.deps/usr/share/lua/5.1/jdtls/init.lua'

    no file '/home/zhifan/.cache/nvim/packer_hererocks/2.1.0-beta3/share/lua/5.1/jdtls.lua'

    no file '/home/zhifan/.cache/nvim/packer_hererocks/2.1.0-beta3/share/lua/5.1/jdtls/init.lua'

    no file '/home/zhifan/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/luarocks/rocks-5.1/jdtls.lua'

    no file '/home/zhifan/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/luarocks/rocks-5.1/jdtls/init.lua'

    no file './jdtls.so'

    no file '/usr/local/lib/lua/5.1/jdtls.so'

    no file '/home/runner/work/neovim/neovim/.deps/usr/lib/lua/5.1/jdtls.so'

    no file '/usr/local/lib/lua/5.1/loadall.so'

    no file '/home/zhifan/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/lua/5.1/jdtls.so'

stack traceback:

    [C]: in function 'require'

    /home/zhifan/.config/nvim/after/ftplugin/java.lua:2: in main chunk

`

This happens only the first time I open a java file, after that subsequent java file that I open doesn't have any issue.

Also, the jdtls attaches properly to each buffer.

c3n21 commented 2 years ago

Apparently it was a problem because in packer.nvim I had ft set

        use {
            'mfussenegger/nvim-jdtls',
            disable = false,
            ft = "java"
        }

but still I don't get why it would give me such an error

mfussenegger commented 2 years ago

Maybe an ordering issue? I assume packer loads the package lazy if you set ft = 'java' and the ftplugin/java.lua file is maybe executed before packer can lazy load it.

If you want to get lazy loading, you could try with disable = true and then add a vim.cmd('packadd nvim-jdtls') or something like that to your ftplugin/java.lua. (I'm not using packer, but I think it still uses the vim packages mechanism)


local jdtls = require "jdtls"

if jdtls ~= nil then

This will afaik always fail if jdtls is not available. If you want to check if it is available without failing, you'd have to use something like this:

local ok, jdtls = require('jdtls')
if ok then
  ...
end
c3n21 commented 2 years ago

Maybe an ordering issue? I assume packer loads the package lazy if you set ft = 'java' and the ftplugin/java.lua file is maybe executed before packer can lazy load it.

If you want to get lazy loading, you could try with disable = true and then add a vim.cmd('packadd nvim-jdtls') or something like that to your ftplugin/java.lua. (I'm not using packer, but I think it still uses the vim packages mechanism)


local jdtls = require "jdtls"

if jdtls ~= nil then

This will afaik always fail if jdtls is not available. If you want to check if it is available without failing, you'd have to use something like this:

local ok, jdtls = require('jdtls')
if ok then
  ...
end

What I would like to achieve is using packer.nvim as a centralized plugin manager so that I can able and disable plugins without so much hassle.

Ig that using after/ftplugin/java.lua could give me some errors if in the future I want to temporarily disable it

fitrh commented 2 years ago

@c3n21 how do you use the packer use specification ? I guess you didn't set the module key to jdtls.

I have tested your approach with below configuration

-- packer plugin declaration
use({
  "mfussenegger/nvim-jdtls",
  config = function()
    -- https://github.com/fitrh/init.nvim/blob/main/lua/plugin/jdtls/config.lua
    require("plugin.jdtls.config").attach()
  end,
  module = "jdtls",
})
-- after/ftplugin/java.lua
require("jdtls")
c3n21 commented 2 years ago

@c3n21 how do you use the packer use specification ? I guess you didn't set the module key to jdtls.

I have tested your approach with below configuration

-- packer plugin declaration
use({
  "mfussenegger/nvim-jdtls",
  config = function()
    -- https://github.com/fitrh/init.nvim/blob/main/lua/plugin/jdtls/config.lua
    require("plugin.jdtls.config").attach()
  end,
  module = "jdtls",
})
-- after/ftplugin/java.lua
require("jdtls")

That's interesting, I missed out that you could set "module" in the config. But a problem still persists: if I want to remove nvim-jdtls, I would also need to comment out after/ftplugin/java.lua

c3n21 commented 2 years ago

In the end I've managed to work it around with this

-- inside lua/config/nvim-jdtls/init.lua
if packer_plugins["nvim-jdtls"] then
    local coq = require "coq" -- add this (my completion plugin)
    local jdtls = require "jdtls"

    local workspace_root = os.getenv('HOME') .. '/.config/nvim-jdtls/workspace/' .. vim.fn.fnamemodify(vim.fn.getcwd(),':p:h:t')

    local config = coq.lsp_ensure_capabilities({
        cmd = {
            'jdtls.sh', workspace_root,
        },
        root_dir = jdtls.setup.find_root({'gradle.build', 'pom.xml', ".git"})
    })

    jdtls.start_or_attach(config)
else
    print("nvim-jdtls not installed")
end

so that if the plugin is not installed it will just print a message and keep after/ftplugin/java.lua always there even when I have nvim-jdtls uninstalled.