How to debug go backend on windows?

I’m currently developing a grafana datasource with http backend plugin!
I’m developing on a Windows machine, with a WSL2 Alpine distribution setup. The Alpine distro has go, nodejs etc. set up, aswell as docker.
I’m using VSCode to develop on my windows machine.

I’ve successfully scaffolded the plugin and made some changes in the react frontend.
Since I’m now at the stage of developing my go backend, I’ve wanted to debug to see what is going on, however I’m not able to get breakpoints working in VSCode.
What I’ve tried:

  • Extended Dockerfile to install delve debugger

    ARG grafana_version=latest
    ARG grafana_image=grafana-enterprise
    
    FROM grafana/${grafana_image}:${grafana_version}
    
    # Make it as simple as possible to access the grafana instance for development purposes
    # Do NOT enable these settings in a public facing / production grafana instance
    ENV GF_AUTH_ANONYMOUS_ORG_ROLE "Admin"
    ENV GF_AUTH_ANONYMOUS_ENABLED "true"
    ENV GF_AUTH_BASIC_ENABLED "false"
    # Set development mode so plugins can be loaded without the need to sign
    ENV GF_DEFAULT_APP_MODE "development"
    
    # Inject livereload script into grafana index.html
    USER root
    RUN sed -i 's/<\/body><\/html>/<script src=\"http:\/\/localhost:35729\/livereload.js\"><\/script><\/body><\/html>/g' /usr/share/grafana/public/views/index.html
    
    # Install Go and Delve for backend debugging
    RUN apk update
    RUN curl -L https://golang.org/dl/go1.18.linux-amd64.tar.gz > go1.18.linux-amd64.tar.gz
    RUN rm -rf /usr/local/go && \
        tar -C /usr/local -xzf go1.18.linux-amd64.tar.gz
    RUN touch README; printf "~~~~~~ START THE DLV SERVER WITH THIS COMMAND BEFORE RUNNING IDE DEBUGGER ~~~~~~ \r\ndlv attach --headless --listen=:2345 PID\r\n\r\n" >> README
    RUN echo "export PATH=$PATH:/usr/local/go/bin:~/go/bin" >> ~/.bashrc
    RUN echo "cat ~/README" >> ~/.bashrc
    
    RUN /usr/local/go/bin/go install github.com/go-delve/delve/cmd/dlv@latest
    
  • Extended docker-compose-yaml to not only mount the dist and provisioning directories, but also the pkg directory so delve has acces to the code and to expose the port to connect to delve

    version: '3.0'
    
    services:
      grafana:
        container_name: 'grafana'
        platform: 'linux/amd64'
        build:
          context: ./.config
          args:
            grafana_image: ${GRAFANA_IMAGE:-grafana-enterprise}
            grafana_version: ${GRAFANA_VERSION:-10.0.3}
        cap_add:
          - SYS_PTRACE # To allow debugging with Delve
        security_opt:
          - seccomp:unconfined # To allow debugging with Delve
        ports:
          - 3000:3000/tcp # Grafana instance port
          - 2345:2345 # Delve debugger port
        volumes:
          - ./dist:/var/lib/grafana/plugins/company-mongodbconnector-datasource
          - ./provisioning:/etc/grafana/provisioning
          - ./pkg:/var/lib/grafana/plugins/company-mongodbconnector-datasource/pkg
    
    
  • Setup a lauch profile in VS Code to attach to delve via remote

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug in Container",
      "type": "go",
      "request": "attach",
      "mode": "remote",
      "remotePath": "/var/lib/grafana/plugins/company-mongodbconnector-datasource/pkg",
      "port": 2345,
      "host": "127.0.0.1",
      "apiVersion": 2,
      "trace": "verbose",

    }
  ]
}

The VSCode logfile states, that breakpoints have been set successfully, however they never break execution. All that happens is, that my datasource doesn’t load anymore, when delve is attached to the go plugin.

Is there a better way to debug on Windows machine?

I managed to make it work by setting the “cwd” and “remotePath” properties correctly