mfussenegger / nvim-dap

Debug Adapter Protocol client implementation for Neovim
GNU General Public License v3.0
5.17k stars 180 forks source link

program-dependant cwd setting (dotnet config) #1035

Closed Amleto closed 9 months ago

Amleto commented 10 months ago

Problem Statement

dotnet debugging should have the cwd of the debug target set to be the same directory that contains the dotnet dll. This is especially important for asp.net core where an appsettings.json file is expected to be located in the cwd when the server is launched.

This is difficult to configure 'automatically' since the config.program needs to be set dynamically and, as far as I understand, the order of evaluation of cwd.program() and config.cwd() is unspecified. Using the metatable + __call also doesn't help for the same reason.

Possible Solutions

config.cs = {
  {
    type = "coreclr",
    name = "launch - netcoredbg",
    request = "launch",
    program = func(),
    cwd = "$programFolder",
  },
}

Considered Alternatives

I tried using metatable and __call in the config that would inject the config instance into a function for program, and once the value for program was determined it would assign the parent dir to config.cwd, but that config instance is lost somewhere around line config = vim.tbl_map(expand_config_variables, config) in dap.lua.

mfussenegger commented 10 months ago

You could set both program and cwd in the __call as it allows you to return a new configuration table.

Amleto commented 10 months ago

I tried doing that, in conjunction with async calls with a telescope picker - described at the end of of the report. The value that is set is lost

ptrkrlsrd commented 10 months ago

I've been looking for the same. Although not 100% what you want, I came up with this solution. It still need some tweaking but it at least allows you to pick a dll from your obj/Debug folders. Note: I haven't gotten it to work, due to an unrelated problem with netcoredbg (DAP Failed command 'configurationDone' : 0x80070005).

            program = function()
            local function get_recursive_dll_files(skip_count)
              local target_folder = "obj/Debug"
              local find_command = 'find . -path "*/' .. target_folder .. '/*.dll" -not -regex ".*\\/(ref|refint)/.*"'

              local files = {}
              local file_map = {}
              for line in io.popen(find_command):lines() do
                local clean_line = string.gsub(line, target_folder .. "/", "")
                local parts = {}
                local count = 0
                local filename = string.match(clean_line, "([^/]+)$")
                for part in string.gmatch(clean_line, "([^/]+)") do
                  if not string.find(part, "^net[0-9\\.]+$") then
                    count = count + 1
                    if count <= skip_count or part == filename then
                      table.insert(parts, part)
                    else
                      table.insert(parts, string.sub(part, 1, 1))
                    end
                  end
                end
                local display_name = table.concat(parts, "/")
                file_map[display_name] = line
                table.insert(files, display_name)
              end
              return files, file_map
            end

            local function get_file_sync()
              local co = coroutine.running()
              local selected_file = nil
              local files, file_map = get_recursive_dll_files(3)

              vim.ui.select(files, {}, function(short_filename)
                selected_file = file_map[short_filename]
                coroutine.resume(co)
              end)

              coroutine.yield()
              return selected_file
            end

            local file = get_file_sync()
            print(file)
            return file
          end
mfussenegger commented 9 months ago

I tried doing that, in conjunction with async calls with a telescope picker - described at the end of of the report. The value that is set is lost

Then there's probably something wrong with how you use it.

See also https://github.com/mfussenegger/nvim-dap/discussions/597#discussioncomment-3098086 for another alternative