0

In neovim, I am getting an error on attaching nvim_dap to a running java process. jdtls starts up fine.

I add a breakpoint using \dt then i try to attach to a running java process by pressing \dc i get an error:-

Error executing luv callback:
...config/nvim/autoload/plugged/nvim-dap/lua/dap/session.lua:676: bad argument #2 to 'connect' (number expected, got nil)
stack traceback:
        [C]: in function 'connect'
        ...config/nvim/autoload/plugged/nvim-dap/lua/dap/session.lua:676: in function <...onfig/nvim/autoload/plugged/nvim-dap/lua/dap/session.lua:669>
Press ENTER or type command to continue

Please help resolve this error. Java lsp is working fine but nvim_dap is not working I tried to follow the following links to configure java and dap. https://github.com/mfussenegger/dotfiles/blob/master/vim/.config/nvim/lua/me/dap.lua https://github.com/s1n7ax/youtube-java-debugging/blob/main/init.lua java.lua is as follows:-

local dap = require 'dap'

local M = {}

function M.execute_buf_command(command, callback)
    vim.lsp.buf_request(
        0, 'workspace/executeCommand', command, function(err, _, res)
            if callback then
                callback(err, res)
            elseif err then
                print('Execute command failed: ' .. err.message)
            end
        end)
end

function M.execute_command(command, callback)
    if type(command) == 'string' then command = { command = command } end

    M.execute_buf_command(
        command, function(err, res)
            --assert(not err, err and (err.message or Log.ins(err)))
            callback(res)
        end)
end

--[[
-- Starts the dubug session and returns the port
--
-- @Param callback {function(port: number)}
--]]
function M.start_debug_session(callback)
    M.execute_command('vscode.java.startDebugSession', callback)
end

--[[
-- Returns all the main classes in the project
--
-- @Param callback {function(main_classes: List<List<String>)}
-- { {
--     filePath = "/home/s1n7ax/Workspace/demo/src/main/java/com/example/demo/DemoApplication.java",
--     mainClass = "com.example.demo.DemoApplication",
--     projectName = "demo"
-- } }
--]]
function M.resolve_main_classes(callback)
    M.execute_command('vscode.java.resolveMainClass', callback)
end

