Skip to content

[Bug] Dap error when opening rust file. #446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
6 of 7 tasks
chaozwn opened this issue Jul 9, 2024 · 16 comments · Fixed by #450
Closed
6 of 7 tasks

[Bug] Dap error when opening rust file. #446

chaozwn opened this issue Jul 9, 2024 · 16 comments · Fixed by #450
Labels
bug Something isn't working

Comments

@chaozwn
Copy link

chaozwn commented Jul 9, 2024

Have you read the docs and searched existing issues?

Neovim version (nvim -v)

0.10.0

Operating system/version

macos 14.5

Output of :checkhealth rustaceanvim

rustaceanvim: require("rustaceanvim.health").check()

Checking for Lua dependencies ~
- OK [mfussenegger/nvim-dap](https://github.com/mfussenegger/nvim-dap) installed.

Checking external dependencies ~
- OK rust-analyzer: found rust-analyzer 0.3.2029-standalone (a5b21ea0a 2024-07-07)
- OK Cargo: found cargo 1.79.0 (ffa9cf99a 2024-06-03)
- OK rustc: found rustc 1.79.0 (129f3b996 2024-06-10)
- OK debug adapter: found codelldb 

Checking config ~
- OK No errors found in config.

Checking for conflicting plugins ~
- OK No conflicting plugins detected.

Checking for tree-sitter parser ~
- OK tree-sitter parser for Rust detected.

How to reproduce the issue

{
    "mrcjkb/rustaceanvim",
    version = "^4",
    ft = "rust",
    specs = {
      {
        "AstroNvim/astrolsp",
        optional = true,
        ---@param opts AstroLSPOpts
        opts = {
          handlers = { rust_analyzer = false }, -- disable setup of `rust_analyzer`
        },
      },
    },
    opts = function()
      local adapter
      local success, package = pcall(function() return require("mason-registry").get_package "codelldb" end)
      local cfg = require "rustaceanvim.config"
      if success then
        local package_path = package:get_install_path()
        local codelldb_path = package_path .. "/codelldb"
        local liblldb_path = package_path .. "/extension/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 = package_path .. "\\extension\\adapter\\codelldb.exe"
          liblldb_path = package_path .. "\\extension\\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
        adapter = cfg.get_codelldb_adapter(codelldb_path, liblldb_path)
      else
        adapter = cfg.get_codelldb_adapter()
      end

      local astrolsp_avail, astrolsp = pcall(require, "astrolsp")
      local astrolsp_opts = (astrolsp_avail and astrolsp.lsp_opts "rust_analyzer") or {}
      local server = {
        ---@type table | (fun(project_root:string|nil, default_settings: table|nil):table) -- The rust-analyzer settings or a function that creates them.
        settings = function(project_root, default_settings)
          local astrolsp_settings = astrolsp_opts.settings or {}

          local merge_table = require("astrocore").extend_tbl(default_settings or {}, astrolsp_settings)
          local ra = require "rustaceanvim.config.server"
          -- load_rust_analyzer_settings merges any found settings with the passed in default settings table and then returns that table
          return ra.load_rust_analyzer_settings(project_root, {
            settings_file_pattern = "rust-analyzer.json",
            default_settings = merge_table,
          })
        end,
      }
      local final_server = require("astrocore").extend_tbl(astrolsp_opts, server)
      return { server = final_server, dap = { adapter = adapter }, tools = { enable_clippy = false } }
    end,
    config = function(_, opts) vim.g.rustaceanvim = require("astrocore").extend_tbl(opts, vim.g.rustaceanvim) end,
  }

Expected behaviour

normal

Actual behaviour

image

The minimal config used to reproduce this issue.

--WARNING: now rust-analyzer is can't use in neovim, because this issue
-- https://github.com/rust-lang/rust-analyzer/issues/17289
local utils = require "astrocore"
local set_mappings = require("astrocore").set_mappings

local function preview_stack_trace()
  local current_line = vim.api.nvim_get_current_line()
  local patterns_list = {
    "--> ([^:]+):(%d+):(%d+)",
    "at ([^:]+):(%d+):(%d+)",
  }

  local function try_patterns(patterns, line)
    for _, pattern in ipairs(patterns) do
      local filepath, line_nr, column_nr = string.match(line, pattern)
      if filepath and line_nr then return filepath, tonumber(line_nr), tonumber(column_nr or 0) end
    end
    return nil, nil, nil
  end

  local filepath, line_nr, column_nr = try_patterns(patterns_list, current_line)
  if filepath then
    vim.cmd ":wincmd k"
    vim.cmd("e " .. filepath)
    vim.api.nvim_win_set_cursor(0, { line_nr, column_nr })
  end
end

---@type LazySpec
return {
  {
    "AstroNvim/astrolsp",
    opts = {
      handlers = { rust_analyzer = false },
      config = {
        rust_analyzer = {
          on_attach = function()
            vim.api.nvim_create_autocmd({ "TermClose", "BufEnter" }, {
              pattern = "*cargo*",
              desc = "Jump to error line",
              callback = function()
                set_mappings({
                  n = {
                    ["gd"] = {
                      preview_stack_trace,
                      desc = "Jump to error line",
                    },
                  },
                }, { buffer = true })
              end,
            })
          end,
          settings = {
            ["rust-analyzer"] = {
              cargo = {
                allFeatures = true,
                loadOutDirsFromCheck = true,
                buildScripts = {
                  enable = true,
                },
              },
              -- Add clippy lints for Rust.
              checkOnSave = true,
              procMacro = {
                enable = true,
                ignored = {
                  ["async-trait"] = { "async_trait" },
                  ["napi-derive"] = { "napi" },
                  ["async-recursion"] = { "async_recursion" },
                },
              },
              -- Add clippy lints for Rust.
              check = {
                command = "clippy",
                extraArgs = { "--no-deps" },
              },
              assist = {
                importEnforceGranularity = true,
                importPrefix = "crate",
              },
              completion = {
                autoimport = {
                  enable = true,
                },
                enableSnippets = true,
              },
              inlayHints = {
                lifetimeElisionHints = {
                  enable = true,
                  useParameterNames = true,
                },
              },
              cachePriming = {
                enable = true,
                numThreads = 2,
              },
            },
          },
        },
      },
    },
  },
  {
    "nvim-treesitter/nvim-treesitter",
    opts = function(_, opts)
      if opts.ensure_installed ~= "all" then
        opts.ensure_installed = utils.list_insert_unique(opts.ensure_installed, { "rust", "toml", "ron" })
      end
    end,
  },
  {
    "jay-babu/mason-nvim-dap.nvim",
    optional = true,
    opts = function(_, opts)
      -- dap
      opts.ensure_installed = utils.list_insert_unique(opts.ensure_installed, { "codelldb" })
    end,
  },
  {
    "williamboman/mason-lspconfig.nvim",
    optional = true,
    opts = function(_, opts)
      -- lsp
      opts.ensure_installed = utils.list_insert_unique(opts.ensure_installed, { "rust_analyzer" })
    end,
  },
  {
    "mrcjkb/rustaceanvim",
    version = "^4",
    ft = "rust",
    dependencies = {
      {
        "AstroNvim/astrolsp",
        optional = true,
        ---@param opts AstroLSPOpts
        opts = {
          handlers = { rust_analyzer = false }, -- disable setup of `rust_analyzer`
        },
      },
    },
    opts = function()
      local adapter
      local success, package = pcall(function() return require("mason-registry").get_package "codelldb" end)
      local cfg = require "rustaceanvim.config"
      if success then
        local package_path = package:get_install_path()
        local codelldb_path = package_path .. "/codelldb"
        local liblldb_path = package_path .. "/extension/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 = package_path .. "\\extension\\adapter\\codelldb.exe"
          liblldb_path = package_path .. "\\extension\\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
        adapter = cfg.get_codelldb_adapter(codelldb_path, liblldb_path)
      else
        adapter = cfg.get_codelldb_adapter()
      end

      local astrolsp_avail, astrolsp = pcall(require, "astrolsp")
      local astrolsp_opts = (astrolsp_avail and astrolsp.lsp_opts "rust_analyzer") or {}
      local server = {
        ---@type table | (fun(project_root:string|nil, default_settings: table|nil):table) -- The rust-analyzer settings or a function that creates them.
        settings = function(project_root, default_settings)
          local astrolsp_settings = astrolsp_opts.settings or {}

          local merge_table = require("astrocore").extend_tbl(default_settings or {}, astrolsp_settings)
          local ra = require "rustaceanvim.config.server"
          -- load_rust_analyzer_settings merges any found settings with the passed in default settings table and then returns that table
          return ra.load_rust_analyzer_settings(project_root, {
            settings_file_pattern = "rust-analyzer.json",
            default_settings = merge_table,
          })
        end,
      }
      local final_server = require("astrocore").extend_tbl(astrolsp_opts, server)
      return { server = final_server, dap = { adapter = adapter }, tools = { enable_clippy = false } }
    end,
    config = function(_, opts) vim.g.rustaceanvim = require("astrocore").extend_tbl(opts, vim.g.rustaceanvim) end,
  },
  {
    "Saecki/crates.nvim",
    lazy = true,
    dependencies = {
      "AstroNvim/astrocore",
      opts = {
        autocmds = {
          CmpSourceCargo = {
            {
              event = "BufRead",
              desc = "Load crates.nvim into Cargo buffers",
              pattern = "Cargo.toml",
              callback = function()
                require("cmp").setup.buffer { sources = { { name = "crates" } } }
                require "crates"
              end,
            },
          },
        },
      },
    },
    opts = {
      completion = {
        cmp = { enabled = true },
        crates = {
          enabled = true,
        },
      },
      null_ls = {
        enabled = true,
        name = "crates.nvim",
      },
    },
  },
  {
    "nvim-neotest/neotest",
    optional = true,
    opts = function(_, opts)
      if not opts.adapters then opts.adapters = {} end
      local rustaceanvim_avail, rustaceanvim = pcall(require, "rustaceanvim.neotest")
      if rustaceanvim_avail then table.insert(opts.adapters, rustaceanvim) end
    end,
  },
}
@chaozwn chaozwn added the bug Something isn't working label Jul 9, 2024
@chaozwn
Copy link
Author

chaozwn commented Jul 9, 2024

link: mason-org/mason.nvim#1741

@mrcjkb
Copy link
Owner

mrcjkb commented Jul 9, 2024

Hey 👋

Thanks for reporting, but that is not a minimal config.

Closing for now.
Feel free to reproduce with a minimal config as requested in the issue template + troubleshooting section and then reopen. 😀

@mrcjkb mrcjkb closed this as completed Jul 9, 2024
@mrcjkb mrcjkb reopened this Jul 9, 2024
@mrcjkb mrcjkb closed this as completed Jul 9, 2024
@mrcjkb mrcjkb added needs reproduction and removed bug Something isn't working labels Jul 9, 2024
@braxtons12
Copy link

I don't have a minimal config for this (yet, will try to put one together this weekend if original reporter doesn't beat me to it), but want to say I'm also experiencing this on Arch Linux w/ the latest nvim nightly.

