From a7a48168a0a9cd763aaa86da722605f86ff5ce2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rams=C3=A9s=20Morales?= Date: Thu, 21 Jul 2022 16:13:46 -0700 Subject: [PATCH 1/2] Add golangci-lint config file and `make` target. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Here we add file `.golangci.yaml`, to have a common configuration for static analysis. If you use the following IDEs/editors, they will read the file: - `vim` with vim-go. - VSCode. The initial configuration file is based on Kubernetes' HEAD. We removed a custom kubernetes-linter for custom kubernetes-logging. We also changed to Golang version 1.17 (etcd's current target Golang version) from 1.18. Also, we added a new target to `Makefile`: `lint`. NOTE that auto-fixing should be part of a later commit, once we are all happy with how the configuration file is working for us. As expected, this change fixes two issues found by this `.golangci.yaml` in file `contrib/lock/client/client.go`: - Dead code, removed. - Innefective assignment, removed. Finally, we are updating `CONTRIBUTING.md` to mention `golangci-lint`. We will add a GitHub-action to run `golangci-lint` in a future change. Local testing done: - `make build`. - `make test`. Both are happy. This is the initial step to fix https://github.com/etcd-io/etcd/issues/14164. Signed-off-by: Ramsés Morales --- .golangci.yaml | 41 +++++++++++++++++++++++++++++++++++ CONTRIBUTING.md | 6 +++++ Makefile | 4 ++++ contrib/lock/client/client.go | 2 +- 4 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 .golangci.yaml diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 000000000..c46d59e00 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,41 @@ +run: + timeout: 30m + skip-files: + - "^zz_generated.*" + +issues: + max-same-issues: 0 + # Excluding configuration per-path, per-linter, per-text and per-source + exclude-rules: + # exclude ineffassing linter for generated files for conversion + - path: conversion\.go + linters: + - ineffassign + +linters: + disable-all: true + enable: # please keep this alphabetized + # Don't use soon to deprecated[1] linters that lead to false + # https://github.com/golangci/golangci-lint/issues/1841 + # - deadcode + # - structcheck + # - varcheck + - ineffassign + - staticcheck + - stylecheck + - unused + +linters-settings: # please keep this alphabetized + staticcheck: + go: "1.17" + checks: [ + "all", + "-SA1019", # TODO(fix) Using a deprecated function, variable, constant or field + "-SA2002" # TODO(fix) Called testing.T.FailNow or SkipNow in a goroutine, which isn’t allowed + ] + stylecheck: + checks: [ + "ST1019", # Importing the same package multiple times. + ] + unused: + go: "1.17" diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3b1d78dcf..d3ce0180f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -92,3 +92,9 @@ commit using message with a more generic `*:` prefix. *: [..] ``` + +### Static analysis. + +We recommend that you install [golangci-lint](https://golangci-lint.run/usage/install/) and run `make lint`. + +Very soon we will have a GitHub action to run our linters. diff --git a/Makefile b/Makefile index d1f7865ed..34b370dae 100644 --- a/Makefile +++ b/Makefile @@ -166,3 +166,7 @@ build-docker-release-main: push-docker-release-main: $(info ETCD_VERSION: $(ETCD_VERSION)) docker push gcr.io/etcd-development/etcd:$(ETCD_VERSION) + +lint: + # Recursively analyzes the tree: + golangci-lint -v run diff --git a/contrib/lock/client/client.go b/contrib/lock/client/client.go index 9f8425a37..3dd633369 100644 --- a/contrib/lock/client/client.go +++ b/contrib/lock/client/client.go @@ -30,7 +30,7 @@ import ( "strconv" "time" - "go.etcd.io/etcd/client/v3" + clientv3 "go.etcd.io/etcd/client/v3" "go.etcd.io/etcd/client/v3/concurrency" ) From 3cdc4225e17f0ddee53fc3fa3c4ab7637fed4b83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rams=C3=A9s=20Morales?= Date: Fri, 22 Jul 2022 12:05:42 -0700 Subject: [PATCH 2/2] Followup to "Add golangci-lint config file" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Addressed comments by @ldez and added `lint-fix` target. Signed-off-by: Ramsés Morales --- .golangci.yaml | 18 +++++++----------- Makefile | 8 ++++++-- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index c46d59e00..fb3c4cb2d 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -1,4 +1,5 @@ run: + go: '1.17' timeout: 30m skip-files: - "^zz_generated.*" @@ -27,15 +28,10 @@ linters: linters-settings: # please keep this alphabetized staticcheck: - go: "1.17" - checks: [ - "all", - "-SA1019", # TODO(fix) Using a deprecated function, variable, constant or field - "-SA2002" # TODO(fix) Called testing.T.FailNow or SkipNow in a goroutine, which isn’t allowed - ] + checks: + - "all" + - "-SA1019" # TODO(fix) Using a deprecated function, variable, constant or field + - "-SA2002" # TODO(fix) Called testing.T.FailNow or SkipNow in a goroutine, which isn’t allowed stylecheck: - checks: [ - "ST1019", # Importing the same package multiple times. - ] - unused: - go: "1.17" + checks: + - "ST1019" # Importing the same package multiple times. diff --git a/Makefile b/Makefile index 34b370dae..959af8a41 100644 --- a/Makefile +++ b/Makefile @@ -167,6 +167,10 @@ push-docker-release-main: $(info ETCD_VERSION: $(ETCD_VERSION)) docker push gcr.io/etcd-development/etcd:$(ETCD_VERSION) +# Recursively analyzes the tree: lint: - # Recursively analyzes the tree: - golangci-lint -v run + golangci-lint run + +# Fixes found issues, if supported by the linter: +lint-fix: + golangci-lint run --fix