--[[
-- Returns classpath for the given main class
--
-- @Param main_class {string} of which classpath should be returned
-- @Param callback {function(classpath: List<List<String>>)}
-- { {},
-- {
--     "/home/s1n7ax/Workspace/demo/bin/main",
--     "/home/s1n7ax/.gradle/.../spring-boot-starter-web/2.5.4/2bef2cedf/spring-boot-starter-web-2.5.4.jar",
-- }
--]]
function M.resolve_class_path(main_class, project_name, callback)
    M.execute_command(
        {
            command = 'vscode.java.resolveClasspath',
            arguments = { main_class, project_name },
        }, callback)
end

--[[
-- Returns list of main class and classpath map
--
-- @Param callback {function(classpaths: List<Map>)}
--]]
function M.resolve_class_paths(callback)
    local classpaths = {}

    local function resolve_all_class_paths(class_iter)
        local class_info = class_iter.next()

        if not class_info then return callback(classpaths) end

        M.resolve_class_path(
            class_info.mainClass, class_info.projectName, function(class_path)
                table.insert(
                    classpaths,
                    { class_info = class_info, class_path = class_path })

                resolve_all_class_paths(class_iter)
            end)
    end

    M.resolve_main_classes(
        function(main_class_info)
            local index = 1

            local main_class_iter = {
                next = function()
                    local temp_index = index
                    index = index + 1
                    return main_class_info[temp_index]
                end,
            }

            resolve_all_class_paths(main_class_iter)
        end)
end

--[[
-- Returns dap java debug configuration
--
-- @Param callback {function(config: Map)}
--]]
function M.get_dap_config(callback)
    M.resolve_class_paths(
        function(class_paths_info)
            local conf = {}

            for index, classpath_info in ipairs(class_paths_info) do
                local main_class = classpath_info.class_info.mainClass
                local project_name = classpath_info.class_info.projectName
                local class_paths = classpath_info.class_path

                table.insert(
                    conf, {
                        name = string.format(
                            '(%d) Launch -> %s -> %s', index, project_name,
                            main_class),
                        projectName = project_name,
                        mainClass = main_class,
                        classPaths = vim.tbl_flatten(class_paths),
                        modulePaths = {},
                        request = 'launch',
                        type = 'java',
                        javaExec = '/usr/bin/java',
                    })
            end

            callback(conf)
        end)
end

function M.setup()
  -- setup_widgets()
  dap.listeners.after.event_initialized['me-keys'] = function()
    api.nvim_set_keymap("n", "<down>", "<cmd>lua require('dap').step_over()<CR>", {noremap = true, silent = true})
    api.nvim_set_keymap("n", "<left>", "<cmd>lua require('dap').step_out()<CR>", {noremap = true, silent = true})
    api.nvim_set_keymap("n", "<right>", "<cmd>lua require('dap').step_into()<CR>", {noremap = true, silent = true})
  end
  dap.listeners.after.event_terminated['me-keys'] = function()
    api.nvim_del_keymap("n", "<down>")
    api.nvim_del_keymap("n", "<left>")
    api.nvim_del_keymap("n", "<right>")
  end

  dap.defaults.fallback.terminal_win_cmd = 'tabnew'

  dap.configurations.java = {
    {
      type = 'java';
      request = 'attach';
      name = "Debug (Attach) - Remote";
      hostName = "127.0.0.1";
      port = 5005;
    },
  }
  require('dap.ext.vscode').load_launchjs()
end

M.setup()

dap.adapters.java = function(callback)
   M.start_debug_session(
       function(port)
           callback({ type = 'server', host = '127.0.0.1', port = port })
       end)
end


local on_attach = function(client, bufnr)
    if client.name == 'java' then
        M.get_dap_config(
            function(conf)
                dap.configurations.java = conf
                print('Debugger is ready')
            end)
    end

    local function buf_set_keymap(...)
        vim.api.nvim_buf_set_keymap(bufnr, ...)
    end
    local function buf_set_option(...)
        vim.api.nvim_buf_set_option(bufnr, ...)
    end

    -- Enable completion triggered by <c-x><c-o>
    buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')

    -- Mappings.
    local opts = { noremap = true, silent = true }

    -- See `:help vim.lsp.*` for documentation on any of the below functions
    buf_set_keymap('n', 'gD', '<cmd>lua vim.lsp.buf.declaration()<CR>', opts)
    buf_set_keymap('n', 'gd', '<cmd>lua vim.lsp.buf.definition()<CR>', opts)
    buf_set_keymap('n', 'K', '<cmd>lua vim.lsp.buf.hover()<CR>', opts)
    buf_set_keymap('n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>', opts)
    buf_set_keymap(
        'n', '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts)
    buf_set_keymap(
        'n', '<space>wa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>',
        opts)
    buf_set_keymap(
        'n', '<space>wr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>',
        opts)
    buf_set_keymap(
        'n', '<space>wl',
        '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>',
        opts)
    buf_set_keymap(
        'n', '<space>D', '<cmd>lua vim.lsp.buf.type_definition()<CR>', opts)
    buf_set_keymap('n', '<space>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', opts)
    buf_set_keymap(
        'n', '<space>ca', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts)
    buf_set_keymap('n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>', opts)
    buf_set_keymap(
        'n', '<space>e',
        '<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>', opts)
    buf_set_keymap(
        'n', '[d', '<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>', opts)
    buf_set_keymap(
        'n', ']d', '<cmd>lua vim.lsp.diagnostic.goto_next()<CR>', opts)
    buf_set_keymap(
        'n', '<space>q', '<cmd>lua vim.lsp.diagnostic.set_loclist()<CR>', opts)
    buf_set_keymap(
        'n', '<space>f', '<cmd>lua vim.lsp.buf.formatting()<CR>', opts)
end


local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = require('cmp_nvim_lsp').update_capabilities(capabilities)

-- If you started neovim within `~/dev/xy/project-1` this would resolve to `project-1`
local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ':p:h:t')

local workspace_dir = '/Users/amansingh/.workspace/' .. project_name

local config = {
  cmd = {'sh','jdtls_launch', workspace_dir},
  root_dir = require('jdtls.setup').find_root({'.git','mvnw','gradlew'}),
  capabilities = capabilities,
  on_attach = on_attach
}
config['init_options'] = {
  bundles = {
    vim.fn.glob(
          '/Users/amansingh/' ..
          'development/com.microsoft.java.debug.plugin-0.35.0.jar'),
    },
  }

require('jdtls').start_or_attach(config)

vim.api.nvim_exec(
    [[
    nnoremap <silent> <leader>dc :lua require'dap'.continue()<CR>
    nnoremap <silent> <F10> :lua require'dap'.step_over()<CR>
    nnoremap <silent> <F11> :lua require'dap'.step_into()<CR>
    nnoremap <silent> <F12> :lua require'dap'.step_out()<CR>
    nnoremap <silent> <leader>dt :lua require'dap'.toggle_breakpoint()<CR>
    nnoremap <silent> <leader>B :lua require'dap'.set_breakpoint(vim.fn.input('Breakpoint condition: '))<CR>
    nnoremap <silent> <leader>lp :lua require'dap'.set_breakpoint(nil, nil, vim.fn.input('Log point message: '))<CR>
    nnoremap <silent> <leader>dr :lua require'dap'.repl.open()<CR>
    nnoremap <silent> <leader>dl :lua require'dap'.run_last()<CR>
    ]], false)

Aman Bains
  • 153
  • 9

1 Answers1

0

You should follow the instructions in https://github.com/mfussenegger/nvim-dap/wiki/Java

I recommend using nvim-jdtls to set it up and closely following its readme

mfussenegger
  • 3,931
  • 23
  • 18
  • Hi, I could not get it to work. that was why i was looking for help. – Aman Bains Apr 06 '22 at 08:43
  • Then it would be good to post the error and logs of what happened when you followed those instructions instead of trying to do things in a different undocumented way and then trying to get help for that. – mfussenegger Apr 11 '22 at 16:43