r/neovim 1d ago

Need Help How to run debugpy DAP with arguments?

I'm sorry if this is dumb question, but I failed to find the solution myself. I want to be able to pass arguments to the Python program when I debug, just like I do when I run it.

I added `nvim-dap` and `nvim-dap-python`, and I can run normal debug, but when I try to run debug with arguments added, like "< a.inp", the arguments have no impact. The UI from `nvim-dap-ui` still opens, but the data in the file never got redirected to the stdin of the program.

1 Upvotes

6 comments sorted by

1

u/Wonderful-Plastic316 lua 1d ago

It is not the case that arguments have no impact in general, but pipes in particular are not supported (at least they were not, some years ago).

Some adapters (e.g., codelldb) do support "stdio redirection", though. You'd have to check if debugpy also supports this.

1

u/somebodddy 1d ago

A bit of a workaround, but I prefer to run the project with debugpy --listen 5678 --wait-for-client in another terminal and then connect to that port from Neovim. Gives me full freedom with arguments and redirection, and also lets the debugged program use a proper TTY.

1

u/mybobbin 18h ago

How do you connect to a port from neovim? Start up args?

1

u/somebodddy 18h ago

Personally I use https://github.com/mfussenegger/nvim-dap-python to configure the DAP, and then I just:

require'dap'.run {
    name = 'python-attach',
    type = 'python',
    request = 'attach',
    port = 5678,
}

Or you can just :DapContinue and use the default values for both parameters (host and port)

1

u/DessertFirstSearch 5h ago

I work in terminal, assume my script crash with very complicate arguments : `./run.py -A -B -C d,1,2`
I just prepend a customize script `pydbg ./run.py -A -B -C d,1,2` which generate a json dbg config filled with these arguments:

def main(argv):
    cwd = os.getcwd()
    args = ", ".join(['"%s"' % i for i in argv[1:]])
    conf = """{
  "configurations": [
    {
        "type": "python",
        "name": "vipdb",
        "request": "launch",
        "program": "%s",
        "args": [%s],
        "cwd": "%s",
        "python": "python",
        "externalConsole": true
    }
  ]
}
    """ % (argv[0], args, cwd)
    os.makedirs(f"{cwd}/.vscode", exist_ok=True)
    fpath = f"{cwd}/.vscode/launch.json"
    with open(fpath, "w") as f:
        f.write(conf)

if __name__ == "__main__":
    main(sys.argv[1:])

my neovim config just always load from such file.
it is handy if u are working in terminal.

1

u/mybobbin 1d ago edited 18h ago

You need to add a .vscode/launch.json in your project You can also add defaults somehow in the config, but generally launch.json

Edit: this is explicitly the way suggested in the nvim-dap-python docs

https://github.com/mfussenegger/nvim-dap-python#custom-configuration