The problem occurs when first opening a Rust file and a very similar (line 26 instead of 46) problem occurs when running :RustLsp debuggables

@chaozwn
Copy link
Author

chaozwn commented Jul 10, 2024

I don't have a minimal config for this (yet, will try to put one together this weekend if original reporter doesn't beat me to it), but want to say I'm also experiencing this on Arch Linux w/ the latest nvim nightly.

The problem occurs when first opening a Rust file and a very similar (line 26 instead of 46) problem occurs when running :RustLsp debuggables

Lock the version for now to avoid this problem.
opts.ensure_installed = utils.list_insert_unique(opts.ensure_installed, { "rust_analyzer@2024-05-27" })

@mrcjkb mrcjkb reopened this Jul 10, 2024
@mrcjkb
Copy link
Owner

mrcjkb commented Jul 10, 2024

Caused by: rust-lang/rust-analyzer#17547

@tim-harding
Copy link

tim-harding commented Jul 10, 2024

FWIW, I'm still getting this error on 4.26.1, both with the stable and nightly versions of Neovim and rust-analyzer. Won't be around for a couple of days, but I'll try and put together a repro when I get back.

@mrcjkb
Copy link
Owner

mrcjkb commented Jul 11, 2024

@tim-harding if you post a stack trace, perhaps I can find out if there's been another breaking rust-analyzer change.

