Debugging or modifying go dependency packages

When debugging a program it sometimes occurs, that one needs to debug also one of it’s dependencies. By fetching all dependencies into a vendor folder, one can easily also modify those dependencies. In this tutorial I’m gonna show you how you can achieve this in a plain Linux environment and in VS Code.

The vendor folder

This is step one of 1. Fetch all dependencies into the vendor folder:

$ go mod vendor

Congratulations! Now all the project dependencies are in the vendor folder. This is where you can apply local changes and debug your application dependencies. Now you only need to tell go, to use this folder as module folder:

$ go build -mod=vendor ./...
$ go run -mod=vendor main.go

Note: Since go 1.14 the -mod=vendor is set by default, so this might not be necessary anymore.

alternative: GOMODCACHE

Go modules are being search from $GOPATH/pkg/mod. To use a different location, you can set the GOMODCACHE variable (see this page for more details).

Using the vendor folder in VS Code

In my case I had to manually set the GOFLAGS to use -mod=vendor otherwise my launch configuration would not honor the present vendor folder:

"env": {"GOFLAGS":"-mod=vendor"},

This is how a concrete launch configuration then looks in practice:

{
    "name": "Launch openqa-mon with rabbitmq",
    "type": "go",
    "env": {"GOFLAGS":"-mod=vendor"},
    "request": "launch",
    "mode": "debug",
    "program": "${workspaceFolder}/cmd/openqa-mon/",
    "args": ["-mpfbc10b", "https://duck-norris/tests/10346", "--rabbit", "../../rabbitmq.conf"]
},

The code navigation and setting of breakpoints however does not work, as VS Code is not able to honor the vendor folder by default in this configuration. Suggestions to improve this are welcome, if you know a better way of doing this.

Licensed under CC BY-NC-SA 4.0