Hello everyone,
My environment:
OS: Ubuntu 24.04.2
Emacs: 30.1
Rust: rustc 1.85.1 (4eb161250 2025-03-15)
dap-mode (from Melpa): dap-mode-20250228.2153
Unfortunately I'm not at all fluent in Emacs Lisp so with the help of Google and ChatGPT, I came up with the following configuration, hoping that I might be able to set up Emacs for Rust programming:
I can confirm that
rust-analyzer
is already in$CARGO_HOME/bin
and visible in my$PATH
- The same goes for
rustfmt
- I also run the following commands in the terminal before configuring Emacs:
sudo apt update
sudo apt install -y gdb lldb llvm clang
cargo install cargo-binutils
And here is my configuration (my init.el
in ~/.emacs.d
I mean) for Rust programming:
(use-package rustic
:ensure t
:config
(setq rustic-lsp-client 'lsp-mode))
(use-package lsp-mode
:ensure t
:hook ((rustic-mode . lsp)
(lsp-mode . lsp-enable-which-key-integration))
:commands lsp)
(use-package lsp-ui
:ensure t
:commands lsp-ui-mode)
(use-package dap-mode
:ensure t
:config
(dap-auto-configure-mode)
(require 'dap-lldb) ;; Manually require dap-lldb
(require 'dap-gdb-lldb) ;; Also required for LLDB debugging
(require 'dap-cpptools))
(use-package company ;; Autocompletion
:ensure t
:hook (prog-mode . company-mode))
(use-package flycheck ;; Linting
:ensure t
:init (global-flycheck-mode))
(use-package which-key ;; Helpful keybindings
:ensure t
:config (which-key-mode))
(use-package magit ;; Git integration
:ensure t)
;;
;;
(setq lsp-rust-analyzer-server-command '("rust-analyzer"))
;;
;; Use Clippy for better diagnostics
(setq lsp-rust-analyzer-cargo-watch-command "clippy")
;;
;; Enable procedural macros
(setq lsp-rust-analyzer-proc-macro-enable t)
;;
;;
(use-package projectile
:ensure t
:config
(projectile-mode +1))
;;
;;
(defun my-dap-rust-debug-config ()
"Generate a DAP configuration for Rust automatically."
(let* ((project-root (projectile-project-root)))
(if (not project-root)
nil ;; Avoid plist error by returning nil
(let* ((binary-name (file-name-nondirectory (directory-file-name project-root))) ;; Extract folder name
(program-path (expand-file-name (concat "target/debug/" binary-name) project-root)))
(list :type "lldb"
:request "launch"
:name "Rust Debug"
:program program-path
:args '()
:cwd project-root)))))
;; Register Rust debug template only when inside a Rust project
(add-hook 'rustic-mode-hook
(lambda ()
(let ((config (my-dap-rust-debug-config)))
(when config
(dap-register-debug-template "Rust Debug Configuration" config)))))
So far, I've done multiple tests for running, compiling (check) and running tests (inside lib.rs) and the result seems to be quite satisfying. However, when it comes to debugging, nothing happens. My understanding is that I have to do
- Run
cargo build
inside my project directory to prepare the environment for debugging - and then in Emacs do
M-x dap-debug
- And finally choose "Rust Debug Configuration" option to start debugging and putting breakpoints
My problem is precisely that in the 2nd step, when I do M-x dap-debug
, Emacs offers me the following six options:
1) Rust Debug Configuration
2) cpptools::Run Configuration
3) LLDB Run Configuration
4) GDBServer Connect configuration
5) GDB Run Configuration
6) LLDB (Vs Code) :: Run Configuration
My understanding is that I have to choose the 1st option, Rust Debug Configuration. However, when I select this option, Emacs prints the following message:
Have you loaded the 'lldb' specific dap package?
And then after printing this message, Emacs does nothing.
Could you kindly give me a hand and indicate what is missing/wrong in my Emacs configuration, so that I might solve the issue and being able to debug my Rust programs using Emacs?
Thanks in advance,