Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 2 commits into from

Conversation

mfussenegger
Copy link
Owner

See microsoft/debugpy#655
This requires mfussenegger/nvim-dap#226

recording.mp4

@meijieru
Copy link
Contributor

meijieru commented Jan 6, 2022

It seems very useful, is there anything blocking it?

@mfussenegger
Copy link
Owner Author

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.

@norseghost
Copy link

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.

@tkreuder
Copy link

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
Copy link

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

@jsatt
Copy link

jsatt commented Aug 17, 2022

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

@mfussenegger
Copy link
Owner Author

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

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

@Mte90
Copy link

Mte90 commented Jan 16, 2024

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

@mfussenegger
Copy link
Owner Author

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

See:

@Mte90
Copy link

Mte90 commented Jan 18, 2024

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 (mfussenegger/nvim-dap#853 says early prototype).

@Mte90
Copy link

Mte90 commented Jan 19, 2024

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.

@mfussenegger
Copy link
Owner Author

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

Repository owner locked as resolved and limited conversation to collaborators Jan 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants