mfussenegger / nvim-dap

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

DAP attach request calls executable #999

Closed steve21168 closed 11 months ago

steve21168 commented 11 months ago

Debug adapter definition and debug configuration

Copied straight from the docs:

dap.adapters.ruby = function(callback, config)
  callback {
    type = "server",
    host = "127.0.0.1",
    port = "9797",
    executable = {
      command = "bundle",
      args = { "exec", "rdbg", "-n", "--open", "--port", "9797",
        "-c", "--", "bundle", "exec", config.command, config.script,
      },
    },
  }
end

dap.configurations.ruby = {
  {
    type = "ruby",
    name = "debug current file",
    request = "attach",
    localfs = true,
    command = "ruby",
    script = "${file}",
  },
}

Debug adapter version

d17d1bba23ec72a157bd183c57840c39e323f515

Steps to Reproduce

  1. Create a ruby file foo.rb
  2. Start rdbg with remote port: rdbg -O --port 9797 -c -- ruby foo.rb
  3. :DapContinue to trigger the dap attach request

Expected Result

I would expect that since I'm making an attach request and not a launch request that the executable in the adapter configuration would not be called. Because it is called the executable errors. The attach portion works as intended.

Actual Result

I see an error that the executable application crashed in the vim cmd line window and then I can see an error when I open the REPL. The crash makes sense, as it was not intended to run the executable command in this context. The attach works fine as it worked as intended.

The issue is that from my understanding and attach request should only use the server portion to connect to the existing dap server.

mfussenegger commented 11 months ago

I would expect that since I'm making an attach request and not a launch request that the executable in the adapter configuration would not be called.

The configuration & request type doesn't control how to connect to the adapter, but rather it tells the adapter what it should do. The adapter definition controls how to connect to the adapter.

If you include a executable block it launches an executable.

nvim-dap can't be smart about this and assume that if you used a attach, you don't want an executable because some debug adapters may actually require the executable. Often a debug adapter is used to act as an adapter from DAP to another protocol. E.g. in Java/JVM the communication looks like this:

 ┌──────────┐ DAP   ┌─────────────┐ JDWP    ┌────┐
 │ nvim-dap ├──────►│debug adapter│───────► │JVM │
 └──────────┘       └─────────────┘         └────┘

If you know that you're connecting to an already running debug adapter, you need to modify the adapter accordingly. E.g. something like this:

dap.adapters.ruby = function(callback, config)
  if config.request == "attach" then
    callback {
      type = "server",
      -- or use host/port from config
      host = "127.0.0.1",
      port = "9797",
    }
  else
    callback {
      type = "server",
      host = "127.0.0.1",
      port = "9797",
      executable = {
        command = "bundle",
        args = { "exec", "rdbg", "-n", "--open", "--port", "9797",
          "-c", "--", "bundle", "exec", config.command, config.script,
        },
      },
    }
  end
end