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

Replace old linters with golangci-lint #3237

Merged
merged 12 commits into from
Sep 2, 2021
35 changes: 35 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
run:
timeout: 10m
skip-dirs:
- swagger-gen
- pkg/cassandra/mocks
- storage/mocks
- cmd/ingester/app/consumer/mocks

linters-settings:
gosec:
# To specify a set of rules to explicitly exclude.
# Available rules: https://github.com/securego/gosec#available-rules
excludes:
- G104
- G107
Comment on lines +14 to +15
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These gosec rules were excluded in the Makefile, so I did the same.

- G404
- G601
Comment on lines +16 to +17
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These gosec rules were not excluded in the Makefile, but were identified, giving the errors:

cmd/collector/app/sanitizer/cache/auto_refresh_cache.go:136:40: G404: Use of weak random number generator (math/rand instead of crypto/rand) (gosec)
	return (interval / 2) + time.Duration(rand.Int63n(int64(interval/2)))
	                                      ^
crossdock/services/tracehandler.go:308:27: G404: Use of weak random number generator (math/rand instead of crypto/rand) (gosec)
	return fmt.Sprintf("%x", rand.Int63())
	                         ^
cmd/ingester/app/processor/decorator/retry.go:54:18: G404: Use of weak random number generator (math/rand instead of crypto/rand) (gosec)
	rand:           rand.New(rand.NewSource(time.Now().UnixNano())),
	                ^
model/converter/thrift/jaeger/from_domain.go:106:37: G601: Implicit memory aliasing in for loop. (gosec)
		jaegerTags[idx] = d.keyValueToTag(&kv)
		                                  ^
cmd/anonymizer/main.go:72:22: G601: Implicit memory aliasing in for loop. (gosec)
				writer.WriteSpan(&span)

I've excluded them for now, but if there is a preference to address these rules in this PR, I can do so.

Copy link
Member

Choose a reason for hiding this comment

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

let's fix them in another PR

gosimple:
go: "1.17"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Bumped the go version since we've moved onto 1.17 already.


linters:
enable:
- gofmt
- gosec
- govet
disable:
- errcheck

issues:
# Excluding configuration per-path, per-linter, per-text and per-source
exclude-rules:
# Exclude some linters from running on tests files.
- path: _test\.go
linters:
- gosec
47 changes: 7 additions & 40 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ALL_SRC := $(shell find . -name '*.go' \
-type f | \
sort)

# ALL_PKGS is used with 'golint'
# ALL_PKGS is used with 'nocover'
ALL_PKGS := $(shell echo $(dir $(ALL_SRC)) | tr ' ' '\n' | sort -u)

UNAME := $(shell uname -m)
Expand All @@ -35,11 +35,8 @@ GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
GOBUILD=CGO_ENABLED=0 installsuffix=cgo go build -trimpath
GOTEST=go test -v $(RACE)
GOLINT=golint
Copy link
Contributor Author

@albertteoh albertteoh Aug 29, 2021

Choose a reason for hiding this comment

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

golint, go vet and gosec should no longer be needed as golangci-lint provides these linters, so I'm removing them from the Makefile to simplify.

GOVET=go vet
GOFMT=gofmt
FMT_LOG=.fmt.log
LINT_LOG=.lint.log
IMPORT_LOG=.import.log

GIT_SHA=$(shell git rev-parse HEAD)
Expand Down Expand Up @@ -82,7 +79,7 @@ go-gen:

.PHONY: clean
clean:
rm -rf cover.out .cover/ cover.html lint.log fmt.log \
rm -rf cover.out .cover/ cover.html $(FMT_LOG) $(IMPORT_LOG) \
jaeger-ui/packages/jaeger-ui/build

.PHONY: test
Expand Down Expand Up @@ -130,7 +127,6 @@ index-rollover-integration-test: docker-images-elastic
go clean -testcache
bash -c "set -e; set -o pipefail; $(GOTEST) -tags index_rollover $(STORAGE_PKGS) | $(COLORIZE)"


.PHONY: token-propagation-integration-test
token-propagation-integration-test:
go clean -testcache
Expand Down Expand Up @@ -161,39 +157,12 @@ fmt:
@$(GOFMT) -e -s -l -w $(ALL_SRC)
./scripts/updateLicenses.sh

.PHONY: lint-gosec
lint-gosec:
time gosec -quiet -exclude=G104,G107 ./...

.PHONY: lint-staticcheck
lint-staticcheck:
@cat /dev/null > $(LINT_LOG)
time staticcheck ./... \
| grep -v \
-e model/model.pb.go \
-e proto-gen \
-e _test.pb.go \
-e thrift-gen/ \
-e swagger-gen/ \
>> $(LINT_LOG) || true
@[ ! -s "$(LINT_LOG)" ] || (echo "Detected staticcheck failures:" | cat - $(LINT_LOG) && false)

