emacs-lsp / dap-mode

Emacs :heart: Debug Adapter Protocol
https://emacs-lsp.github.io/dap-mode
GNU General Public License v3.0
1.3k stars 182 forks source link

Python Flask in dap-mode #573

Closed DJManas closed 2 years ago

DJManas commented 2 years ago

Hello,

I have problem with debugging of flask app inside my emacs. I don't have vanilla, but using Spacemacs or Doom one, both are the same. System Mac OS Emacs version GNU Emacs 27.2 (build 1, x86_64-apple-darwin21.3.0, Carbon Version 165 AppKit 2113.3) of 2022-02-20 Python version 3.9.10

pip list
Package                Version
---------------------- ---------
astroid                2.9.3
autopep8               1.5.5
Brotli                 1.0.9
certifi                2021.10.8
charset-normalizer     2.0.12
click                  8.0.4
columnize              0.3.10
debugpy                1.5.1
distlib                0.3.4
epc                    0.0.5
filelock               3.6.0
flake8                 3.8.4
Flask                  2.0.3
Flask-Classy           0.6.10
idna                   3.3
importmagic            0.1.7
isort                  5.10.1
itsdangerous           2.1.0
jedi                   0.17.2
Jinja2                 3.0.3
lazy-object-proxy      1.7.1
mariadb                1.0.10
MarkupSafe             2.1.0
mccabe                 0.6.1
multivolumefile        0.2.3
nodeenv                1.6.0
nose                   1.3.7
parso                  0.7.1
pip                    22.0.3
pipenv                 2022.1.8
platformdirs           2.5.1
pluggy                 1.0.0
psycopg2               2.9.3
ptvsd                  4.3.2
py7zr                  0.17.4
pybcj                  0.5.0
pycodestyle            2.6.0
pycryptodomex          3.14.1
pydebug                1.0.3
pydocstyle             6.1.1
pyficache              2.3.0
pyflakes               2.2.0
Pygments               2.11.2
pylint                 2.12.2
pyppmd                 0.17.3
pyright                1.1.225
python-jsonrpc-server  0.4.0
python-language-server 0.36.2
python-lsp-jsonrpc     1.0.0
python-lsp-server      1.3.3
pyzstd                 0.15.2
requests               2.27.1
rope                   0.23.0
setuptools             60.9.3
sexpdata               0.0.3
six                    1.16.0
snowballstemmer        2.2.0
spark-parser           1.8.9
term-background        1.0.1
texttable              1.6.4
toml                   0.10.2
tracer                 0.3.2
trepan3k               1.2.8
typing_extensions      4.1.1
ujson                  5.1.0
uncompyle6             3.8.0
urllib3                1.26.8
virtualenv             20.13.2
virtualenv-clone       0.5.7
Werkzeug               2.0.3
wrapt                  1.13.3
xdis                   6.0.3
yapf                   0.32.0

When I go into the project directory, where I have launch.json, generated by VSCode, the template gets shown, when I M-X and use dap-debug, I see the template there, no problem, but when I launch it, it won't work.

The template looks like

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Kotelna",
      "type": "python",
      "request": "launch",
      "module": "flask",
      "cwd": "${workspaceFolder}",
      "env": {
        "FLASK_APP": "app/app.py",
        "FLASK_ENV": "development",
        "FLASK_DEBUG": "1"
      },
      "args": [
        "run",
        "--no-debugger",
        "--no-reload"
      ],
      "jinja": true
    }
  ]
}

I have setup python in Doom like this (Doom can use debugpy).

