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

Viewed 1392

I'm setting up linting with golangci-lint in my Go project. I have a file generated by go-bindata that VSCode is listing the following under the Problems tab:

assets/assets.go: redundant type from array, slice, or map composite literal (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.

Right now, with the configuration below, I can't make VSCode stop reporting this error. If I run golangci-lint run ./... in the root of the workspace I get no output. I can provide my linting config if needed but VSCode seems to be running something else. 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?

Here's Go-related vscode settings:

{
  "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. If it were golangci-lint outputting this, the nolint would prevent the warning from showing. I reloaded the window and closed/reopened vscode to be sure the change was noticed.

picture of the source where the linter is making this warning

1 Answers

After reproducing locally, it seems this message comes from gopls, as disabling gopls silences the message. There are a couple of related complaints/issues on the Go issue tracker:

Neither offers an actual solution.

However, this issue on the vscode-go repo, provides a work-around. In your VSCode config, add the gopls.analyses.simplifycompositelit key, with a value of 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.

Related