gochecknoglobals icon indicating copy to clipboard operation
gochecknoglobals copied to clipboard

Add support for ignoring generated files

Open benjivesterby opened this issue 3 years ago • 3 comments

Using the guidance here: https://pkg.go.dev/cmd/go/internal/generate

This tool should ignore files which contain the ^// Code generated .* DO NOT EDIT\.$ regex match.

This can be a flag or default, but it should allow for ignoring those files.

benjivesterby avatar Dec 01 '21 19:12 benjivesterby

Interesting. I'm aware of the common practice to include that comment in generated files. I wasn't aware that linters should exclude those files though. TIL.

My experience with other linters, such as staticcheck, is that generated files are still processed. For example, in this other project I work on that uses staticcheck we had to include these linter comments to disable certain rules: https://github.com/stellar/go/blob/aa089ae/xdr/xdr_generated.go#L1-L2.

Looking at golangci-lint's configuration it appears many of the linters offer analyzing generated code as an option, but as you suggest, it is a flag that can be enabled or disabled. Although, it also looks like golangci-lint excludes generated code files by default at the top-level across all linters by simply excluding any issues found in them, so adding a flag is probably not necessary.

So far gochecknoglobals has been zero configuration, and folks who need more knobs use tools like golangci-lint that offer the additional knobs, and wrap gochecknoglobals and the many other linters available. I'm inclined to keep it as zero configuration unless there's strong need for configuration.

@benjivesterby Are you using golangci-lint? If you are, and you have gochecknoglobals enabled, it shouldn't report generated code files since golangci-lint will exclude any issues found on them by default. If it isn't, we should investigate that.

leighmcculloch avatar Dec 04 '21 07:12 leighmcculloch

I'm using golangci-lint for some projects but not the one I'm running this on. It seemed like this was a missing feature other linters have which is why I submitted the issue.

benjivesterby avatar Dec 04 '21 21:12 benjivesterby

I think I'm inclined to add a flag like -t. The way -t works is it ignores tests by default and adding -t enables tests. So if we add a flag like -g, change the default to ignore generated files, and then when -g is specified we check generated files.

Thoughts anyone?

leighmcculloch avatar Jan 15 '23 00:01 leighmcculloch