简体   繁体   English

VSCode 如何找到这个 Go 掉毛问题,我该如何忽略它?

[英]How is VSCode finding this Go linting Problem and how do I ignore it?

I'm setting up linting with golangci-lint in my Go project.我在我的 Go 项目中使用golangci-lint设置 linting。 I have a file generated by go-bindata that VSCode is listing the following under the Problems tab:我有一个由go-bindata生成的文件,VSCode 在“问题”选项卡下列出了以下内容:

assets/assets.go: redundant type from array, slice, or map composite literal (simplifycompositelit) assets/assets.go:来自数组、切片或 map 复合文字的冗余类型(simplifycompositelit)

I can't seem to get rid of it.我似乎无法摆脱它。 It's not a compiler error and I'll be re-running go-bindata from time to time so I don't want to make a habit of modifying generated code.这不是编译器错误,我会不时重新运行go-bindata ,所以我不想养成修改生成代码的习惯。

Right now, with the configuration below, I can't make VSCode stop reporting this error.现在,通过下面的配置,我无法让 VSCode 停止报告这个错误。 If I run golangci-lint run./... in the root of the workspace I get no output.如果我在工作区的根目录中运行golangci-lint run./... ,我不会得到 output。 I can provide my linting config if needed but VSCode seems to be running something else.如果需要,我可以提供我的 linting 配置,但 VSCode 似乎正在运行其他东西。 How do I figure out what's reporting this error and how do I make it stop reporting anything for the file assets/assets.go in this one workspace?如何确定报告此错误的内容以及如何使其停止报告此工作区中文件assets/assets.go的任何内容?

Here's Go-related vscode settings:这是 Go 相关的 vscode 设置:

{
  "go.formatTool": "gofmt",
  "go.lintTool": "golangci-lint",
  "go.liveErrors": {
    "enabled": true,
    "delay": 500
  },
  "go.lintOnSave": "workspace",
  "editor.codeActionsOnSave": {
    "source.organizeImports": true
  },
  "go.useLanguageServer": true,
  "go.languageServerExperimentalFeatures": {
    "diagnostics": true,
    "documentLink": true
  },
}

Here's the line in question even with a nolint comment to show it's not behaving as expected.这是有问题的行,即使有一个 nolint 评论表明它的行为不像预期的那样。 If it were golangci-lint outputting this, the nolint would prevent the warning from showing.如果是 golangci-lint 输出这个,则 nolint 会阻止显示警告。 I reloaded the window and closed/reopened vscode to be sure the change was noticed.我重新加载了 window 并关闭/重新打开 vscode 以确保注意到更改。

linter 发出此警告的来源的图片

After reproducing locally, it seems this message comes from gopls , as disabling gopls silences the message.在本地复制后,这条消息似乎来自gopls ,因为禁用gopls会使消息静音。 There are a couple of related complaints/issues on the Go issue tracker: Go 问题跟踪器上有几个相关的投诉/问题:

Neither offers an actual solution.两者都没有提供实际的解决方案。

However, this issue on the vscode-go repo, provides a work-around.但是, vscode-go repo 上的这个问题提供了一种解决方法。 In your VSCode config, add the gopls.analyses.simplifycompositelit key, with a value of false :在您的 VSCode 配置中,添加gopls.analyses.simplifycompositelit键,其值为false

    "gopls": {
        "analyses": {
            "simplifycompositelit": false
        },
    }

Of course, this disables it for all projects, not just generated files, but if you're also using golangci-lint , it can be configured to catch the same types of errors, and can be configured on a more granular basis, so that you won't miss the same class of errors in non-generated code.当然,这会为所有项目禁用它,不仅是生成的文件,而且如果您还使用golangci-lint ,它可以配置为捕获相同类型的错误,并且可以在更细粒度的基础上进行配置,以便您不会错过非生成代码中相同的 class 错误。

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

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