rcarriga / nvim-dap-ui

A UI for nvim-dap
MIT License
2.67k stars 99 forks source link

Failed to close windows automatically by using nvim-dap events. #147

Open xbot opened 2 years ago

xbot commented 2 years ago

I configured vimrc according to the README.md, the windows show up when dap.continue() is executed, which is as expected.

But when dap.terminate() is executed, the windows do not close automatically.

The full configuration is as follows:

    augroup nvim_dap_mappings
        au!
        autocmd FileType lua,php,python nnoremap <silent> <buffer> <Leader>dbb <Cmd>lua require('dap').toggle_breakpoint()<CR>
        autocmd FileType lua,php,python nnoremap <silent> <buffer> <Leader>dbB <Cmd>lua require('dap').set_breakpoint(vim.fn.input('[Condition] > '))<CR>
        autocmd FileType lua,php,python nnoremap <silent> <buffer> <Leader>dbr <Cmd>lua require('dap').continue()<CR>
    augroup END

lua << EOF

-- https://github.com/xdebug/vscode-php-debug/releases
-- Extract the vsix content
local dap, dapui = require('dap'), require('dapui')

function dap_keybind(dap_action, key)
    if require('dap').session() then
        dap_action()
    else
        vim.cmd('normal! ' .. key)
    end
end

local map = require('user.utils').map
map('n', '<F3>',                 function () dap_keybind(dap.terminate, '<F3>') end)
map('n', '<F4>',                 function () dap_keybind(dap.run_last, '<F4>') end)
map('n', '<F5>',                 function () dap_keybind(dap.continue, '<F5>') end)
map('n', '<F8>',                 function () dap_keybind(dap.run_to_cursor, '<F8>') end)
map('n', '<F10>',                function () dap_keybind(dap.step_over, '<F10>') end)
map('n', '<F11>',                function () dap_keybind(dap.step_into, '<F11>') end)
map('n', '<F12>',                function () dap_keybind(dap.step_out, '<F12>') end)
map('n', '<Leader>bp',           function () dap_keybind(dap.list_breakpoints, '<Leader>bp') end)
map('n', '<Leader><Leader><F3>', function () dap_keybind(dap.close, '<Leader><Leader><F3>') end)
map('n', '<Leader>die',          function () dap_keybind(function() dapui.eval(vim.fn.input('[Expression] > ')) end, '<Leader>die') end)
map('n', '<Leader>diw',          function () dap_keybind(require'dap.ui.widgets'.hover, '<Leader>diw') end)
map('x', '<Leader>di',           function () dap_keybind(dapui.eval, '<Leader>di') end)
map('n', '<Leader>dwe',          function () dap_keybind(function() print('Adding watches not implemented yet.') end, '<Leader>dwe') end)

-- PHP debug settings
dap.configurations.php = {
    {
        type = 'php',
        request = 'launch',
        name = 'local',
        port = '9001',
        log = true,
    },
    {
        type = 'php',
        request = 'launch',
        name = 'docker',
        port = '9003',
        log = true,
        serverSourceRoot = '/app',
        localSourceRoot = "${workspaceFolder}",
    },
}
dap.adapters.php = {
    type = 'executable',
    command = 'node',
    args = {os.getenv('HOME') .. "/Projects/3rd-party/vscode-php-debug/out/phpDebug.js"},
}

-- Lua debug settings
dap.configurations.lua = {
    {
        type = 'nlua',
        request = 'attach',
        name = "Attach to running Neovim instance",
        host = function() return vim.fn.input('Remote IP: ', '127.0.0.1') end,
        port = function()
            local val = tonumber(vim.fn.input('Port: ', 4444))
            assert(val, "Please provide a port number")
            return val
        end,
    }
}
dap.adapters.nlua = function(callback, config)
    callback({ type = 'server', host = config.host, port = config.port })
end

