在vscode-go中,是否有一种方法可以运行go:generate语句的本地工具版本?

huangapple go评论102阅读模式
英文:

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

问题

在许多项目中,我有一个名为tools/tools.go的文件,它允许我使用本地版本的工具(参考链接:https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module)。

我还在我的代码中使用go:generate语句来自动生成模拟文件,使用的工具是https://github.com/golang/mock/mockgen。我在项目的bin文件夹下有一个本地版本的mockgen,我可以通过运行类似PATH=./bin:${PATH} go generate ./...的命令来生成项目的所有模拟文件。

Vscode-go可以识别代码中的go:generate语句,并为我们生成代码。

我想知道是否有办法让vscode-go使用我在bin目录下的本地版本的mockgen(通过gopls.generate)运行。

通过设置go.alternateTools,vscode-go允许我们为扩展使用的工具(如dlv)指定替代路径。我尝试为mockgen配置该选项,但没有任何变化。我猜测这是因为vscode-go通过gopls.generate运行mockgen,而不是直接运行mockgen。但这只是我的猜测。

如果我在$PATH中没有mockgen二进制文件,我会得到以下错误:

[Info  - 5:44:18 PM] 2021/08/08 17:44:18 xxxxxxx.go:19: running "mockgen": exec: "mockgen": 在 $PATH 中找不到可执行文件

	operation="generate"

有人有任何想法吗?😅

编辑:请参考@Zyl的评论这里,那里有一个非常好的替代方法。

英文:

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中,是否有一种方法可以运行go:generate语句的本地工具版本?

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.

答案1

得分: 3

我今天遇到了同样的问题。找到了这个解决方案,对我有效,如果将来有人需要的话:

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

然后在包含"go:generate"注释的文件所在的目录中运行:

go generate
英文:

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

huangapple
  • 本文由 发表于 2021年8月8日 17:01:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/68699272.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定