.PHONY: lint
lint: lint-staticcheck lint-gosec
$(GOVET) ./...
$(MAKE) go-lint
@echo Running go fmt on ALL_SRC ...
@$(GOFMT) -e -s -l $(ALL_SRC) > $(FMT_LOG)
./scripts/updateLicenses.sh >> $(FMT_LOG)
Copy link
Member

Choose a reason for hiding this comment

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

This must remain

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch, I've added this back into the lint target. Tested by removing the license from grpc_handler.go:

$ make lint
golangci-lint run
./scripts/updateLicenses.sh > .fmt.log
License check failures, run 'make fmt'
cmd/collector/app/handler/grpc_handler.go
make: *** [lint] Error 1

lint:
golangci-lint -v run
./scripts/updateLicenses.sh > $(FMT_LOG)
./scripts/import-order-cleanup.sh stdout > $(IMPORT_LOG)
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
@[ ! -s "$(FMT_LOG)" -a ! -s "$(IMPORT_LOG)" ] || (echo "Go fmt, license check, or import ordering failures, run 'make fmt'" | cat - $(FMT_LOG) && false)

.PHONY: go-lint
go-lint:
@cat /dev/null > $(LINT_LOG)
@echo Running go lint...
@$(GOLINT) $(ALL_PKGS) | grep -v _nolint.go >> $(LINT_LOG) || true;
@[ ! -s "$(LINT_LOG)" ] || (echo "Lint Failures" | cat - $(LINT_LOG) && false)
@[ ! -s "$(FMT_LOG)" -a ! -s "$(IMPORT_LOG)" ] || (echo "License check or import ordering failures, run 'make fmt'" | cat - $(FMT_LOG) $(IMPORT_LOG) && false)

.PHONY: build-examples
build-examples:
Expand Down Expand Up @@ -400,10 +369,8 @@ changelog:
.PHONY: install-tools
install-tools:
go install github.com/wadey/gocovmerge
go install golang.org/x/lint/golint
go install github.com/mjibson/esc
go install github.com/securego/gosec/cmd/gosec
go install honnef.co/go/tools/cmd/staticcheck
go install github.com/golangci/golangci-lint/cmd/[email protected]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Add this to the tools so CI is able to run golangci-lint, and ensure devs also have the right tools installed to test linting.


.PHONY: install-ci
install-ci: install-tools
Expand Down
4 changes: 2 additions & 2 deletions cmd/agent/app/reporter/grpc/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestReporter_EmitZipkinBatch(t *testing.T) {
})
defer s.Stop()
conn, err := grpc.Dial(addr.String(), grpc.WithInsecure())
//lint:ignore SA5001 don't care about errors
//nolint:staticcheck // don't care about errors
defer conn.Close()
require.NoError(t, err)

Expand Down Expand Up @@ -94,7 +94,7 @@ func TestReporter_EmitBatch(t *testing.T) {
})
defer s.Stop()
conn, err := grpc.Dial(addr.String(), grpc.WithInsecure())
//lint:ignore SA5001 don't care about errors
//nolint:staticcheck // don't care about errors
defer conn.Close()
require.NoError(t, err)
rep := NewReporter(conn, nil, zap.NewNop())
Expand Down
4 changes: 1 addition & 3 deletions cmd/flags/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ import (
)

const (
healthCheckHTTPPort = "health-check-http-port"
adminHTTPPort = "admin-http-port"
Comment on lines -35 to -36
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unused and deprecated here.

adminHTTPHostPort = "admin.http.host-port"
adminHTTPHostPort = "admin.http.host-port"
)

// AdminServer runs an HTTP server with admin endpoints, such as healthcheck at /, /metrics, etc.
Expand Down
2 changes: 1 addition & 1 deletion pkg/gogocodec/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ package gogocodec
import (
"testing"

"github.com/golang/protobuf/proto" //lint:ignore SA1019 deprecated package
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please let me know if there are issues with moving to "google.golang.org/protobuf/proto", and I can revert back and add a //nolint directive. Tests appear to still be passing with this change.

"google.golang.org/protobuf/types/known/emptypb"

"github.com/jaegertracing/jaeger/model"
Expand Down
2 changes: 1 addition & 1 deletion pkg/queue/bounded_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func helper(t *testing.T, startConsumers func(q *BoundedQueue, consumerFn func(i

// block further processing until startLock is released
startLock.Lock()
//lint:ignore SA2001 empty section is ok
//nolint:staticcheck // empty section is ok
startLock.Unlock()
})

Expand Down