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

Upgrade golangci lint 1.39.0 #2198

Merged
merged 5 commits into from
Apr 12, 2021

Conversation

MarcosDY
Copy link
Collaborator

@MarcosDY MarcosDY commented Apr 7, 2021

I applied changes to make lints pass, but there are some cases that we must highlight

  • scopelint lint is deprecated we may replace it with exportloopref
  • We are supporting old TLS for mysql because there are some cases where it is required. But we really want it? must we disable it as default and add a configuration to allow tls versiones bellow 1.2?
  • we are using old k8s libraries, but upgrading it will take some time and it is not required yet, so I added a nolint for undeprecated client/fake.

I didn't add new lints to existing list, but I did a summary with list of lints that looks useful

Complete list: https://golangci-lint.run/usage/linters/

I divide it in 2 lists:

  • Interesting: list of lints I think we MUST add
  • Maybe: list of lints that are useful, but I'm not sure we really want, some of them are in my must list but they will require several changes that I'm not sure we really want.

Interesting

  • durationcheck: check for two durations multiplied together (we have a case in a test)
  • errorlint: go-errorlint is a source code linter for Go software that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13.
  • forcetypeassert: finds forced type assertions
  • goheader: Checks is file header matches to pattern
  • ifshort: Checks that your code uses short syntax for if-statements whenever possible (I personally loves using ifs short version)
  • nolintlint: Reports ill-formed or insufficient nolint directives (we have some nolints that are not longer required)
  • rowserrcheck: checks whether Err of rows is checked successfully (in some cases someone can miss calling s.Err and verify it before accesing data)
  • exportloopref: checks for pointers to enclosing loop variables (replacement for deprectated scopelint)
  • wastedassign: wastedassign finds wasted assignment statements.

Maybe

  • goerr113: Golang linter to check the errors handling expressions (we use %v everywhere, it will force us to use %w)
  • wrapcheck: Checks that errors returned from external packages are wrapped (I think we may add it)
  • dupl: search for duplicated code (It can be useful, and our code/tests has several duplications, in some cases like unit tests it is expected because we are creating expected/initial structs)
  • exhaustive: check exhaustiveness of enum switch statements (It can be usefull in some cases, and force us to always put a default on switch)
  • forbidigo: Forbids identifiers (serch for uses of fmt.Print, fmt.Errors, etc. It can be useful in case someone push an fmt.Print used for debuging)
  • gochecknoinits: Checks that no init functions are present in Go code (init functions are executed when importing somethings, it is not a good practice to add it)
  • goprintffuncname: Checks that printf-like functions are named with f at the end
  • importas: Enforces consistent import aliases (it is useful and may require some configurations)
  • makezero: Finds slice declarations with non-zero initial length
  • nilerr: Finds the code that returns nil even if it checks that the error is not nil. (we have some cases where we return nil when error, but in those cases it is expected)
  • noctx: noctx finds sending http request without context.Context (we have some cases, that we call http.Get without ctx, so we can not cancel those calls)
  • prealloc: Finds slice declarations that could potentially be preallocated (code is full of this cases)
  • predeclared: find code that shadows one of Go's predeclared identifiers
  • revive: a replacement for golint, it support several configuration. https://github.com/mgechev/revive
  • sqlclosecheck: Checks that sql.Rows and sql.Stmt are closed.
  • testpackage: linter that makes you use a separate _test package (it is something that we must do but all code does not set it and will take some time to update all tets to use only public functions)
  • thelper: thelper detects golang test helpers without t.Helper() call and checks the consistency of test helpers
  • gomoddirectives: with some configurations allows us to prevents someone push 'replace', 'retract', and 'excludes' directives

Signed-off-by: Marcos Yacob <marcos.yacob@hpe.com>
Signed-off-by: Marcos Yacob <marcos.yacob@hpe.com>
Copy link
Member

@azdagron azdagron left a comment

Choose a reason for hiding this comment

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

Awesome. Thanks @MarcosDY. Many of the new linters look good. Maybe we can decide which ones we want to apply and then open up a PR one at a time, introducing the new linter and the changes associated with that linter?

