mfussenegger / nvim-dap-python

An extension for nvim-dap, providing default configurations for python and methods to debug individual test methods or classes.
GNU General Public License v3.0
571 stars 51 forks source link

How to set current working directory ? #101

Closed SamPosh closed 1 year ago

SamPosh commented 1 year ago

In my python file I am opening a file , the file path contains my_tool/input.yaml

/workspaces |__ my_tool | tool.py | input.yaml

I opened the file in neovim and execute the tool.py

f __name__ == "__main__":
    filepath = "my_tool/input.yml"
    with open(filepath, "r") as f:
        input_data = yaml.load(f, Loader=yaml.SafeLoader)

it fails as Thread stopped due to exception of type FileNotFoundError

But if i change the filepath to 'input.yml' then it works. Seems like current working directory is set to file path. Can I modify the cwd? How to do it? Is there an option available for it?

mfussenegger commented 1 year ago

You can set the working directory via a cwd property in the :h dap-configuration. See also :help dap-python.DebugpyLaunchConfig for a list of the options or https://github.com/microsoft/debugpy/wiki/Debug-configuration-settings

And there's also a part in the readme about customizing the default configurations: https://github.com/mfussenegger/nvim-dap-python#custom-configuration

lucaguarro commented 1 year ago

@mfussenegger sorry to open this back up again but if I am understanding correctly the configuration is global, right? Obviously, I want the working directory to be different from python project to python project so is there a way to configure it at the project level?

Thanks.

Edit: So I tried making a .vscode/launch.json file in the root directory of my project that looked like this:

{
   "version": "0.2.0",
   "configurations": [
       {
           "type": "python",
           "request": "launch",
           "name": "Launch",
           "cwd": "/home/luca/Documents/Projects/Head_Hunter_9000"
       }
   ]
}

And I put the following line in my ~/.config/nvim/after/plugin/dappython.lua: require('dap.ext.vscode').load_launchjs()

This did not seem to change my working directory. I feel like I am close though...

Okay I had to add "program": "${file}" to the configuration. Also feel like an idiot b.c. I wasn't using the write debug launcher... So everything is resolved.