Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In vscode-go, is there a way to run the local version of a tool for go:generate statements?

In many projects, I have a tools/tools.go file that allows me to have local versions of the tools I use (reference).

I also have go:generate statements in my code to automate the generation of mock files using https://github.com/golang/mock/mockgen. I have a local version of mockgen under a bin folder of my project and I can generate all the mocks of my project by running something like PATH=./bin:${PATH} go generate ./....

Vscode-go recognizes the go:generate statements in the code and can generate code for us.

vscode-go generate helper

I would like to know if there is a way to get vscode-go to run mockgen (through gopls.generate) using the local version I have under the bin directory.

With the setting go.alternateTools, vscode-go lets us specify an alternate path for the tools used by the extension (i.e. dlv). I tried to configure that option for mockgen but it didn't change anything. My guess is that it does not work because vscode-go runs mockgen through gopls.generate; it does not run mockgen directly. But this is just a guess.

Below is the error I get if I don't have a mockgen binary in my $PATH:

[Info  - 5:44:18 PM] 2021/08/08 17:44:18 xxxxxxx.go:19: running "mockgen": exec: "mockgen": executable file not found in $PATH

    operation="generate"

Does anyone have any idea? 😅

Edit: see @Zyl's comment there for a very good alternative.

like image 600
moewb Avatar asked Oct 15 '25 07:10

moewb


1 Answers

I've run into the same problem today. Found this solution, which worked for me if someone needs this in future:

export GOPATH="$HOME/go" 
export PATH="$GOPATH/bin:$PATH"

And then run (in the directory where file with the "go:generate" comment is located)

go generate
like image 186
Даниэль Сеидов Avatar answered Oct 18 '25 14:10

Даниэль Сеидов



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!