@@ -86,9 +86,9 @@ func TestRunTaskCancelsTasksIfContextCanceled(t *testing.T) {
assertErrorChan(t, out2, context.Canceled)
}

/////////////////////////////////////////////////////////////////////////////
// ///////////////////////////////////////////////////////////////////////////
Copy link
Member

Choose a reason for hiding this comment

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

gross. we should just get rid of the comment block.

@@ -185,7 +185,7 @@ func (e *Endpoints) ListenAndServe(ctx context.Context) error {
}

func (e *Endpoints) createTCPServer(ctx context.Context, unaryInterceptor grpc.UnaryServerInterceptor, streamInterceptor grpc.StreamServerInterceptor) *grpc.Server {
tlsConfig := &tls.Config{
tlsConfig := &tls.Config{ //nolint: gosec // False positive, getTLSConfig is setting MinVerion
Copy link
Member

Choose a reason for hiding this comment

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

typo on MinVersion

@@ -83,7 +83,7 @@ func configureConnection(cfg *configuration, isReadOnly bool) (string, error) {
return connectionString, nil
}

tlsConf := tls.Config{}
tlsConf := tls.Config{} //nolint: gosec // There are scenaries where MySQL requires older tls version, and example is when compiled with yaSSL that supports only TLSv1 and TLSv1.1 protocols.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
tlsConf := tls.Config{} //nolint: gosec // There are scenaries where MySQL requires older tls version, and example is when compiled with yaSSL that supports only TLSv1 and TLSv1.1 protocols.
// MySQL still allows, and in some places requires, older TLS versions. For example, when built with yaSSL, it is limited to TLSv1 and TLSv1.1.
// TODO: consider making this more secure by default
tlsConf := tls.Config{} //nolint: gosec // see above

@@ -29,7 +29,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/client/fake" // nolint: staticcheck // No longer deprectated in newer versions.
Copy link
Member

Choose a reason for hiding this comment

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

typo on deprecated

@@ -19,7 +19,7 @@ import (
"k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/client/fake" // nolint: staticcheck // No longer deprectated in newer versions.
Copy link
Member

Choose a reason for hiding this comment

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

typo on deprecated

@@ -18,7 +18,7 @@ import (
"k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/client/fake" // nolint: staticcheck // No longer deprectated in newer versions.
Copy link
Member

Choose a reason for hiding this comment

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

typo on deprecated

@azdagron
Copy link
Member

azdagron commented Apr 7, 2021

I like all of the linters that you've outlined as "Interesting" with maybe the exception of goheader. I'd love to hear @evan2645's thoughts on that. We've had discussions on per-file copyrights before.

From the "maybe" column, there are a bunch there that would be a huge amount of work to support. In general I like most of them, but I'm not sure the amount of churn required to get them to lint clean would be worth it. Particularly the error wrapping related ones and the testpackage one are going to be big lifts. The latter would be especially difficult, since the tests that benefit from not doing this are usually setting hooks and stuff that we'd have to figure out a good way of injecting.

@MarcosDY
Copy link
Collaborator Author

MarcosDY commented Apr 8, 2021

agree testpackage will be hard to use but we can add exceptions for older tests and just be sure that new tests we add are doing a good use of testpackage.
I can remember some tests that depends of calling private functions or vars, it will be hard to migrate (in some cases we'll need refactors to be able to tests them as expected).

Signed-off-by: Marcos Yacob <marcos.yacob@hpe.com>
@azdagron azdagron self-assigned this Apr 8, 2021
Copy link
Member

@azdagron azdagron left a comment

Choose a reason for hiding this comment

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

Thanks, @MarcosDY !

@azdagron azdagron merged commit 48d5606 into spiffe:master Apr 12, 2021
This was referenced May 15, 2021
@hiyosi hiyosi mentioned this pull request May 21, 2021
3 tasks
@MarcosDY MarcosDY deleted the upgrade-golangci-lint-1-39-0 branch August 2, 2022 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants