You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法配置nvim-dap实现Rust开发调试功能

Rust调试问题:nvim-dap + rustaceanvim启动调试直接运行程序结束,无法进入调试

我尝试配置Rust开发调试环境已有一段时间,但始终无法成功:启动调试会话时,程序仅运行可执行文件就直接结束,无法进入调试环节,完全无法满足需求。

我已执行rustc demo.rs生成了demo可执行文件,但无法启动调试器。我使用nvim-dap作为主调试工具,同时使用rustaceanvim(rust-tools的全新分支,因rust-tools已归档)。rustaceanvim说明称:

This is a filetype plugin that works out of the box, so there is no need to call a setup function or configure anything to get this plugin working.

因此我仅为rustaceanvim配置了一些按键映射,未进行其他设置,主要配置了nvim-dap,代码如下:

local dap = require("dap")
dap.adapters.codelldb = {
    type = "server",
    host = "127.0.0.1",
    port = "${port}",
    executable = {
        command = "$HOME/.local/share/nvim/mason/bin/codelldb",
        args = { "--port", "${port}" },
    },
}

dap.configurations.rust = {
    {
        name = "Launch",
        type = "codelldb",
        request = "launch",
        program = function()
            return vim.fn.input("Path to executable: ", vim.fn.getcwd() .. "/target/debug/", "file")
        end,
        cwd = "${workspaceFolder}",
        stopOnEntry = false,
    },
}

无论我尝试何种操作,问题依旧。

可能的原因及解决方法

1. 编译时未生成调试符号

直接用rustc编译代码时,默认不会生成调试信息,调试器无法识别断点和执行流程。必须添加-g参数编译:

rustc -g demo.rs

如果用Cargo管理项目,cargo build默认的debug模式已经包含调试符号,无需额外参数。

2. 手动nvim-dap配置与rustaceanvim自动配置冲突

rustaceanvim会自动配置nvim-dap的Rust调试环境,你手动编写的dap.adapters.codelldb和dap.configurations.rust可能和它的默认配置冲突。

  • 注释掉你nvim-dap配置中关于Rust的部分,仅保留rustaceanvim的默认配置,再尝试启动调试。

3. 调试配置未设置启动暂停

你的配置中stopOnEntry = false,如果程序逻辑简单、执行速度快,会直接运行结束,来不及进入调试环节。

  • 将配置中的stopOnEntry改为true,这样程序启动后会立刻暂停,此时可以设置断点再继续执行:
dap.configurations.rust = {
    {
        name = "Launch",
        type = "codelldb",
        request = "launch",
        program = function()
            return vim.fn.input("Path to executable: ", vim.fn.getcwd() .. "/target/debug/", "file")
        end,
        cwd = "${workspaceFolder}",
        stopOnEntry = true, -- 修改这里
    },
}

4. 检查可执行文件路径正确性

调试时输入的可执行文件路径,必须是带调试符号的版本。如果之前编译过不带-g的程序,要确保选择的是重新编译后的可执行文件。

5. 查看调试日志排查错误

如果以上方法都无效,开启nvim-dap的调试日志查看具体错误:

require("dap").set_log_level("DEBUG")

日志文件路径为~/.cache/nvim/dap.log,查看其中的错误信息定位问题。

内容的提问来源于stack exchange,提问作者imindMan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 03:57:45