@tim-harding
Copy link

On further inspection, it seems that the issue is coming from using codelldb instead of codelldb-bin from the AUR. If you see any reports with this sort of thing in the DAP log, they might be the same issue:

[ INFO ] 2024-07-10T15:20:17Z-0700 ] .../tim/.local/share/nvim/lazy/nvim-dap/lua/dap/session.lua:904 ]	"Breakpoint unverified"	{
  id = 1,
  message = "Resolved locations: 0",
  verified = false
}

For clarification, with some Rust and Neovim versions I would see the error from this issue, but usually I was getting a DAP session that would just seem to hang, with no breakpoints hit and nothing in the scopes window. Apologies for the noise in this thread, after hours of trying I thought my issue might be relevant here because I saw the same thing sometimes, but it seems it was unrelated after all.

@Ciel-MC
Copy link

Ciel-MC commented May 3, 2025

I haven't used nvim in a long time, I just came back, fixed lspconfig, but now when I open a Rust file I get this same error, any ideas?

@mrcjkb
Copy link
Owner

mrcjkb commented May 3, 2025

#446 (comment)

@Ciel-MC
Copy link

Ciel-MC commented May 3, 2025

#446 (comment)

   Error  01:12:00 AM msg_show.lua_error Error executing vim.schedule lua callback: ...are/nvim/lazy/rustaceanvim/lua/rustaceanvim/lsp/init.lua:297: attempt to call field 'pretty_print' (a nil value)
