Skip to content

Instantly share code, notes, and snippets.

@abidibo
Last active October 27, 2023 11:31
Show Gist options
  • Save abidibo/d7e0854f896f29f3c6523100a624abf1 to your computer and use it in GitHub Desktop.
Save abidibo/d7e0854f896f29f3c6523100a624abf1 to your computer and use it in GitHub Desktop.
Neovim DAP

Python

On local machine

  1. Install debugpy in application virtualenv
  2. Run django server with debugpy: python -m debugpy --listen 5678 manage.py runserver --noreload 0.0.0.0:8000
  3. In neovim F5 (attach to 127.0.0.1, port 5678)

In docker container

  1. Install debugpy in application virtualenv
  2. Change command in docker-compose:
command: bash -c "source ../../venv/bin/activate && pip install -r requirements/local.txt && python manage.py migrate && python -m debugpy --listen 0.0.0.0:5678 manage.py runserver 0.0.0.0:8000"
  1. Add security options in docker-compose, app:
security_opt:
  - seccomp:unconfined
cap_add:
  - SYS_PTRACE
  1. Add port mapping 5678 to 5678 in docker-compose, app section
  2. Add to local .nvim.lua:
vim.cmd([[
    autocmd FileType python set ft=python.django
    autocmd FileType html set ft=htmldjango
]])

table.insert(require('dap').configurations['python.django'], {
    type = 'python',
    request = 'attach',
    name = 'Attach remote docker',
    connect = function()
        local host = vim.fn.input('Host [127.0.0.1]: ')
        host = host ~= '' and host or '127.0.0.1'
        local port = tonumber(vim.fn.input('Port [5678]: ')) or 5678
        return { host = host, port = port }
    end,
    cwd = vim.fn.getcwd(),
    pathMappings = {
        {
            localRoot = vim.fn.getcwd(),                                        -- Wherever your Python code lives locally.
            remoteRoot = "/home/app/nova-americana/nova-americana",             -- Wherever your Python code lives in the container.
        },
    },
})

change remoteRoot to your project path in container

  1. In neovim F5 (attach to remote docker 127.0.0.1, port 5678)

GO

Be sure delve is installed in Mason!!

In docker container

Create a debug container

FROM golang:1.20

ENV PORT=8081

WORKDIR /go/src/app

# use cache
COPY src/go.mod .
COPY src/go.sum .

COPY src .

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

ENTRYPOINT /go/bin/dlv

Create a debug service

  debug:
    build:
      context: .
      dockerfile: Dockerfile.debug
    security_opt:
      - seccomp:unconfined
    cap_add:
      - SYS_PTRACE
    entrypoint: dlv
    restart: unless-stopped
    ports:
      - 9004:9004
      - 8081:8081
    command:
      - "debug"
      - "--headless"
      - "--listen=:9004"
      - "--api-version=2"
      - "--accept-multiclient"
      - "--log"
      #- "--log-output=debugger,rpc,dap"
      - "./main.go"
    env_file:
      - .env.dev
    volumes:
        - ./src:/go/src/app/

nvim-dap configuration

dap.adapters.go = {
    type = "server",
    port = '9004',
    executable = {
      command = "dlv",
      args = { "dap", "-l", "127.0.0.1:9004"},
    },
}

nvim-dap-go configuration:

return {
    'leoluz/nvim-dap-go',
    config = function()
        require('dap-go').setup {
            dap_configurations = {
                {
                    type = "go",
                    name = "Attach remote",
                    mode = "remote",
                    request = "attach",
                    -- tell which host and port to connect to
                    connect = {
                        host = "127.0.0.1",
                        port = "9004"
                    },
                    substitutepath = { {
                        from = vim.fn.getcwd(),
                        to = "/go/src/app",
                    } },
                },
            },
            delve = {
                port = "9004"
            },
        }
    end
}

Open vim from src folder (go.mod level)

F5 -> attach remote

Perform requests to port 8081

Javascript

nvim dap configuration

dap.configurations.javascriptreact = {
  {
    type = 'chrome';
  },
}
dap.adapters.chrome = {
  type = "executable",
  command = "node",
  args = { vim.fn.stdpath("data") .. "/mason/packages/chrome-debug-adapter/out/src/chromeDebug.js" },
}

mason nvim dap configuration:

return {
    "jay-babu/mason-nvim-dap.nvim",
    config = function ()
        require('mason-nvim-dap').setup {
          automatic_setup = true,
          ensure_installed = {
            'chrome',
            'js',
          },
          handlers = {},
        }
    end
}

Start chrome with open remote debugging port

$ google-chrome --remote-debugging-port=9222

Inside vim set a breakpoint, then F5 - chrome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment