svaante / dape

Debug Adapter Protocol for Emacs
GNU General Public License v3.0
477 stars 31 forks source link

[BUG] Cannot debug a Python application #29

Closed abougouffa closed 9 months ago

abougouffa commented 11 months ago

Hello,

First, I would like to thank you for this amazing package. I was looking for a replacement for dap-mode for a while and your package nails it!

I'm trying to debug a Python script. When I M-x dape, I'm getting the *dape-info* and *dape-repl* buffers. I'm seeing a temporary status in the mode-line [Dape:starting]. However, debug is not working, and when I inspect the *dape-debug* buffer, I see that something goes wrong, but it fails silently! Here is the content of *dape-debug*

[info] Starting new single session
[info] Process started ("python3" "-m" "debugpy.adapter")
[io] Sending:
(:arguments (:clientID "dape" :adapterID nil :pathFormat "path" :linesStartAt1 t :columnsStartAt1 t :supportsRunInTerminalRequest t :supportsProgressReporting t :supportsStartDebuggingRequest t) :type "request" :command "initialize" :seq 1)
[info] 
Process ("python3" "-m" "debugpy.adapter") exited with 1
[error] Timeout for reached for seq 1

Any ideas on how to fix this?

svaante commented 10 months ago

Hi and thanks! :)

Could you please share your debugpy configuration and make sure that debugpy is installed python3 -c "import debugpy.adapter".

abougouffa commented 10 months ago

Thanks for the reply,

I'm using the default value for dape-configs.

When I M-x shell-command then type python3 -c "import debugpy.adapter", I get (Shell command succeeded with no output). M-x shell-command then python3 --version gives Python 3.11.6.

Please note that on my machine, the Python 3.11.6 and debugpy is installed via the pyenv utility with all the relevant environment variables set and visible from Emacs (for example, I have /home/user/.pyenv/bin/ in my PATH envvar).

However, I've just found out that running run-python on my machine opens Python 3.7.3 (which is installed globally and doesn't have access to debugpy).

Does dape takes into account the paths set in PATH? I think I've tested this before (by printing a message in the ensure section of debugpy in dape-configs). I can see that debugpy is visible and available from there.

Do you have any idea how to fix this?

Thanks again for this awesome package

svaante commented 10 months ago

I have added an printout to dape-debug buffer which could help us solve this case.

Would it be possible for you to use the latest commit on master and post you dape-debug buffer?

abougouffa commented 10 months ago

Hello @svaante ,

Thank you for your responsiveness.

Here is the content of the *dape-debug* buffer after updating to the master branch.

I think the issue is related to the pyenv, I just can't find why! dape used to work on the same machine before I switched to using pyenv (for Python version issues).

[info] Starting new single session with config:
(modes (python-mode python-ts-mode) ensure (lambda (config) (let ((python (dape--config-eval-value (plist-get config 'command)))) (unless (zerop (call-process-shell-command (format "%s -c \"import debugpy.adapter\"" python))) (user-error "%s module debugpy is not installed" python)))) command "python3" command-args ("-m" "debugpy.adapter") :request "launch" :type "executable" :cwd "/home/user/work-stuff/workspace/rdkteam/qcom-rdkb/tools/yocto-cve-db/" :program "/home/user/work-stuff/workspace/rdkteam/qcom-rdkb/tools/yocto-cve-db/cve-update-db.py" :justMyCode nil :showReturnValue t)
[info] Process started ("python3" "-m" "debugpy.adapter")
[io] Sending:
(:arguments (:clientID "dape" :adapterID "executable" :pathFormat "path" :linesStartAt1 t :columnsStartAt1 t :supportsRunInTerminalRequest t :supportsProgressReporting t :supportsStartDebuggingRequest t) :type "request" :command "initialize" :seq 1)
[io] Flushing io buffer:

[info] 
Process ("python3" "-m" "debugpy.adapter") exited with 1
[error] Timeout for reached for seq 1
Aaronzinhoo commented 10 months ago

Was looking into this as well, I believe it has to do with call-process-shell-command. If you run (call-process-shell-command "env | which python3") I see it end up as the homebrew installed python and not pyenv. Whereas (call-process-shell-command "env | which python") provides the right path for me.

Edit: installed the debugpy on my homebrew installed python and dape started working for me at least. Seems like it might be beneficial to default to using python instead of python3 in the debugpy config?

svaante commented 10 months ago

@abougouffa Does the setting the debugpy configuration command to "python" solve your issue as @Aaronzinhoo suggested?

(plist-put (alist-get 'debugpy dape-configs) 'command "python")

This is strange, why does not python complain about debugpy module not existing.

@Aaronzinhoo Some distributions have chosen "python" to refer to "python2", but maybe using "python" as default is the better choice it's unclear to me. But either way I should add something about it in the wiki or readme

Aaronzinhoo commented 10 months ago

@svaante yea agreed about needing to add something to the wiki. You could also add a variable that enables us to set the Python executable path. This might be best overall for flexibility and correctness.

I would not worry about Python2 so much since most distributions now should only come with python3. Python2 has been sunsetted for a few years now.

svaante commented 10 months ago

Changed to python as defaults based on your inputs @Aaronzinhoo. Any updates @abougouffa?

abougouffa commented 9 months ago

Hello @svaante ,

Sorry, I didn't see your comments.

@abougouffa Does the setting the debugpy configuration command to "python" solve your issue as @Aaronzinhoo suggested?

Nop, I've tried then but didn't solve the problem. In my config, both (call-process-shell-command "env | which python3") and (call-process-shell-command "env | which python") returns a path in the pyenv path.

I've just tried it on the same machine, now it don't trigger the same error, instead, it blocks Emacs for a second then prints

dape--create-connection: Unable to connect to server localhost:35943
Aaronzinhoo commented 9 months ago

@abougouffa i believe this could be an issue of your environment within eMacs. You may want to run a shell command inside eMacs and outside.

which python

For making pyenv and emacs work together I have to set some env vars and use packages.

abougouffa commented 9 months ago

Thank you for the feedback @Aaronzinhoo ,

I will try to tinker with Python env vars to make it work, I presume you are talking about these variables?

https://github.com/Aaronzinhoo/dotfiles/blob/59f46b7b8b33c27cd97531b245e99cb570c18151/emacs/init.el#L2677-L2680

abougouffa commented 9 months ago

I did test it with pyvenv.el and with changing the Python env variables like the example above but with no success.

I still get this error when I run M-x dape:

dape--create-connection: Unable to connect to server localhost:35943

No *dape-debug* buffer is created!

svaante commented 9 months ago

dape-debug has been removed as dape has switched to jsonrcp.el instead hopefully the dape-repl buffer now has all the non communication errors present.

Do you get any output in "debugpy --help output" buffer?

(make-process :name "debugpy test"
              :command '("python" "-m" "debugpy.adapter" "--help")
              :connection-type 'pipe
              :buffer (generate-new-buffer "debugpy --help output")
              :coding 'utf-8-emacs-unix
              :noquery t
              :file-handler t)
abougouffa commented 9 months ago

Hello @svaante , Thanks for the reply!

I've tested the code above, it seems that Emacs didn't pick the right pyenv environment.

/usr/bin/python: No module named debugpy

Process debugpy test exited abnormally with code 1

However, when I start Emacs from terminal, it picks the pyenv environment and dape works as expected. So I think this issue is irrelevant, I need to see how I can activate pyenv in Emacs.