question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nvim-dap, Pytest, Debugpy and Docker

See original GitHub issue

Firstly, this isn’t an issue with the plugin at all, more of a general question on being able to use vim-ultest to debug remotely into a Docker container.

With my current config (for reference rather than to understand), I use a pretty hefty command to pass to the command line which triggers Docker to wait for any feedback from nvim-dap:

docker-compose -f "./docker-compose.yml" exec -T -w /usr/src/app debug python -m debugpy --listen ' ..
                debug_host .. ':' .. debug_port .. ' --wait-for-client -m pytest ' .. test_method[1]

Where test_method is the test name for the nearest test (something I pinched from vim-test). Using the instructions in the vim-ultest docs, combined with my previous setup, I form the following:

require("ultest").setup({
        builders = {
            ['python#pytest'] = function(cmd)
                local debug_host = '0.0.0.0'
                local debug_port = 5678

                local test_method = fn['test#python#pytest#build_position']('nearest', {
                    file = fn['expand']('%'),
                    line = fn['line']('.'),
                    col = fn['col']('.')
                })

                local args = 'docker-compose -f "./docker-compose.yml" exec -T -w /usr/src/app debug python -m debugpy --listen ' .. debug_host .. ':' .. debug_port .. ' --wait-for-client -m pytest ' .. test_method[1]
                return {
                    dap = {
                        type = "python",
                        request = "attach",
                        connect = {
                            host = debug_host,
                            port = debug_port
                        },
                        args = args,
                        mode = "remote",
                        name = "Remote Attached Debugger",
                        cwd = fn.getcwd(),
                        pathMappings = {{
                            localRoot = fn.getcwd(), -- Wherever your Python code lives locally.
                            remoteRoot = "/usr/src/app" -- Wherever your Python code lives in the container.
                        }}
                    }
                }
            end
        }
    })

When I run this with UltestDebugNearest I get Invalid adapter: nil which is an nvim-dap error. Which seems odd as I use it with nvim-dap in my previous setup.

Is there anything obvious I may be overlooking? Granted I need to actually make use of the vim-ultest cmd function to pass to nvim-dap.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
rcarrigacommented, Apr 28, 2021

That’s great to hear! 😁 As an alternative to the wait, could you add the -d flag to the exec command to detach? If that works OK (provided debugpy doesn’t require an interactive session for some reason) then it should be as simple as swapping

            g.debug_job_id = fn.jobstart(docker_cmd)

with

            g.debug_job_id = fn.system(docker_cmd)

which will block until it returns.

I’d definitely like to add this to the wiki btw!

1reaction
rcarrigacommented, Apr 24, 2021

So I’m not too familiar with the docker side of nvim-dap but to me it looks like config you’re returning to vim-ultest is the wrong one.

From what I can tell you should be running your docker command as a separate job (using vim.loop.spawn, jobstart or whatever) and then return the pythonAttachConfig from the example in the docs. So it’d be something like

require("ultest").setup(
  {
    builders = {
      ["python#pytest"] = function(cmd)
        local docker_cmd =
          'docker-compose -f "./docker-compose.yml" exec -T -w /usr/src/app debug python -m debugpy --listen 0.0.0.0:5678 --wait-for-client -m ' ..
          table.concat(cmd, " ")

        -- You can attach output handlers but anything needed by vim-ultest should be caught by the "attach" adapter
        vim.fn.jobstart(docker_cmd)

        return {
          dap = {
            type = "python",
            request = "attach",
            connect = {
              port = 5678, -- Need to open bind this port to your container
              host = "0.0.0.0"
            },
            mode = "remote",
            name = "Remote Attached Debugger",
            cwd = vim.fn.getcwd(),
            pathMappings = {
              {
                localRoot = vim.fn.getcwd(),
                remoteRoot = "/usr/src/app" -- Likely need to change this
              }
            }
          }
        }
      end
    }
  }
)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Execute test runners remotely in docker containers #6 - GitHub
I would like test_method() to execute the test runner in running a docker ... as I explore Python, Pytest and Docker with nvim-dap...
Read more >
Debugpy + Nvim-DAP in a Docker container - help getting this ...
I've been trying to get Debugpy working with Nvim-DAP from inside a Docker container. I think I am close, but it's not really...
Read more >
Neovim for Beginners — Python Remote Debugging - alpha2phi
Neovim for Beginners — Python Remote Debugging. In this article, we will learn how to debug a Python application running inside a Docker...
Read more >
How can I make a dap configuration to launch the debugger ...
I want to debug my tests with python. I use the pytest module. Normally, to just run the tests, I do python -m...
Read more >
Luca's literate Emacs config
... dap-mode; pytest; jupyter; evil-lisp state; Adjust window size (transient) ... quotes were breaking my VIM object detection in the rest of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found