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

feat: add cwd option to setup opts #170

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

devxpain
Copy link

@devxpain devxpain commented Nov 30, 2024

This commit introduces a cwd option to dap-python.setup.opts, enabling users to specify the working directory for debug sessions.

  • Useful for projects with complex directory structures.
  • Default value is nil to preserve existing behavior.

Current Configuration

Here is my current nvim-dap-python setup:

return {
    "mfussenegger/nvim-dap-python",
    dependencies = "mfussenegger/nvim-dap",
    ft = "python",
    config = function()
      require("dap-python").setup()
    end,
}

Issue Faced

Given the following project structure:

.
├── examples
│   └── generate_signature.py
└── keys
    ├── private_key.pem
    └── public_key.pem

When I open Neovim in the project folder, load examples/generate_signature.py, and run the debugger using require("dap").continue(), I encounter the following error:

with open("keys/private_key.pem", "r") as file:     ■ Thread stopped due to exception of type FileNotFoundError (unhandled)  Description: [Errno 2] No such file or directory: 'keys/private_key.pem'

This happens because debugpy defaults the working directory (cwd) to the directory containing the file being debugged (./examples) rather than the Neovim working directory (vim.fn.getcwd()). As a result, relative paths like keys/private_key.pem are resolved incorrectly.

Proposed Solution

To resolve this issue, I propose adding an optional cwd parameter to the dap-python.setup.opts configuration. This option will allow users to explicitly specify the working directory for debugging sessions. By default, debugpy uses the directory of the file being debugged as the working directory, but with this change, users can customize the cwd to match Neovim’s current working directory (vim.fn.getcwd()), ensuring that relative paths are resolved correctly.

Changes:

  1. Add a cwd option to the dap-python.setup.opts configuration.
  2. The cwd option can be set to a function that dynamically returns the desired directory. If the option is not set, the default behavior (using the file's directory) will be maintained.

Updated nvim-dap-python configuration:

return {
    "mfussenegger/nvim-dap-python",
    dependencies = "mfussenegger/nvim-dap",
    ft = "python",
    config = function()
        require("dap-python").setup(nil, {
            cwd = function()
                return vim.fn.getcwd()  -- Set cwd to Neovim's current working directory
            end
        })
    end,
}

This commit introduces a `cwd` option to `dap-python.setup.opts`, enabling users to specify the working directory for debug sessions.

- Useful for projects with complex directory structures.
- Default value is `nil` to preserve existing behavior.
@devxpain devxpain changed the title feat(dap-python): add cwd option to setup opts feat: add cwd option to setup opts Nov 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant