简体   繁体   English

在 VSCode 中启用 golangci-lint 的详细选项

[英]Enable detailed options on golangci-lint in VSCode

I'm using golangci-lint.我正在使用 golangci-lint。 By default it disables golint linter.默认情况下,它禁用 golint linter。 To add golint to default linters the command needs a -E golint flag:要将 golint 添加到默认 linters,该命令需要一个-E golint标志:

golangci-lint run -E golint

From the command line that works fine.从运行良好的命令行。
But now I integrated golangci-lint with VSCode by adding this option to the settings:但是现在我通过将此选项添加到设置中来将 golangci-lint 与 VSCode 集成:

"go.lintTool":"golangci-lint",

The default linters work fine but when I add a flag to enable golint it stops linting and doesn't return any output at all.默认的 linters 工作正常,但是当我添加一个标志以启用 golint 时,它会停止 linting 并且根本不返回任何 output。 To pass additional flag I followed the golangci-lint documentation and added go.lintFlags :为了传递额外的标志,我遵循了golangci-lint 文档并添加了go.lintFlags

"go.lintFlags": [
  "--enable golint"
]

Note that I don't want to just run golint but rather to enable all default linters in golangci-lint and on top of that to use golint.请注意,我不想只运行 golint,而是要启用 golangci-lint 中的所有默认 linters,并在此基础上使用 golint。

I found it way easier to provide golangci-lint config.我发现提供golangci-lint配置更容易。

"go.lintTool": "golangci-lint",
"go.lintFlags": [
    "-c",
    "~/.dotfiles/.golangci.yml",
    "--issues-exit-code=0"
],

And btw, you don't need to enable all default linters - thay are already enabled (see reference ).顺便说一句,您不需要启用所有默认 linter - 已经启用(请参阅参考资料)。

But if you want to run it via settings.json , you can define (as you did linter flags).但是如果你想通过settings.json运行它,你可以定义(就像你做 linter 标志一样)。 For example next configuration例如下一个配置

"go.lintTool": "golangci-lint",
"go.lintFlags": [
    "-E", "dogsled",
    "-E", "gochecknoglobals"
],

Applied to the应用于

package main

func s(i int) (int, int, int, int, int) {
    return -1, -2, -3, -4, -5
}

func fpl() {
}

var i = 0

func main() {
    _, _, _, _, _ = s(i)
}

will result in:将导致:

...>Finished running tool: /Users/0_o/go/bin/golangci-lint run -E dogsled -E gochecknoglobals --print-issued-lines=false --out-format=colored-line-number --issues-exit-code=0
.../main.go:7:6 `fpl` is unused (deadcode)
.../main.go:13:2 declaration has 5 blank identifiers (dogsled)
.../main.go:10:5 `i` is a global variable (gochecknoglobals)

add a.golangci.toml (or.yml or.json) to the root of the repo and set up the configuration via the file.将 a.golangci.toml (or.yml or.json) 添加到 repo 的根目录并通过文件设置配置。 You can also put that file in your $HOME directory if you want it to run across all repos.如果您希望它在所有存储库中运行,您也可以将该文件放在 $HOME 目录中。 That's a lot better than trying to type in a bunch of CLI flags into your VSCode configuration.这比尝试在 VSCode 配置中输入一堆 CLI 标志要好得多。 golangci-lint will read that file automatically and do the right thing. golangci-lint 将自动读取该文件并做正确的事情。 And that way if you run it from the command line, it'll use the same configuration.这样,如果您从命令行运行它,它将使用相同的配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM