nvim-dap-python icon indicating copy to clipboard operation
nvim-dap-python copied to clipboard

Add support for custom debugpyAttach event (for multi-session / multiprocessing support)

Open mfussenegger opened this issue 3 years ago • 5 comments

See https://github.com/microsoft/debugpy/issues/655 This requires https://github.com/mfussenegger/nvim-dap/pull/226

https://user-images.githubusercontent.com/38700/124895528-e99b4b80-dfdc-11eb-9793-861c5b5ea457.mp4

mfussenegger avatar Jul 08 '21 09:07 mfussenegger

It seems very useful, is there anything blocking it?

meijieru avatar Jan 06 '22 05:01 meijieru

It seems very useful, is there anything blocking it?

If I remember right it was mainly lacking testing and the documentation. The branch should be working.

mfussenegger avatar Mar 31 '22 20:03 mfussenegger

I've just tried this; and have hit a snag. (this branch and latest nvim-dap)

when attempting to run the debugger on multithreaded code; I get the follwing error:

Error executing vim.schedule lua callback: ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:711: attempt
 to index local 'opts' (a number value)
stack traceback:
        ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:711: in function 'session_defaults'
        ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:734: in function 'connect'
        ...ite/pack/packer/start/nvim-dap-python/lua/dap-python.lua:122: in function 'c'
        ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:685: in function <...nvim/site/pack/packer/star
t/nvim-dap/lua/dap/session.lua:673>

the dap logs seem to indicate attachment to the subprocess;

[ DEBUG ] 2022-05-04T04:55:12Z+0200 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:638 ]	{
  body = {
    reason = "started",
    threadId = 1
  },
  event = "thread",
  seq = 11,
  type = "event"
}
[ DEBUG ] 2022-05-04T04:55:14Z+0200 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua:638 ]	{
  body = {
    connect = {
      host = "127.0.0.1",
      port = 36145
    },
    console = "integratedTerminal",
    module = "pytest",
    name = "Subprocess 53743",
    python = { "/home/martin/.cache/pypoetry/virtualenvs/nordigen-beancount-EOvi3lCy-py3.10/bin/python" },
    pythonPath = "/home/martin/.cache/pypoetry/virtualenvs/nordigen-beancount-EOvi3lCy-py3.10/bin/python",
    request = "attach",
    subProcessId = 53743,
    type = "python"
  },
  event = "debugpyAttach",
  seq = 12,
  type = "event"
}
[ DEBUG ] 2022-05-04T04:55:14Z+0200 ] ...nvim/site/pack/packer/start/nvim-dap/lua/dap/session.lua

but the debugger is just as stuck as with the main branch; and opening the sessions widget only shows the one thread.

norseghost avatar May 04 '22 03:05 norseghost

I receive no error, but I cannot access the server.

dap.log

[ INFO ] 2022-07-13T23:55:10Z+0200 ] ...e/nvim/site/pack/packer/opt/nvim-dap/lua/dap/session.lua:600 ]	"Telemetry"	"ptvsd"
[ INFO ] 2022-07-13T23:55:10Z+0200 ] ...e/nvim/site/pack/packer/opt/nvim-dap/lua/dap/session.lua:600 ]	"Telemetry"	"debugpy"

my config looks like this:

local M = {}

function M.setup(_)
  require("dap-python").setup("/usr/local/Caskroom/miniconda/base/envs/xxx/bin/python")

  table.insert(require("dap").configurations.python, {
    type = "python",
    name = "FastAPI",
    request = "launch",
    module = "uvicorn",
    args = {"app.app.main:app", "--reload", "--ws", "websockets", "--host", "0.0.0.0", "--port", "8001", "--log-level", "debug"},
    envFile = "/Users/torbenkreuder/development/portabiles/backend/.env",
    pythonPath = function() return "/usr/local/Caskroom/miniconda/base/envs/xxx/bin/python" end,
    connect = { host = "0.0.0.0", port = 8001 }


  })
end

return M

Removing "--reload" allows for attaching the debugger, but I would love to use hot reloading. I get the same result when I use vimspector, where it also only works without --reload.

Can I somehow circumvent this issue?

tkreuder avatar Jul 13 '22 22:07 tkreuder

So the subprocess is probably the reason @mfussenegger ? https://github.com/encode/uvicorn/blob/master/uvicorn/config.py#L394

tkreuder avatar Jul 14 '22 05:07 tkreuder

I've been trying for several days at this point to figure out why my code would break at the breakpoint in VSCode but not in nvim and this branch finally fixed the issue for me. I would love to see it merged soon.

I'm using attach to connect to my Django app running under uvicorn in Docker.

This is the config I'm using:

{
    type = 'python',
    request = 'attach',
    name = 'Attach Docker',
    pathMappings = {
      {
        -- localRoot = vim.fn.getcwd(),
        localRoot = '${workspaceFolder}',
        remoteRoot = '/usr/src/app',
      }
    },
    connect = function()
      local host = vim.fn.input('Host [127.0.0.1]: ')
      host = host ~= '' and host or '127.0.0.1'
      local port = tonumber(vim.fn.input('Port [5678]: ')) or 5678
      return { host = host, port = port }
    end;
  }

And this is the command that is executing inside of Docker:

command:
  - python3
  - -m
  - debugpy
  - --listen
  - 0.0.0.0:5678
  - -m
  - uvicorn
  - myapplication.asgi:application
  - --workers=4
  - --host=0.0.0.0
  - --port=8000
  - --reload
  - --log-level=info
  - --lifespan=off

jsatt avatar Aug 17 '22 15:08 jsatt

Closing this as I won't wrap this up in it's current form.

I'm waiting on https://github.com/microsoft/debug-adapter-protocol/issues/79. This will then be implemented in nvim-dap and if debugpy implements it too things should work out of the box.

mfussenegger avatar Oct 09 '22 12:10 mfussenegger

I don't understand if this project at the end got the support for subProcess parameter.

Mte90 avatar Jan 16 '24 16:01 Mte90

The parameter should work yes. nvim-dap gained support for the DAP startDebugging reverse request.

See:

  • https://github.com/mfussenegger/nvim-dap/pull/853
  • https://github.com/mfussenegger/nvim-dap/releases/tag/0.5.0

mfussenegger avatar Jan 18 '24 07:01 mfussenegger

Because I tried with subProcess with Django but doesn't work at all (with the nothreading parameter or not), as I wasn't able to find any information I wasn't sure about the integration (https://github.com/mfussenegger/nvim-dap/pull/853 says early prototype).

Mte90 avatar Jan 18 '24 14:01 Mte90

With doesn't work I mean that launching Django from nvim-dap doesn't show the console output and doesn't run rightly.

Also on internet I don't find anything about the subProcess parameter for debugpy together with Django so I have no hints about something else to try.

Mte90 avatar Jan 19 '24 10:01 Mte90

I use:

        {
            "name": "Django",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/manage.py",
            "args": [
                "runserver"
            ],
            "django": true,
            "console": "integratedTerminal"
        },

In a project and reload works just fine.

If it doesn't work for you, please create a new issue with a clear problem description to avoid https://xyproblem.info/, reproduction steps and log output

mfussenegger avatar Jan 20 '24 08:01 mfussenegger