-- Python debug settings
dap.configurations.python = {
    {
        type = 'remote_python',
        request = 'attach',
        name = "Attach to a running python program",
        host = function() return vim.fn.input('Remote IP: ', '127.0.0.1') end,
        port = function()
            local val = tonumber(vim.fn.input('Port: ', 4444))
            assert(val, "Please provide a port number")
            return val
        end,
    },
}
dap.adapters.remote_python = function(callback, config)
    callback({ type = 'server', host = config.host, port = config.port; })
end

dapui.setup()

dap.listeners.after.event_initialized["dapui_config"] = function()
    dapui.open()
end
dap.listeners.before.event_terminated["dapui_config"] = function()
    dapui.close()
end
dap.listeners.before.event_exited["dapui_config"] = function()
    dapui.close()
end

require("nvim-dap-virtual-text").setup()
EOF
rcarriga commented 2 years ago

Can you provide a debug log for nvim-dap? I believe this is because the debugger is not emitting the required events

danrot commented 2 years ago

I have the exact same issue... If I use my configure shortcut to terminate the debugging session the debuggee process is really terminated, but the windows do not disappear... The important parts of the configuration look like this:

local dap = require('dap')

dap.adapters.php = {
    type = 'executable',
    command = 'node',
    args = { vim.fn.stdpath('data') .. '/mason/packages/php-debug-adapter/extension/out/phpDebug.js' },
}

dap.configurations.php = {
    {
        type = 'php',
        request = 'launch',
        name = 'Listen for Xdebug',
        port = os.getenv('NVIM_XDEBUG_PORT'),
        pathMappings = {
            [os.getenv('NVIM_XDEBUG_PATH_SERVER')] = os.getenv('NVIM_XDEBUG_PATH_LOCAL'),
        },
    },
}

vim.keymap.set('n', '<F2>', dap.step_over)
vim.keymap.set('n', '<F3>', dap.step_into)
vim.keymap.set('n', '<F4>', dap.step_out)
vim.keymap.set('n', '<F5>', dap.continue)
vim.keymap.set('n', '<F6>', dap.terminate)
vim.keymap.set('n', '<F10>', dap.toggle_breakpoint)

local dapui = require('dapui')
dapui.setup()

dap.listeners.after.event_initialized["dapui_config"] = function()
    dapui.open()
end

dap.listeners.before.event_terminated["dapui_config"] = function()
    dapui.close()
end

dap.listeners.before.event_exited["dapui_config"] = function()
    dapui.close()
end

The first listener to open the UI works, but when I press F6 to terminate the session the UI windows remain...

I've checked the log, but it seems like today there was no error or anything else indicating a malfunction of nvim-dap:

[ INFO ] 2022-10-31T08:43:14Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1465 ] "Session closed due to disconnect"
[ INFO ] 2022-10-31T08:43:14Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1128 ] "Closed all handles"
[ INFO ] 2022-10-31T08:43:14Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1131 ] "Process closed"    59467   false

[ INFO ] 2022-10-31T08:45:21Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1465 ] "Session closed due to disconnect"
[ INFO ] 2022-10-31T08:45:21Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1128 ] "Closed all handles"
[ INFO ] 2022-10-31T08:45:21Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1131 ] "Process closed"    62681   false

[ INFO ] 2022-10-31T08:46:09Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1465 ] "Session closed due to disconnect"
[ INFO ] 2022-10-31T08:46:09Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1128 ] "Closed all handles"
[ INFO ] 2022-10-31T08:46:09Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1131 ] "Process closed"    62973   false

