How to debug Rust in dap-mode?

I have read Configuration - DAP Mode, :tools debugger - Doom Emacs v21.12 documentation and Debugging Rust in Emacs with dap-mode neither have helped to setup a debug environment to debug rust in doom emacs.

My current config.el is

(after! dap-mode
  (setq dap-cpptools-extension-version "1.5.1")

  (require 'dap-gdb-lldb)

  (with-eval-after-load 'lsp-rust
    (require 'dap-cpptools))

  (dap-register-debug-template "Rust::GDB Run Configuration"
                             (list :type "gdb"
                                   :request "launch"
                                   :name "GDB::Run"
                           :gdbpath "rust-gdb"
                                   :target nil
                                   :cwd nil))

  (with-eval-after-load 'dap-cpptools
    ;; Add a template specific for debugging Rust programs.
    ;; It is used for new projects, where I can M-x dap-edit-debug-template
    (dap-register-debug-template "Rust::CppTools Run Configuration"
                                 (list :type "cppdbg"
                                       :request "launch"
                                       :name "Rust::Run"
                                       :MIMode "gdb"
                                       :miDebuggerPath "rust-gdb"
                                       :environment []
                                       :program "${workspaceFolder}/target/debug/hello / replace with binary"
                                       :cwd "${workspaceFolder}"
                                       :console "external"
                                       :dap-compilation "cargo build"
                                       :dap-compilation-dir "${workspaceFolder}")))

  (with-eval-after-load 'dap-mode
    (setq dap-default-terminal-kind "integrated")
    (dap-auto-configure-mode +1)))

(map! :map dap-mode-map
      :prefix ("d" . "dap")
      ;; basics
      :desc "dap next"          "n" #'dap-next
      :desc "dap step in"       "i" #'dap-step-in
      :desc "dap step out"      "o" #'dap-step-out
      :desc "dap continue"      "c" #'dap-continue
      :desc "dap hydra"         "h" #'dap-hydra
      :desc "dap debug restart" "r" #'dap-debug-restart
      :desc "dap debug"         "s" #'dap-debug

      ;; debug
      :prefix ("dd" . "Debug")
      :desc "dap debug recent"  "r" #'dap-debug-recent
      :desc "dap debug last"    "l" #'dap-debug-last

      ;; eval
      :prefix ("de" . "Eval")
      :desc "eval"                "e" #'dap-eval
      :desc "eval region"         "r" #'dap-eval-region
      :desc "eval thing at point" "s" #'dap-eval-thing-at-point
      :desc "add expression"      "a" #'dap-ui-expressions-add
      :desc "remove expression"   "d" #'dap-ui-expressions-remove

      :prefix ("db" . "Breakpoint")
      :desc "dap breakpoint toggle"      "b" #'dap-breakpoint-toggle
      :desc "dap breakpoint condition"   "c" #'dap-breakpoint-condition
      :desc "dap breakpoint hit count"   "h" #'dap-breakpoint-hit-condition
      :desc "dap breakpoint log message" "l" #'dap-breakpoint-log-message)

and my current init.el is

(doom! :ui
       deft              ; notational velocity for Emacs
       doom              ; what makes DOOM look the way it does
       doom-dashboard    ; a nifty splash screen for Emacs
       doom-quit         ; DOOM quit-message prompts when you quit Emacs
       (emoji +unicode)  ; 🙂
       hl-todo           ; highlight TODO/FIXME/NOTE/DEPRECATED/HACK/REVIEW
       indent-guides     ; highlighted indent columns
       (ligatures +extra)  ; ligatures and symbols to make your code pretty again
       minimap             ; show a map of the code on the side
       modeline            ; snazzy, Atom-inspired modeline, plus API
       nav-flash         ; blink cursor line after big motions
       neotree           ; a project drawer, like NERDTree for vim
       ophints             ; highlight the region an operation acts on
       (popup +defaults)   ; tame sudden yet inevitable temporary windows
       tabs                ; a tab bar for Emacs
       treemacs          ; a project drawer, like neotree but cooler
       unicode           ; extended unicode support for various languages
       (vc-gutter +pretty) ; vcs diff in the fringe
       vi-tilde-fringe     ; fringe tildes to mark beyond EOB
       window-select       ; visually switch windows
       workspaces          ; tab emulation, persistence & separate workspaces

       (debugger +lsp)     ; FIXME stepping through code, to help you add bugs
       lsp                 ; M-x vscode

       (:if IS-MAC macos)  ; improve compatibility with macOS
       tty               ; improve the terminal Emacs experience

       (rust +lsp)       ; Fe2O3.unwrap().unwrap().unwrap().unwrap()
       (default +bindings +smartparens))

And my current packages.el is

Why nobody can help with debugging, I have a bug that no idea of why happens and need debug, but cannot use debug.

Can you provide what is happening at least?

I got debugging running using the guide you linked that is using cpptools. There is a bug that occurs sometimes that it doesn’t download cpptools when you run (dap-cpptools-setup). Maybe thats whats wrong here? Do ls ~/.config/emacs/.local/etc/dap-extension/vscode/cpptools. If that is empty, delete the folder and run (dap-cpptools-setup) again.

Also you have to change the template according to your project of course. So

    (dap-register-debug-template "Rust::CppTools Run Configuration"
                                 (list :type "cppdbg"
                                       :request "launch"
                                       :name "Rust::Run"
                                       :MIMode "gdb"
                                       :miDebuggerPath "rust-gdb"
                                       :environment []
                                       :program "${workspaceFolder}/target/debug/hello / replace with binary"
                                       :cwd "${workspaceFolder}"
                                       :console "external"
                                       :dap-compilation "cargo build"
                                       :dap-compilation-dir "${workspaceFolder}"))

can not stay like this. The variable :program must be changed. I also noticed that M-x debug-debug-edit-template does not work very intuitively. In the window that pop ups, you can not simply change that template and it will use that. You have to change the name (The Rust::CppTools Run Configuration and the :name variable it seems) at least. Then run M-x eval-buffer for that template and in your project run M-x dap-debug and choose the newly created template.

Here is what I have in my config, if it helps:

(use-package dap-mode
  (dap-ui-controls-mode 1)

  ;; (require 'dap-lldb)
  (require 'dap-cpptools)
  ;; (require 'dap-gdb-lldb)
  ;; installs .extension/vscode
  ;; (dap-gdb-lldb-setup)
(dap-register-debug-template "Rust::CppTools Run Configuration"
                                 (list :type "cppdbg"
                                       :request "launch"
                                       :name "Rust::Run"
                                       :MIMode "gdb"
                                       :miDebuggerPath "rust-gdb"
                                       :environment []
                                       :program "${workspaceFolder}/target/debug/REPLACETHIS"
                                       :cwd "${workspaceFolder}"
                                       :console "external"
                                       :dap-compilation "cargo build"
                                       :dap-compilation-dir "${workspaceFolder}")))

  (with-eval-after-load 'dap-mode
    (setq dap-default-terminal-kind "integrated") ;; Make sure that terminal programs open a term for I/O in an Emacs buffer
    (dap-auto-configure-mode +1))

I also have (debugger +lsp) and (rust +lsp) in my init.el like you do.

The templates created with `(M-x dap-debug-edit-template) are lost when you exit emacs. If you are tired of making a new template every time you restart emacs, you can use a launch.json file in the project root. (The folder where you cargo.toml is) It should look something like this:

    "configurations": [
            "type": "cppdbg",
            "request": "launch",
            "name": "Debugtest run",
            "MIMode": "gdb",
            "miDebuggerPath": "rust-gdb",
            "environment": [],
            "program": "${workspaceFolder}/target/debug/debugtest",
            "cwd": "${workspaceFolder}",
            "console": "external",
            "dap-compilation": "cargo build",
            "dap-compilation-dir": "${workspaceFolder}",
ls ~/.config/emacs/.local/etc/dap-extension/vscode/cpptools

I do not know what you are implying with this, but since dap-cpptools changed a bit in recently, I want to extent this a bit.

So what you need on your system is lldb now. No need to download and compile lldb-mi as well as other tutorials might imply, because this was the past. Now add

  (require 'dap-lldb)
  (require 'dap-cpptools)

to your config and eval it. Then run M-x dap-cpptools-setup. If it does nothing it is becaus it ran before. In this case force it to download using SPC u M-x dap-cpptools-setup (or C-u instead of SPC u when you don’t use Dooms evil package).

Then forget all debug templates, just go M-x lsp-rust-analyzer-debug and it will ask you where to start (which test or main). Or activate M-x lsp-lens-mode and it will show you the -> Run | Debug thingies next to tests and the main function.