(use-package! lsp-python-ms
  :init (setq lsp-python-ms-auto-install-server t)
  :config
  (setq! lsp-pylsp-plugins-autopep8-enabled t
         lsp-pylsp-plugins-flake8-config "~/.config/flake8"
         lsp-pylsp-plugins-pylint-args ["--max-line-length 120" "--indent-string='  '"]
         )
  (require 'lsp-python-ms)
  (require 'dap-python)
  :hook (python-mode . (lambda ()
                         (lsp)))
  )

(after! dap-mode
  (setq dap-python-debugger 'debugpy
        dap-auto-configure-mode t))

(add-hook 'dap-stopped-hook
          (lambda (arg) (call-interactively #'dap-hydra)))

When I launch the debugger inside my project using M-X dap-debug and select the Kotelna configuration I get this.

Usage: python -m flask [OPTIONS] COMMAND [ARGS]...

  A general utility script for Flask applications.

  Provides commands from Flask, extensions, and the application. Loads the
  application defined in the FLASK_APP environment variable, or from a wsgi.py
  file. Setting the FLASK_ENV environment variable to 'development' will
  enable debug mode.

    $ export FLASK_APP=hello.py
    $ export FLASK_ENV=development
    $ flask run

Options:
  --version  Show the flask version
  --help     Show this message and exit.

Commands:
  routes  Show the routes for the app.
  run     Run a development server.
  shell   Run a shell in the app context.

In * Messages buffer I can see this

Switched to ’KotelnaFlask’
Projectile is initializing cache for /Users/djmanas/Projekty/Python/KotelnaFlask/ ...
funcall-interactively: End of buffer
Loading /Users/djmanas/.emacs.d/.local/cache/recentf...done
Note: standard-indent, tab-width, evil-shift-width adjusted to 2
LSP :: Guessed project root is ~/Projekty/Python/KotelnaFlask
LSP :: Connected to [pylsp:45218/starting].
LSP :: pylsp:45218 initialized successfully in folders: (/Users/djmanas/Projekty/Python/KotelnaFlask)
Note: js-indent-level, tab-width, evil-shift-width adjusted to 2
Saved text until "aunch",
      "jinja": true
    }
  ]
}
"
No message handler for process
Debug session process exited with status: killed: 9

Beginning of buffer [17 times]
Saved text from "Usage: python -m flask [OPTIONS] COMMAND"

I had tried the discord and was pointed here by @yyoncho. Also he pointed me to this link that there was something similar with the ptvsd (I got almost the same with ptvsd under spacemacs so I wanted to alter it a bit, but I am new to emacs and lisp). I might try to debug the process, but I will have to get used to emacs and lisp debugging process.

Thanks, Regards, Petr Sourek

DJManas commented 2 years ago

I had played a little with ptvsd and dap-python.el. Had found the correct command for and/not and had altered the file on line 205, where I had changed (if program (shell-quote-argument program) "") to (if (and program (not (string-equal module "flask"))) (shell-quote-argument program) "")

Then I had started the debugger (spc d d-d), it "worked", the command was: python -m ptvsd --wait --host localhost --port 56814 -m flask run --no-debugger --no-reload but I have hunch, that it doesn't send the enviromental variables into the command, because the result is.

Missing launch request or attach request before configuration done request.
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
Usage: python -m flask run [OPTIONS]
Try 'python -m flask run --help' for help.

Error: Failed to find Flask application or factory in module 'app'. Use 'FLASK_APP=app:name' to specify one.

I had tried to rewrite the launch json into * DAP Template like this

(dap-register-debug-template "Flask"
                             (list :type "python"
                                   :env '(
                                          (:FLASK_APP "app/app.py")
                                          (:FLASK_ENV "development"))
                                   :request "launch"
                                   :args "run --no-debugger --no-reload"
                                   :module "flask"
                                   :jinja t
                                   :name "Flask"))

But I am getting this error when launching it shell-quote-argument: Wrong type argument: sequencep, 114 I will try to debug it tomorrow. I have a hunch it has something to do with my if statement.

DJManas commented 2 years ago

For flask with debugpy it should be repaired using pull request #576

Barmaley0 commented 2 months ago

Missing launch request or attach request before configuration done request.

Error: Failed to find Flask application or factory in module 'app'. Use 'FLASK_APP=app:name' to specify one.

The problem is that flask cannot find the derrictory and even if you specify it, it does not work and gives an error. As soon as you fall into the derritory with the app.py file, everything starts fine.