[ INFO ] 2022-10-31T08:49:51Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1465 ] "Session closed due to disconnect"
[ INFO ] 2022-10-31T08:49:51Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1128 ] "Closed all handles"
[ INFO ] 2022-10-31T08:49:51Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1131 ] "Process closed"    63154   false
[ INFO ] 2022-10-31T08:49:58Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1465 ] "Session closed due to disconnect"
[ INFO ] 2022-10-31T08:49:58Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1128 ] "Closed all handles"
[ INFO ] 2022-10-31T08:49:58Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1131 ] "Process closed"    63164   false
[ INFO ] 2022-10-31T08:50:22Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1465 ] "Session closed due to disconnect"
[ INFO ] 2022-10-31T08:50:22Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1128 ] "Closed all handles"
[ INFO ] 2022-10-31T08:50:22Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1131 ] "Process closed"    63173   false
[ INFO ] 2022-10-31T08:51:38Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1465 ] "Session closed due to disconnect"
[ INFO ] 2022-10-31T08:51:38Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1128 ] "Closed all handles"
[ INFO ] 2022-10-31T08:51:38Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1131 ] "Process closed"    63198   false
[ INFO ] 2022-10-31T08:53:43Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1465 ] "Session closed due to disconnect"
[ INFO ] 2022-10-31T08:53:43Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1128 ] "Closed all handles"
[ INFO ] 2022-10-31T08:53:43Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1131 ] "Process closed"    63382   false
[ INFO ] 2022-10-31T08:53:57Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1465 ] "Session closed due to disconnect"
[ INFO ] 2022-10-31T08:53:57Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1128 ] "Closed all handles"
[ INFO ] 2022-10-31T08:53:57Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1131 ] "Process closed"    63406   false

[ INFO ] 2022-10-31T09:17:10Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1465 ] "Session closed due to disconnect"
[ INFO ] 2022-10-31T09:17:10Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1128 ] "Closed all handles"
[ INFO ] 2022-10-31T09:17:10Z+0100 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:1131 ] "Process closed"    63991   false
christianbader commented 2 years ago

same here, the debugger terminates but the events (dap.listeners.before.event_terminated, dap.listeners.before.event_exited) are never fired. The dap.listeners.after.event_initialized event works as expected.

rcarriga commented 2 years ago

Can you add a listener for disconnect to see if that works?

dap.listeners.before.disconnect["dapui_config"] = function() dapui.close() end
danrot commented 2 years ago

This works for me, but only if the session is terminated using dap.terminate. If the program finishes normally the ui stays, but that's already good enough for me.

rcarriga commented 2 years ago

Is that only when using disconnect or using event_terminated and event_exited as well?

danrot commented 2 years ago

I am only talking about disconnect, the other two event did not work.

GKSN commented 1 year ago

same here, the debugger terminates but the events (dap.listeners.before.event_terminated, dap.listeners.before.event_exited) are never fired. The dap.listeners.after.event_initialized event works as expected.

Same here. When using disconnect is working but with event_terminated and event_exited don't work.

medwatt commented 1 year ago

I am experiencing a similar issue.

  1. I expect that when the script exits, the dap-repl buffer closes (it does not).
  2. For some reason, if I switch to the dap-repl buffer and back to the script I'm debugging, the debugging menu panel, moves to the buffer containing the script (only a restart seems to resolve it).

Here's a video showcasing points 1 and 2.

https://user-images.githubusercontent.com/17733465/209577097-f93f9958-5511-4eec-a176-e2891b3de1e6.mp4

rcarriga commented 1 year ago

I expect that when the script exits, the dap-repl buffer closes (it does not).

The window is closing but the buffer is still exists and so is listed. This is expected, otherwise all REPL state would be erased when closing the window.

For some reason, if I switch to the dap-repl buffer and back to the script I'm debugging, the debugging menu panel, moves to the buffer containing the script (only a restart seems to resolve it).

Separate bug, could you please open a separate issue?

GKSN commented 1 year ago

I am experiencing a similar issue.

1. I expect that when the script exits, the `dap-repl` buffer closes (it does not).

I use a command to close the dap-repl after disconnect:

dap.listeners.after.disconnect["dapui_config"] = function() vim.cmd("lua require('dap').close()") vim.cmd("lua require('dap').clear_breakpoints()") vim.cmd("lua require('dap.repl').close()") end

I don't know if it is the best way but it works for me.

  1. For some reason, if I switch to the dap-repl buffer and back to the script I'm debugging, the debugging menu panel, moves to the buffer containing the script (only a restart seems to resolve it).

Maybe what I'm talking about is not the same thing, but have you tried to change the location of the controls to another window? I changed the controls panel to the console window, it appears to be working. I'm not saying that this is not a bug just showing a possible working around.

dapui.setup({ controls = { element = "console", } })

nicolasaunai commented 1 year ago

Same issue with my config and events here

Disconnect event is caught when calling dap.terminate