Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ci] Add errcheck to golangci-lint #7996

Merged
merged 10 commits into from
Aug 26, 2021
Merged

Conversation

ajm188
Copy link
Contributor

@ajm188 ajm188 commented Apr 29, 2021

Description

This PR enables the errcheck linter as part of the golangci-lint linter suite, and then sets up a bunch of configs to allow us to progressively enable this linter on more and more parts of the codebase.

To make this work, I needed to bump the version of golangci-lint to v1.39.0, since earlier versions were not correctly working with the errcheck_excludes.txt file. I can't go all the way to the latest version (v1.40.1) because that starts to complain about stuff in the fuzz tests, and I didn't want to touch those in this change haha.

After I got the config into a working state, I then moved through and fixed up a bunch of the now-complaining errcheck failures. I stuck to packages that had relatively few failures, and to errcheck violations that I thought would be pretty uncontroversial to fix. The rest of the packages I've left as ignored in our .golangci.yml config, and as we gradually fix those instances we can remove those entries from the config.

Related Issue(s)

Closes #8222

Checklist

  • Tests were added or are not required -- n/a
  • Documentation was added or is not required

Deployment Notes

@harshit-gangal
Copy link
Member

@ajm188 AFAIK, in draft mode also the CI tests run :)

@ajm188
Copy link
Contributor Author

ajm188 commented Apr 29, 2021

huh! i have a vague memory of turning a PR back to draft mode to debug some tests and none of the CIs were running. happy to be wrong though!

@ajm188 ajm188 marked this pull request as draft April 29, 2021 13:32
@ajm188 ajm188 force-pushed the am_errcheck branch 6 times, most recently from eefb8a6 to 8dca6f7 Compare May 30, 2021 01:49
@ajm188 ajm188 changed the title [WIP | Do not merge] add errcheck to golangci-lint [ci] Add errcheck to golangci-lint May 30, 2021
@ajm188 ajm188 requested review from falun, systay and deepthi May 31, 2021 19:45
@ajm188 ajm188 marked this pull request as ready for review May 31, 2021 19:45
@@ -40,11 +40,13 @@ func List() *cobra.Command {
out = rules
}
} else {
out = rules.Find(listOptName)
if listOptNamesOnly && out != nil {
rule := rules.Find(listOptName)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See 6ab6b47 for why this change

@@ -283,7 +283,7 @@ func (c *Collection) Append(m Metric) error {
c.Lock() //nolint SA5011: possible nil pointer dereference
defer c.Unlock() //nolint SA5011: possible nil pointer dereference
// we don't want to add nil metrics
if c == nil {
if c == nil { //nolint SA5011: redundant nil pointer check - maybe this was supposed to check if m == nil ?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shlomi-noach calling your particular attention to this line; I think this should be checking if m == nil (since we checked c == nil just before locking above) but wanted to confirm with you first.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ajm188 I believe you're right, this should be m == nil. Took me a while to understand why I have no recollection of this code, it was a contribution and I'm not too familiar with it.

Copy link
Contributor

@shlomi-noach shlomi-noach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@@ -283,7 +283,7 @@ func (c *Collection) Append(m Metric) error {
c.Lock() //nolint SA5011: possible nil pointer dereference
defer c.Unlock() //nolint SA5011: possible nil pointer dereference
// we don't want to add nil metrics
if c == nil {
if c == nil { //nolint SA5011: redundant nil pointer check - maybe this was supposed to check if m == nil ?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ajm188 I believe you're right, this should be m == nil. Took me a while to understand why I have no recollection of this code, it was a contribution and I'm not too familiar with it.

Copy link
Member

@deepthi deepthi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
A couple of things:

@@ -85,7 +85,7 @@ func main() {
startMsg := fmt.Sprintf("USER=%v SUDO_USER=%v %v", os.Getenv("USER"), os.Getenv("SUDO_USER"), strings.Join(os.Args, " "))

if syslogger, err := syslog.New(syslog.LOG_INFO, "vtctl "); err == nil {
syslogger.Info(startMsg)
syslogger.Info(startMsg) // nolint:errcheck
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: why not _ = ... instead of nolint?

ajm188 added 8 commits August 26, 2021 15:19
Signed-off-by: Andrew Mason <[email protected]>
Signed-off-by: Andrew Mason <[email protected]>
Signed-off-by: Andrew Mason <[email protected]>
This is an often-surprising Go quirk, so I will explain a bit:

An interface has two components: the concrete type, and the value. If
both of these are `nil`, then the interface is equal to `nil`. If the
concrete type is non-nil, even if the value for that type is nil, then
the interface is non-nil. For a toy example, see:
https://play.golang.org/p/0uLrBekVg_y.

In this specific case, we were taking `rules.Find`, which returns
`*rules.Rule` and assigning that to a variable with the type of
`interface{}`. Since we have a concrete type for the interface, the `out
!= nil` check will never be false. The fix here is to use a second
variable, which will have the concrete type, and check if the pointer is
nil, which is what we actually wanted here.

Signed-off-by: Andrew Mason <[email protected]>
Signed-off-by: Andrew Mason <[email protected]>
@deepthi
Copy link
Member

deepthi commented Aug 26, 2021

Still need to update the version we download in the hook (if the binary doesn't exist locally).

curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.34.1

Also, do we need to announce/warn people in #developers that they will need to upgrade their installed version of the linter?

@ajm188
Copy link
Contributor Author

ajm188 commented Aug 26, 2021

yeah, i was planning to post in #developers after i merged!

@ajm188 ajm188 merged commit de7f133 into vitessio:main Aug 26, 2021
@ajm188 ajm188 deleted the am_errcheck branch August 26, 2021 22:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add errcheck to CI build
6 participants