simrat39 / rust-tools.nvim

Tools for better development in rust using neovim's builtin lsp
MIT License
2.17k stars 158 forks source link

Debugging does not work in rust with no error #289

Open mateuszaaa opened 1 year ago

mateuszaaa commented 1 year ago

Hi all, i really appreciate your work. I wanted to test Rust debugging but for some reason it does not work... There are no errors or whatsoever:

rust-tool setup

local rt = require("rust-tools")
rt.setup({
  server = {
    standalone = false,
    on_attach = function(_, bufnr)
      -- Hover actions
      -- vim.keymap.set("n", "<C-space>", rt.hover_actions.hover_actions, { buffer = bufnr })
      -- -- Code action groups
      -- vim.keymap.set("n", "<Leader>a", rt.code_action_group.code_action_group, { buffer = bufnr })

      vim.opt.updatetime = 100
      local diag_float_grp = vim.api.nvim_create_augroup("DiagnosticFloat", { clear = true })
      vim.api.nvim_create_autocmd("CursorHold", {
          callback = function()
              vim.diagnostic.open_float(nil, { focusable = false })
          end,
          group = diag_float_grp,
      })
      local opts = { noremap=true, silent=true }
      vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gD', '<cmd>lua vim.lsp.buf.declaration()<CR>', opts)
      vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gd', '<cmd>lua vim.lsp.buf.definition()<CR>', opts)
      vim.api.nvim_buf_set_keymap(bufnr, 'n', 'K', '<cmd>lua vim.lsp.buf.hover()<CR>', opts)
      vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>', opts)
      vim.api.nvim_buf_set_keymap(bufnr, 'n', '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts)
      vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>D', '<cmd>lua vim.lsp.buf.type_definition()<CR>', opts)
      vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', opts)
      vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>ca', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts)
      vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>', opts)
      vim.api.nvim_buf_set_keymap(bufnr, 'n', '[d', '<cmd>lua vim.lsp.diagnostic.goto_prev({ border = "single" })<CR>', opts)
      vim.api.nvim_buf_set_keymap(bufnr, 'n', ']d', '<cmd>lua vim.lsp.diagnostic.goto_next({ border = "single" })<CR>', opts)
      vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>cd', '<cmd>lua vim.diagnostic.open_float()<CR>', opts)
      vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>f', '<cmd>lua vim.lsp.buf.formatting()<CR>', opts)
      vim.api.nvim_buf_set_keymap(bufnr, 'n', '<space>q', '<cmd>lua vim.diagnostic.set_loclist()<CR>', opts)
      vim.api.nvim_buf_set_keymap(bufnr, 'n', 'K', '<cmd>lua vim.lsp.buf.hover()<CR>', opts)
      vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc')
    end,
      },
    dap = {
      adapter = require('rust-tools.dap').get_codelldb_adapter(
        "/home/dev/.vscode-oss/extensions/vadimcn.vscode-lldb-1.8.1-universal/adapter/codelldb",
        "/home/dev/.vscode-oss/extensions/vadimcn.vscode-lldb-1.8.1-universal/lldb/lib/liblldb.so"
      )
    }
})

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

https://user-images.githubusercontent.com/5952088/206032994-73691e0e-208f-4be2-83ee-37aabde69af1.gif ss

kting28 commented 1 year ago

@mateuszaaa , do you have breakpoints set up before starting the debug? Since the default nvim-dap config created by rust-tools.nvim is not to "stop on entry", without a breakpoint it simply exits.

Try :lua require'dap'.toggle_breakpoint() to break at entrance of code first.

ConnorWay32 commented 1 year ago

Have a similar issue with CodeLLDB v1.9.0 using Debug through the hover actions. I get the Compiling a debug build message, then nothing happens. Breakpoints have been set in the function being debugged, and my rust-tools config is as follows

    opts = {
      dap = {
        adapter = function()
          local extension_path = vim.env.HOME .. "/.vscode/extensions/vadimcn.vscode-lldb-1.9.0"
          local codelldb_path = extension_path .. "adapter/codelldb"
          local liblldb_path = extension_path .. "lldb/lib/liblldb.so"

          require("rust-tools.dap").get_codelldb_adapter(codelldb_path, liblldb_path)
        end,
      },
      server = {
        on_attach = function(_, bufnr)
          vim.keymap.set("n", "K", require("rust-tools").hover_actions.hover_actions, { buffer = bufnr })
          vim.keymap.set(
            "n",
            "<leader>ca",
            require("rust-tools").code_action_group.code_action_group,
            { buffer = bufnr }
          )
          -- vim.keymap.set("n", "<C-R>", require("rust-tools").debuggables.debuggables())
        end,
      },
    },
robamu commented 1 year ago

I have the same issue as @ConnorWay32 with the following rust-tools.cfg:

local generic_cfg = require("setup/generic-lspcfg")
local rt = require("rust-tools")

local mason_registry = require("mason-registry")

local codelldb = mason_registry.get_package("codelldb")
-- Copied from https://github.com/simrat39/rust-tools.nvim/wiki/Debugging
-- Adapted for mason
local extension_path = codelldb:get_install_path() .. "/extension/"
local codelldb_path = extension_path .. "adapter/codelldb"
-- This is the shared object for Ubuntu.. might be different on different OSes
local liblldb_path = extension_path .. "lldb/lib/liblldb"
local this_os = vim.loop.os_uname().sysname;

-- The path in windows is different
if this_os:find "Windows" then
  codelldb_path = extension_path .. "adapter\\codelldb.exe"
  liblldb_path = extension_path .. "lldb\\bin\\liblldb.dll"
else
  -- The liblldb extension is .so for linux and .dylib for macOS
  liblldb_path = liblldb_path .. (this_os == "Linux" and ".so" or ".dylib")
end

-- Configure LSP through rust-tools.nvim plugin.
-- rust-tools will configure and enable certain LSP features for us.
-- See https://github.com/simrat39/rust-tools.nvim#configuration
rt.setup({
  runnables = {
    use_telescope = true,
  },

  -- all the opts to send to nvim-lspconfig
  -- these override the defaults set by rust-tools.nvim
  -- see https://github.com/neovim/nvim-lspconfig/blob/master/CONFIG.md#rust_analyzer
  server = {
    -- on_attach is a callback called when the language server attachs to the buffer
    on_attach = function(_, bufnr)
      generic_cfg.on_attach(_, bufnr)
      generic_cfg.nmap("<Leader>k", rt.hover_actions.hover_actions, "Hover actions")
    end,
    settings = {
      -- to enable rust-analyzer settings visit:
      -- https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/user/generated_config.adoc
      ["rust-analyzer"] = {
        -- enable clippy on save
        checkOnSave = {
          command = "clippy",
        },
      },
    },
  },

  dap = {
    adapter = require("rust-tools.dap").get_codelldb_adapter(codelldb_path, liblldb_path)

  }
})

You can find the full configuration here: https://github.com/robamu/nvim-cfg/blob/main/lua/setup/rust-tools.lua

robamu commented 1 year ago

It actually works, I just had an error with dap-ui not opening automatically, and no breakpoint being set, so it was a bit confusing that nothing appeared to happen, but its working :-)