leoluz / nvim-dap-go

An extension for nvim-dap providing configurations for launching go debugger (delve) and debugging individual tests
MIT License
492 stars 78 forks source link

Substitute paths when connecting to remote headless `dlv` server #43

Open nnarayen opened 1 year ago

nnarayen commented 1 year ago

I'm currently using nvim-dap and nvim-dap-go to connect to a headless dlv server running remotely in k8s. Unfortunately, the source code during compilation of the remote binary is different than my local source code, so by default most breakpoint functionality does not work.

As far as I understand, path transformations occur client side and not on the dlv server. I can get this to work via dlv connect on my local machine either with config substitute-path <from> <to> or via the dlv config.yml file. Unfortunately, I haven't found a way to have the nvim dap client perform any path substitutions.

This issue is more of an open question, but do you know if that's currently possible? Thanks in advance!

Build information

Remote dlv server

Delve Debugger
Version: 1.20.1
Build: $Id: 96e65b6c615845d42e0e31d903f6475b0e4ece6e $

nvim-dap: v0.5.0 nvim-dap-go: sha b4ded7de579b4e2a85c203388233b54bf1028816

guruor commented 1 year ago

I am not a user of nvim-dap-go, but came across this issue with custom nvim-dap config. For me I use substitutePath with vscode launch.json, that works pretty well, maybe you can try using that.

launch.json

``` { "version": "0.2.0", "configurations": [ { "name": "Golang: Remote Attach delve launch.json", "type": "go", "debugAdapter": "dlv-dap", "request": "attach", "mode": "remote", "preLaunchTask": "debug-attach", "postDebugTask": "debug-stop", "connect": { "host": "127.0.0.1", "port": 38697 }, "logToFile": true, "cwd": "${workspaceFolder}", "pathMappings": [ { "localRoot": "${workspaceFolder}", "remoteRoot": "/app" } ], "substitutePath": [{ "from": "${workspaceFolder}", "to": "/app" }] } ] } ```

leoluz commented 1 year ago

Unfortunately, I haven't found a way to have the nvim dap client perform any path substitutions.

@nnarayen I am also unaware of such configuration. Have you tried asking in the nvim-dap project?

ryutah commented 1 year ago

I was able to set the substitutionPath from configurations as described at https://github.com/mfussenegger/nvim-dap/issues/905.

It requires some ingenuity as the substitutePath needs to match the remote path, but I have been able to debug applications running on Docker with the configuration below.

local dap_go = require('dap-go')
local dap = require('dap')

dap_go.setup()
table.insert(dap.configurations.go, {
  type = 'delve',
  name = 'Attach remote',
  mode = 'remote',
  request = 'attach',
  substitutePath = {
    { from = '${workspaceFolder}', to = '/app' },
  },
})
dap.adapters.delve = {
  type = 'server',
  host = 'localhost',
  port = '38697'
}

It works like this.

sample

leoluz commented 1 year ago

@nnarayen can you please try the suggestion provided by @ryutah and confirm that it is working for you. Maybe we can improve the docs describing it for users with the same requirements.

gvital3230 commented 11 months ago

Hi, debug in remote container in Kubernetes works fine for me.

Here is the full plugin config for Lazy.

      {
        "leoluz/nvim-dap-go",
        opts = {
          dap_configurations = {
            {
              -- Must be "go" or it will be ignored by the plugin
              type = "go",
              name = "Connect remote",
              request = "attach",
              mode = "remote",
              substitutePath = {
                {
                  from = "${workspaceFolder}",
                  to = "/app",
                },
              },
            },
          },
          delve = {
            port = 2345,
          },
        },
      },

I use 2345 port for communication with dlv, so my command in remote container is

dlv debug --headless --listen=:2345 --log --api-version=2
stale[bot] commented 5 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

ddaniel27 commented 5 months ago

I can confirm that setup given from @ryutah works fine for me to debug in a remote container. In my case I use /wd as my application path in docker, so I have to change it. This is my entirely setup:

Dockerfile:

FROM golang:1.20-bullseye

WORKDIR /wd

COPY go.mod .
COPY go.sum .

RUN go mod download -x

RUN go install github.com/go-delve/delve/cmd/dlv@latest

COPY . .

CMD ["dlv", "debug", "--listen=:34567", "--headless", "--build-flags='-buildvcs=false'"]

nvim setup:

local dap_go = require('dap-go')
local dap = require('dap')

dap_go.setup()
table.insert(dap.configurations.go, {
  type = 'delve',
  name = 'Container debugging (/wd:34567)',
  mode = 'remote',
  request = 'attach',
  substitutePath = {
    { from = '${workspaceFolder}', to = '/wd' },
  },
})

dap.adapters.delve = {
  type = 'server',
  host = 'localhost',
  port = '34567'
}

docker-compose:

version: '3'
services:
  app:
    container_name: my-app
    hostname: my-app
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - '3000:3000'
      - '34567:34567'
    restart: unless-stopped
    volumes:
      - ./:/wd