From f11b24e08a61751eeb3f3f6725845a1ba7c38e2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enver=20Bi=C5=A1evac?= Date: Thu, 13 Feb 2025 14:06:47 +0000 Subject: [PATCH] feat: [CODE-3149]: minor optimization of golangci-lint config, upgrade the linter version (#3382) * Merge branch 'eb/code-3149' of https://git0.harness.io/l7B_kbSEQD2wjrM7PShm5w/PROD/Harness_Commons/gitness into eb/code-3149 * include whole file * feat:[CODE-3149]: add ok to ignore for shadow declarations (#3394) * feat:[CODE-3149]: add ok to ignore for shadow declarations * requested changes * branch linting added * minor optimization of golangci-lint config, upgrade the linter version --- .golangci.yml | 102 +++----------------------------------------------- Makefile | 13 ++++++- 2 files changed, 17 insertions(+), 98 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index bdb6477d2..4a23a6d2d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,22 +1,8 @@ ## Golden config for golangci-lint v1.49.0 -run: - # Timeout for analysis, e.g. 30s, 5m. - # Default: 1m - timeout: 3m - - # This file contains only configs which differ from defaults. # All possible options can be found here https://github.com/golangci/golangci-lint/blob/master/.golangci.reference.yml linters-settings: - cyclop: - # The maximal code complexity to report. - # Default: 10 - max-complexity: 30 - # The maximal average package complexity. - # If it's higher than 0.0 (float) the check is enabled - # Default: 0.0 - package-average: 10.0 errcheck: # Report about not checking of errors in type assertions: `a := b.(MyStruct)`. @@ -24,16 +10,6 @@ linters-settings: # Default: false check-type-assertions: true - funlen: - # Checks the number of lines in a function. - # If lower than 0, disable the check. - # Default: 60 - lines: 100 - # Checks the number of statements in a function. - # If lower than 0, disable the check. - # Default: 40 - statements: 50 - gocritic: # Settings passed to gocritic. # The settings key is the name of a supported gocritic checker. @@ -48,26 +24,6 @@ linters-settings: # Default: true skipRecvDeref: false - gomnd: - # List of function patterns to exclude from analysis. - # Values always ignored: `time.Date` - # Default: [] - ignored-functions: - - os.Chmod - - os.Mkdir - - os.MkdirAll - - os.OpenFile - - os.WriteFile - - prometheus.ExponentialBuckets - - prometheus.ExponentialBucketsRange - - prometheus.LinearBuckets - - strconv.FormatFloat - - strconv.FormatInt - - strconv.FormatUint - - strconv.ParseFloat - - strconv.ParseInt - - strconv.ParseUint - gomodguard: blocked: # List of blocked modules. @@ -188,31 +144,24 @@ linters: - staticcheck # is a go vet on steroids, applying a ton of static analysis checks - typecheck # like the front-end of a Go compiler, parses and type-checks Go code - unused # checks for unused constants, variables, functions and types + ## disabled by default - asasalint # checks for pass []any as any in variadic func(...any) - asciicheck # checks that your code does not contain non-ASCII identifiers - bidichk # checks for dangerous unicode character sequences - bodyclose # checks whether HTTP response body is closed successfully - #- contextcheck # checks the function whether use a non-inherited context # TODO: enable after golangci-lint uses https://github.com/sylvia7788/contextcheck/releases/tag/v1.0.7 - - cyclop # checks function and package cyclomatic complexity + - contextcheck # checks the function whether use a non-inherited context # - dupl # tool for code clone detection - durationcheck # checks for two durations multiplied together - errname # checks that sentinel errors are prefixed with the Err and error types are suffixed with the Error - errorlint # finds code that will cause problems with the error wrapping scheme introduced in Go 1.13 - - execinquery # checks query string in Query function which reads your Go src files and warning it finds - exhaustive # checks exhaustiveness of enum switch statements - - exportloopref # checks for pointers to enclosing loop variables + - copyloopvar # linter detects places where loop variables are copied - forbidigo # forbids identifiers - #- funlen # tool for detection of long functions - #- gochecknoglobals # checks that no global variables exist - #- gochecknoinits # checks that no init functions are present in Go code - - gocognit # computes and checks the cognitive complexity of functions - goconst # finds repeated strings that could be replaced by a constant - gocritic # provides diagnostics that check for bugs, performance and style issues - - gocyclo # computes and checks the cyclomatic complexity of functions - godot # checks if comments end in a period - goimports # in addition to fixing imports, goimports also formats your code in the same style as gofmt - - gomnd # detects magic numbers - gomoddirectives # manages the use of 'replace', 'retract', and 'excludes' directives in go.mod - gomodguard # allow and block lists linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations - goprintffuncname # checks that printf-like functions are named with f at the end @@ -224,7 +173,6 @@ linters: - nilerr # finds the code that returns nil even if it checks that the error is not nil - nilnil # checks that there is no simultaneous return of nil error and an invalid value - noctx # finds sending http request without context.Context - # - nolintlint # reports ill-formed or insufficient nolint directives # - nonamedreturns # reports all named returns - nosprintfhostport # checks for misuse of Sprintf to construct a host with port in a URL - predeclared # finds code that shadows one of Go's predeclared identifiers @@ -236,7 +184,6 @@ linters: - stylecheck # is a replacement for golint - tagliatelle # checks the struct tags - tenv # detects using os.Setenv instead of t.Setenv since Go1.17 - #- testpackage # makes you use a separate _test package - tparallel # detects inappropriate usage of t.Parallel() method in your Go test codes - unconvert # removes unnecessary type conversions - unparam # reports unused function parameters @@ -250,40 +197,16 @@ linters: - gci # controls golang package import order and makes it always deterministic #- godox # detects FIXME, TODO and other comment keywords - goheader # checks is file header matches to pattern - #- interfacebloat # checks the number of methods inside an interface - #- ireturn # accept interfaces, return concrete types - #- prealloc # [premature optimization, but can be used in some cases] finds slice declarations that could potentially be preallocated - #- varnamelen # [great idea, but too many false positives] checks that the length of a variable's name matches its scope - #- wrapcheck # checks that errors returned from external packages are wrapped - - ## disabled - #- containedctx # detects struct contained context.Context field - #- depguard # [replaced by gomodguard] checks if package imports are in a list of acceptable packages - #- dogsled # checks assignments with too many blank identifiers (e.g. x, _, _, _, := f()) - #- errchkjson # [don't see profit + I'm against of omitting errors like in the first example https://github.com/breml/errchkjson] checks types passed to the json encoding functions. Reports unsupported types and optionally reports occasions, where the check for the returned error can be omitted - #- forcetypeassert # [replaced by errcheck] finds forced type assertions - #- goerr113 # [too strict] checks the errors handling expressions - #- gofmt # [replaced by goimports] checks whether code was gofmt-ed - #- gofumpt # [replaced by goimports, gofumports is not available yet] checks whether code was gofumpt-ed - #- grouper # analyzes expression groups - #- importas # enforces consistent import aliases - #- logrlint # [owner archived repository] checks logr arguments - #- maintidx # measures the maintainability index of each function - misspell # [useless] finds commonly misspelled English words in comments - #- nlreturn # [too strict and mostly code is not more readable] checks for a new line before return and branch statements to increase code clarity - #- paralleltest # [too many false positives] detects missing usage of t.Parallel() method in your Go test - #- thelper # detects golang test helpers without t.Helper() call and checks the consistency of test helpers - #- wsl # [too strict and mostly code is not more readable] whitespace linter forces you to use empty lines - issues: # Maximum count of issues with the same text. # Set to 0 to disable. # Default: 3 - max-same-issues: 50 + max-same-issues: 10 exclude-rules: - - text: 'shadow: declaration of "(err|ctx)" shadows declaration at' + - text: 'shadow: declaration of "(err|ctx|ok)" shadows declaration at' linters: [ govet ] - source: "^//\\s*go:generate\\s" linters: [ lll ] @@ -307,7 +230,7 @@ issues: - path: "^registry/app/driver/filesystem/.*" linters: [ gocritic ] - path: "^registry/app/driver/s3-aws/.*" - linters: [ gocognit, gocyclo, gosec, nestif, cyclop] + linters: [ gocognit, gosec, nestif] - path: "^registry/app/remote/clients/registry/interceptor/interceptor.go" linters: [ goheader ] - path: "^registry/app/common/http/modifier/modifier.go" @@ -492,16 +415,3 @@ issues: linters: [ goheader ] - path: "^registry/app/storage/blobStore.go" linters: [ gosec ] - #Registry Specific ends - - text: "mnd: Magic number: \\d" - linters: - - gomnd - - path: "_test\\.go" - linters: - - bodyclose - - dupl - - funlen - - goconst - - gosec - - noctx - - wrapcheck \ No newline at end of file diff --git a/Makefile b/Makefile index 4ba3205ed..ca12aa5b4 100644 --- a/Makefile +++ b/Makefile @@ -102,10 +102,19 @@ sec: @echo "Vulnerability detection $(1)" @govulncheck ./... -lint: tools generate # lint the golang code +lint: tools generate # lint the golang code - CI + @echo "Linting $(1)" + @golangci-lint run --timeout=3m --verbose --new-from-rev=HEAD~ + +lint-full: tools generate # full linting the golang code @echo "Linting $(1)" @golangci-lint run --timeout=3m --verbose +lint-local: tools generate # lint the golang code - only untracked and staged changes + @echo "Linting $(1)" + @golangci-lint run --new --timeout=3m --verbose --whole-files + + ############################################################################### # Code Generation # @@ -139,7 +148,7 @@ delete-tools: ## Delete the tools # Install golangci-lint $(GOBIN)/golangci-lint: @echo "🔘 Installing golangci-lint... (`date '+%H:%M:%S'`)" - @curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOBIN) v1.56.2 + @curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOBIN) v1.63.4 # Install goimports to format code $(GOBIN)/goimports: