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
492 stars 46 forks source link

Debugpy - Windows #118

Closed msMatix closed 3 months ago

msMatix commented 10 months ago

Hello, I wanted to debug my application on windows with nvim-dap-python. However, I get the following warning message: The debug adapter did not respond. Either the adapter is slow (then wait and ignore) or there is a problem with your adapter in the 'python' configuration.

Furthermore I get with the cmd DapShowLog the following hint:

[ ERROR ] 2023-09-19T09:22:46Z+0200 ] ...ppData/Local/nvim-data/lazy/nvim-dap/lua/dap/session.lua:1431 ]    "stderr"    {
  command = "C:\\Users\\xxx\\AppData\\Local\\nvim-data\\mason\\bin\\debugpy-adapter.CMD",
  type = "executable"
}   "^C"

However, my configuration works fine on my Linux system. I have now seen in some hints that there are issues with windows.

My configuration is as follows:

return {
  {
    "mfussenegger/nvim-dap",
    optional = true,
    dependencies = {
      "mfussenegger/nvim-dap-python",
      -- stylua: ignore
      keys = {
        { "<leader>dPt", function() require('dap-python').test_method() end, desc = "Debug Method" },
        { "<leader>dPc", function() require('dap-python').test_class() end, desc = "Debug Class" }
      },
      config = function()
        local path = require('mason-registry').get_package('debugpy'):get_install_path()
        if vim.loop.os_uname().sysname:find("Windows") then
          require("dap-python").setup(path .. "dap-python" .. "python")
        else
          require("dap-python").setup(path .. "/venv/bin/python")
        end
      end,
    },
  },
}

Additionally, I use LazyVim framework. My guess is at the path. However, when I print the path in the setup method, I get the correct path. Can someone help me here? If you need more information, please contact me.

Thanks in advance.

heheda123123 commented 6 months ago

I have the same problem

heheda123123 commented 6 months ago

If I use the config from here, it can debug normally, it should be a bug in nvim-dap-python

https://github.com/mfussenegger/nvim-dap/wiki/Debug-Adapter-installation#python

image my config

return {
   {
    "jay-babu/mason-nvim-dap.nvim",
    config = function(_, opts)
      require("mason-nvim-dap").setup(opts)
      local dap = require("dap")
      dap.adapters.python = function(cb, config)
        if config.request == "attach" then
          ---@diagnostic disable-next-line: undefined-field
          local port = (config.connect or config).port
          ---@diagnostic disable-next-line: undefined-field
          local host = (config.connect or config).host or "127.0.0.1"
          cb({
            type = "server",
            port = assert(port, "`connect.port` is required for a python `attach` configuration"),
            host = host,
            options = {
              source_filetype = "python",
            },
          })
        else
          cb({
            type = "executable",
            command = "C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python311\\pythonw.exe",
            args = { "-m", "debugpy.adapter" },
            options = {
              source_filetype = "python",
            },
          })
        end
      end
      dap.configurations.python = {
        {
          -- The first three options are required by nvim-dap
          type = "python", -- the type here established the link to the adapter definition: `dap.adapters.python`
          request = "launch",
          name = "Launch file",

          -- Options below are for debugpy, see https://github.com/microsoft/debugpy/wiki/Debug-configuration-settings for supported options

          program = "${file}", -- This configuration will launch the current file if used.
          pythonPath = function()
            -- debugpy supports launching an application with a different interpreter then the one used to launch debugpy itself.
            -- The code below looks for a `venv` or `.venv` folder in the current directly and uses the python within.
            -- You could adapt this - to for example use the `VIRTUAL_ENV` environment variable.
            local cwd = vim.fn.getcwd()
            if vim.fn.executable(cwd .. "/venv/Scripts/pythonw.exe") == 1 then
              return cwd .. "/venv/Scripts/pythonw.exe"
            elseif vim.fn.executable(cwd .. "/.venv/Scripts/pythonw.exe") == 1 then
              return cwd .. "/.venv/Scripts/pythonw.exe"
            else
              return "C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python311\\pythonw.exe"
            end
          end,
        },
      }
    end,
  },

}
akthe-at commented 5 months ago

If I use the config from here, it can debug normally, it should be a bug in nvim-dap-python

https://github.com/mfussenegger/nvim-dap/wiki/Debug-Adapter-installation#python

image my config

return {
   {
    "jay-babu/mason-nvim-dap.nvim",
    config = function(_, opts)
      require("mason-nvim-dap").setup(opts)
      local dap = require("dap")
      dap.adapters.python = function(cb, config)
        if config.request == "attach" then
          ---@diagnostic disable-next-line: undefined-field
          local port = (config.connect or config).port
          ---@diagnostic disable-next-line: undefined-field
          local host = (config.connect or config).host or "127.0.0.1"
          cb({
            type = "server",
            port = assert(port, "`connect.port` is required for a python `attach` configuration"),
            host = host,
            options = {
              source_filetype = "python",
            },
          })
        else
          cb({
            type = "executable",
            command = "C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python311\\pythonw.exe",
            args = { "-m", "debugpy.adapter" },
            options = {
              source_filetype = "python",
            },
          })
        end
      end
      dap.configurations.python = {
        {
          -- The first three options are required by nvim-dap
          type = "python", -- the type here established the link to the adapter definition: `dap.adapters.python`
          request = "launch",
          name = "Launch file",

          -- Options below are for debugpy, see https://github.com/microsoft/debugpy/wiki/Debug-configuration-settings for supported options

          program = "${file}", -- This configuration will launch the current file if used.
          pythonPath = function()
            -- debugpy supports launching an application with a different interpreter then the one used to launch debugpy itself.
            -- The code below looks for a `venv` or `.venv` folder in the current directly and uses the python within.
            -- You could adapt this - to for example use the `VIRTUAL_ENV` environment variable.
            local cwd = vim.fn.getcwd()
            if vim.fn.executable(cwd .. "/venv/Scripts/pythonw.exe") == 1 then
              return cwd .. "/venv/Scripts/pythonw.exe"
            elseif vim.fn.executable(cwd .. "/.venv/Scripts/pythonw.exe") == 1 then
              return cwd .. "/.venv/Scripts/pythonw.exe"
            else
              return "C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python311\\pythonw.exe"
            end
          end,
        },
      }
    end,
  },

}

Using this config, and changing the paths to reflect my local machine resulted in being able to debug normally, without any external shell window from opening up. Thank YOU

nlahmi commented 3 months ago

Just ran into this issue as well. @akthe-at 's response helped point me in the right direction, but ultimately is not very portable. I managed to solve it with https://github.com/linux-cultist/venv-selector.nvim , just make sure to enable the dap_enabled option (and to activate a venv before debugging your code)