stack traceback:
	...are/nvim/lazy/rustaceanvim/lua/rustaceanvim/lsp/init.lua:297: in function 'callback'
	.../share/nvim/lazy/rustaceanvim/lua/rustaceanvim/cargo.lua:81: in function 'callback'
	.../share/nvim/lazy/rustaceanvim/lua/rustaceanvim/cargo.lua:32: in function ''
	vim/_editor.lua: in function <vim/_editor.lua:0>

This is all I have, no idea how it's possibly ngl

@mrcjkb
Copy link
Owner

mrcjkb commented May 3, 2025

@Ciel-MC are you using the latest version of this plugin?

This is the line #297 that is calling a non-existent pretty_print field, according to your stack trace:

lsp_start_config.init_options = vim.tbl_deep_extend(
'force',

A search for pretty_print in this repo yields no results (except for your comment).
So you either have an issue in your config or another plugin is somehow interfering - plus you're likely using an outdated version of rustaceanvim?

@Ciel-MC
Copy link

Ciel-MC commented May 3, 2025

@Ciel-MC are you using the latest version of this plugin?

This is the line #297 that is calling a non-existent pretty_print field, according to your stack trace:

rustaceanvim/lua/rustaceanvim/lsp/init.lua

Lines 297 to 298 in 4b8c2ed

lsp_start_config.init_options = vim.tbl_deep_extend(
'force',
A search for pretty_print in this repo yields no results (except for your comment). So you either have an issue in your config or another plugin is somehow interfering - plus you're likely using an outdated version of rustaceanvim?

Hi, sorry about that. It is the line you highlighted that was the problem. I just tried to print the table out (which ended on the same time) for debugging. The problem is that the table extend’s second argument is nil.

@Ciel-MC
Copy link

Ciel-MC commented May 3, 2025

   Error  01:43:40 AM msg_show.lua_error Error executing vim.schedule lua callback: vim/shared.lua:0: after the second argument: expected table, got nil
stack traceback:
	[C]: in function 'error'
	vim/shared.lua: in function 'validate'
	vim/shared.lua: in function 'tbl_deep_extend'
	...are/nvim/lazy/rustaceanvim/lua/rustaceanvim/lsp/init.lua:297: in function 'callback'
	.../share/nvim/lazy/rustaceanvim/lua/rustaceanvim/cargo.lua:81: in function 'callback'
	.../share/nvim/lazy/rustaceanvim/lua/rustaceanvim/cargo.lua:32: in function ''
	vim/_editor.lua: in function <vim/_editor.lua:0>

Here it is with the debug line removed

@mrcjkb
Copy link
Owner

mrcjkb commented May 3, 2025

#746

@Ciel-MC
Copy link

Ciel-MC commented May 4, 2025

#746

Werked, tanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants