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

🌱 Add sub-modules in go lint and fix reported issues #897

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dilyar85
Copy link
Member

@dilyar85 dilyar85 commented Feb 19, 2025

What does this PR do, and why is it needed?

Currently golangci-lint doesn't support multiple modules as reported in golangci/golangci-lint#828, so our api/ and other submodules are never being linted.

This PR addresses this by updating the lint-go Makefile target to run the golangci-lint command in each module.

The .golangci.yml is updated to ignore some of the rules specific to _conversion.go files by using the path: param.

Which issue(s) is/are addressed by this PR?

Fixes N/A.

Are there any special notes for your reviewer:

  • Ran make lint-go-full command locally and passed:
$ make lint-go-full
Running golangci-lint in ./api/
INFO golangci-lint has version v1.60.0 built with go1.23.2 from (unknown, modified: ?, mod sum: "h1:MgoGnQcKdStp+lOhqQM79cQpI29j0ZQugNBC9sDg6h8=") on (unknown)
INFO [config_reader] Config search paths: [./ /Users/sdiliyaer/vm-operator/api /Users/sdiliyaer/vm-operator /Users/sdiliyaer /Users /]
INFO [config_reader] Used config file ../.golangci.yml
INFO [lintersdb] Active 31 linters: [asciicheck bodyclose depguard dogsled errcheck errorlint exportloopref goconst gocritic gocyclo godot gofmt goimports goprintffuncname gosec gosimple govet importas ineffassign misspell nakedret nilerr nolintlint prealloc revive rowserrcheck staticcheck stylecheck unconvert unparam unused]
INFO [loader] Go packages loading at mode 575 (name|types_sizes|compiled_files|deps|exports_file|files|imports) took 560.507698ms
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 20.366132ms
INFO [linters_context/goanalysis] analyzers took 0s with no stages
INFO [runner] Issues before processing: 1424, after processing: 0
INFO [runner] Processors filtering stat (in/out): exclude-rules: 525/64, skip_files: 1424/562, exclude: 562/525, invalid_issue: 1424/1424, skip_dirs: 562/562, identifier_marker: 562/562, nolint: 64/0, cgo: 1424/1424, filename_unadjuster: 1424/1424, path_prettifier: 1424/1424, autogenerated_exclude: 562/562
INFO [runner] processing took 52.003232ms with stages: exclude-rules: 14.601382ms, identifier_marker: 13.64729ms, autogenerated_exclude: 12.536094ms, nolint: 4.188398ms, skip_files: 2.234126ms, path_prettifier: 2.144283ms, exclude: 2.14063ms, skip_dirs: 286.43µs, cgo: 121.341µs, invalid_issue: 67.795µs, filename_unadjuster: 32.156µs, max_same_issues: 1.034µs, uniq_by_line: 364ns, diff: 341ns, fixer: 295ns, max_from_linter: 263ns, sort_results: 230ns, source_code: 207ns, path_prefixer: 192ns, path_shortener: 147ns, max_per_file_from_linter: 128ns, severity-rules: 106ns
INFO [runner] linters took 1.44452342s with stages: goanalysis_metalinter: 1.392379653s
INFO File cache stats: 0 entries of total size 0B
INFO Memory: 22 samples, avg is 55.1MB, max is 77.6MB
INFO Execution took 2.043655194s
Skipping ./external/appplatform/
Skipping ./external/byok/
Skipping ./external/capabilities/
Skipping ./external/ncp/
Skipping ./external/storage-policy-quota/
Skipping ./external/tanzu-topology/
Skipping ./hack/tools/
Running golangci-lint in ./pkg/backup/api/
INFO golangci-lint has version v1.60.0 built with go1.23.2 from (unknown, modified: ?, mod sum: "h1:MgoGnQcKdStp+lOhqQM79cQpI29j0ZQugNBC9sDg6h8=") on (unknown)
INFO [config_reader] Config search paths: [./ /Users/sdiliyaer/vm-operator/pkg/backup/api /Users/sdiliyaer/vm-operator/pkg/backup /Users/sdiliyaer/vm-operator/pkg /Users/sdiliyaer/vm-operator /Users/sdiliyaer /Users /]
INFO [config_reader] Used config file ../../../.golangci.yml
INFO [lintersdb] Active 31 linters: [asciicheck bodyclose depguard dogsled errcheck errorlint exportloopref goconst gocritic gocyclo godot gofmt goimports goprintffuncname gosec gosimple govet importas ineffassign misspell nakedret nilerr nolintlint prealloc revive rowserrcheck staticcheck stylecheck unconvert unparam unused]
INFO [loader] Go packages loading at mode 575 (exports_file|imports|name|types_sizes|compiled_files|deps|files) took 53.929784ms
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 441.806µs
INFO [linters_context/goanalysis] analyzers took 0s with no stages
INFO [runner] Issues before processing: 2, after processing: 0
INFO [runner] Processors filtering stat (in/out): autogenerated_exclude: 2/2, exclude-rules: 1/0, filename_unadjuster: 2/2, path_prettifier: 2/2, cgo: 2/2, invalid_issue: 2/2, skip_files: 2/2, skip_dirs: 2/2, identifier_marker: 2/2, exclude: 2/1
INFO [runner] processing took 317.981µs with stages: autogenerated_exclude: 228.819µs, identifier_marker: 39.518µs, path_prettifier: 27.796µs, skip_dirs: 7.113µs, exclude-rules: 3.079µs, skip_files: 2.504µs, cgo: 2.34µs, invalid_issue: 2.132µs, exclude: 1.577µs, nolint: 598ns, max_same_issues: 451ns, filename_unadjuster: 402ns, fixer: 358ns, uniq_by_line: 269ns, sort_results: 184ns, max_from_linter: 182ns, source_code: 119ns, diff: 119ns, max_per_file_from_linter: 115ns, path_shortener: 108ns, path_prefixer: 102ns, severity-rules: 96ns
INFO [runner] linters took 1.072035307s with stages: goanalysis_metalinter: 1.071653326s
INFO File cache stats: 0 entries of total size 0B
INFO Memory: 13 samples, avg is 58.9MB, max is 65.3MB
INFO Execution took 1.145308082s
Running golangci-lint in ./pkg/constants/testlabels/
INFO golangci-lint has version v1.60.0 built with go1.23.2 from (unknown, modified: ?, mod sum: "h1:MgoGnQcKdStp+lOhqQM79cQpI29j0ZQugNBC9sDg6h8=") on (unknown)
INFO [config_reader] Config search paths: [./ /Users/sdiliyaer/vm-operator/pkg/constants/testlabels /Users/sdiliyaer/vm-operator/pkg/constants /Users/sdiliyaer/vm-operator/pkg /Users/sdiliyaer/vm-operator /Users/sdiliyaer /Users /]
INFO [config_reader] Used config file ../../../.golangci.yml
INFO [lintersdb] Active 31 linters: [asciicheck bodyclose depguard dogsled errcheck errorlint exportloopref goconst gocritic gocyclo godot gofmt goimports goprintffuncname gosec gosimple govet importas ineffassign misspell nakedret nilerr nolintlint prealloc revive rowserrcheck staticcheck stylecheck unconvert unparam unused]
INFO [loader] Go packages loading at mode 575 (types_sizes|deps|exports_file|imports|name|compiled_files|files) took 56.773912ms
INFO [runner/filename_unadjuster] Pre-built 0 adjustments in 425.454µs
INFO [linters_context/goanalysis] analyzers took 0s with no stages
INFO [runner] Issues before processing: 2, after processing: 0
INFO [runner] Processors filtering stat (in/out): filename_unadjuster: 2/2, skip_dirs: 2/2, cgo: 2/2, identifier_marker: 2/2, exclude: 2/1, exclude-rules: 1/0, path_prettifier: 2/2, autogenerated_exclude: 2/2, invalid_issue: 2/2, skip_files: 2/2
INFO [runner] processing took 363.963µs with stages: autogenerated_exclude: 263.794µs, path_prettifier: 42.586µs, identifier_marker: 34.586µs, skip_dirs: 7.094µs, exclude-rules: 2.977µs, invalid_issue: 2.715µs, skip_files: 2.365µs, exclude: 2.023µs, cgo: 2.012µs, nolint: 1.019µs, max_same_issues: 805ns, filename_unadjuster: 354ns, fixer: 307ns, uniq_by_line: 227ns, diff: 191ns, max_from_linter: 176ns, sort_results: 168ns, severity-rules: 153ns, source_code: 120ns, max_per_file_from_linter: 103ns, path_prefixer: 94ns, path_shortener: 94ns
INFO [runner] linters took 1.101322686s with stages: goanalysis_metalinter: 1.100894886s
INFO File cache stats: 0 entries of total size 0B
INFO Memory: 13 samples, avg is 51.1MB, max is 61.3MB
INFO Execution took 1.176194872s
  • Verified make fix didn't fix issues that were explicitly excluded.

Please add a release note if necessary:

Add sub-modules in lint-go Makefile target and fix reported issues.

📚 Documentation preview 📚: https://vm-operator--897.org.readthedocs.build/en/897/

@github-actions github-actions bot added the size/L Denotes a PR that changes 100-499 lines. label Feb 19, 2025
@akutz
Copy link
Collaborator

akutz commented Feb 19, 2025

@dilyar85 ,

First of all, thank you for this. I really appreciate you taking the effort and doing this without anyone asking. Second, unfortunately, I already went down this route and did not open this exact PR because of the same things you did to avoid the issues:

  • We should not exclude *_conversion.go files. They are manually created and should be linted.
  • Some exclusions, such as varnames, should be caught for the rest of the project.

Ultimately we need a special linter config for the ./api sub-module, and I am currently investigating how to use multiple config files to use the root one as the base and overlay it with additional settings, perhaps by using yq to build a config dynamically as outlined by golangci/golangci-lint#2001 (comment).

@akutz
Copy link
Collaborator

akutz commented Feb 19, 2025

@dilyar85 ,

First of all, thank you for this. I really appreciate you taking the effort and doing this without anyone asking. Second, unfortunately, I already went down this route and did not open this exact PR because of the same things you did to avoid the issues:

  • We should not exclude *_conversion.go files. They are manually created and should be linted.
  • Some exclusions, such as varnames, should be caught for the rest of the project.

Ultimately we need a special linter config for the ./api sub-module, and I am currently investigating how to use multiple config files to use the root one as the base and overlay it with additional settings, perhaps by using yq to build a config dynamically as outlined by golangci/golangci-lint#2001 (comment).

My suggestion is to refactor this PR to include the fixes to the linter errors, such as ending comments with a . character or other fixes you did. But to not yet activate the linting on the sub-mods until we can create a dynamic config.

Copy link

Code Coverage

Package Line Rate Health
github.com/vmware-tanzu/vm-operator/controllers/contentlibrary/clustercontentlibraryitem 100%
github.com/vmware-tanzu/vm-operator/controllers/contentlibrary/contentlibraryitem 100%
github.com/vmware-tanzu/vm-operator/controllers/contentlibrary/utils 89%
github.com/vmware-tanzu/vm-operator/controllers/infra/capability/configmap 92%
github.com/vmware-tanzu/vm-operator/controllers/infra/capability/crd 100%
github.com/vmware-tanzu/vm-operator/controllers/infra/configmap 74%
github.com/vmware-tanzu/vm-operator/controllers/infra/node 77%
github.com/vmware-tanzu/vm-operator/controllers/infra/secret 76%
github.com/vmware-tanzu/vm-operator/controllers/infra/validatingwebhookconfiguration 85%
github.com/vmware-tanzu/vm-operator/controllers/infra/zone 73%
github.com/vmware-tanzu/vm-operator/controllers/storageclass 95%
github.com/vmware-tanzu/vm-operator/controllers/storagepolicyquota 97%
github.com/vmware-tanzu/vm-operator/controllers/util/encoding 73%
github.com/vmware-tanzu/vm-operator/controllers/virtualmachine/storagepolicyusage 98%
github.com/vmware-tanzu/vm-operator/controllers/virtualmachine/virtualmachine 70%
github.com/vmware-tanzu/vm-operator/controllers/virtualmachine/volume 87%
github.com/vmware-tanzu/vm-operator/controllers/virtualmachineclass 75%
github.com/vmware-tanzu/vm-operator/controllers/virtualmachineimagecache 89%
github.com/vmware-tanzu/vm-operator/controllers/virtualmachinepublishrequest 81%
github.com/vmware-tanzu/vm-operator/controllers/virtualmachinereplicaset 68%
github.com/vmware-tanzu/vm-operator/controllers/virtualmachineservice 83%
github.com/vmware-tanzu/vm-operator/controllers/virtualmachineservice/providers 92%
github.com/vmware-tanzu/vm-operator/controllers/virtualmachinesetresourcepolicy 82%
github.com/vmware-tanzu/vm-operator/controllers/virtualmachinewebconsolerequest 72%
github.com/vmware-tanzu/vm-operator/controllers/virtualmachinewebconsolerequest/v1alpha1 72%
github.com/vmware-tanzu/vm-operator/controllers/virtualmachinewebconsolerequest/v1alpha1/conditions 88%
github.com/vmware-tanzu/vm-operator/controllers/virtualmachinewebconsolerequest/v1alpha1/patch 78%
github.com/vmware-tanzu/vm-operator/pkg/bitmask 100%
github.com/vmware-tanzu/vm-operator/pkg/builder 95%
github.com/vmware-tanzu/vm-operator/pkg/conditions 90%
github.com/vmware-tanzu/vm-operator/pkg/config 100%
github.com/vmware-tanzu/vm-operator/pkg/config/capabilities 100%
github.com/vmware-tanzu/vm-operator/pkg/config/env 100%
github.com/vmware-tanzu/vm-operator/pkg/context/generic 100%
github.com/vmware-tanzu/vm-operator/pkg/context/operation 100%
github.com/vmware-tanzu/vm-operator/pkg/errors 100%
github.com/vmware-tanzu/vm-operator/pkg/exit 100%
github.com/vmware-tanzu/vm-operator/pkg/mem 100%
github.com/vmware-tanzu/vm-operator/pkg/patch 78%
github.com/vmware-tanzu/vm-operator/pkg/prober 91%
github.com/vmware-tanzu/vm-operator/pkg/prober/probe 90%
github.com/vmware-tanzu/vm-operator/pkg/prober/worker 77%
github.com/vmware-tanzu/vm-operator/pkg/providers/vsphere 76%
github.com/vmware-tanzu/vm-operator/pkg/providers/vsphere/client 80%
github.com/vmware-tanzu/vm-operator/pkg/providers/vsphere/clustermodules 73%
github.com/vmware-tanzu/vm-operator/pkg/providers/vsphere/config 88%
github.com/vmware-tanzu/vm-operator/pkg/providers/vsphere/contentlibrary 73%
github.com/vmware-tanzu/vm-operator/pkg/providers/vsphere/credentials 100%
github.com/vmware-tanzu/vm-operator/pkg/providers/vsphere/network 80%
github.com/vmware-tanzu/vm-operator/pkg/providers/vsphere/placement 80%
github.com/vmware-tanzu/vm-operator/pkg/providers/vsphere/session 71%
github.com/vmware-tanzu/vm-operator/pkg/providers/vsphere/storage 44%
github.com/vmware-tanzu/vm-operator/pkg/providers/vsphere/sysprep 100%
github.com/vmware-tanzu/vm-operator/pkg/providers/vsphere/vcenter 85%
github.com/vmware-tanzu/vm-operator/pkg/providers/vsphere/virtualmachine 85%
github.com/vmware-tanzu/vm-operator/pkg/providers/vsphere/vmlifecycle 64%
github.com/vmware-tanzu/vm-operator/pkg/record 87%
github.com/vmware-tanzu/vm-operator/pkg/topology 91%
github.com/vmware-tanzu/vm-operator/pkg/util 88%
github.com/vmware-tanzu/vm-operator/pkg/util/cloudinit 89%
github.com/vmware-tanzu/vm-operator/pkg/util/cloudinit/validate 91%
github.com/vmware-tanzu/vm-operator/pkg/util/image 100%
github.com/vmware-tanzu/vm-operator/pkg/util/kube 89%
github.com/vmware-tanzu/vm-operator/pkg/util/kube/cource 100%
github.com/vmware-tanzu/vm-operator/pkg/util/kube/internal 100%
github.com/vmware-tanzu/vm-operator/pkg/util/kube/proxyaddr 73%
github.com/vmware-tanzu/vm-operator/pkg/util/kube/spq 100%
github.com/vmware-tanzu/vm-operator/pkg/util/netplan 100%
github.com/vmware-tanzu/vm-operator/pkg/util/ovfcache 75%
github.com/vmware-tanzu/vm-operator/pkg/util/ovfcache/internal 100%
github.com/vmware-tanzu/vm-operator/pkg/util/paused 100%
github.com/vmware-tanzu/vm-operator/pkg/util/ptr 100%
github.com/vmware-tanzu/vm-operator/pkg/util/resize 97%
github.com/vmware-tanzu/vm-operator/pkg/util/vmopv1 81%
github.com/vmware-tanzu/vm-operator/pkg/util/vsphere/client 64%
github.com/vmware-tanzu/vm-operator/pkg/util/vsphere/library 100%
github.com/vmware-tanzu/vm-operator/pkg/util/vsphere/vm 79%
github.com/vmware-tanzu/vm-operator/pkg/util/vsphere/watcher 88%
github.com/vmware-tanzu/vm-operator/pkg/vmconfig 95%
github.com/vmware-tanzu/vm-operator/pkg/vmconfig/crypto 91%
github.com/vmware-tanzu/vm-operator/pkg/webconsolevalidation 100%
github.com/vmware-tanzu/vm-operator/services/vm-watcher 93%
github.com/vmware-tanzu/vm-operator/webhooks/common 100%
github.com/vmware-tanzu/vm-operator/webhooks/persistentvolumeclaim/validation 95%
github.com/vmware-tanzu/vm-operator/webhooks/unifiedstoragequota/validation 89%
github.com/vmware-tanzu/vm-operator/webhooks/virtualmachine/mutation 87%
github.com/vmware-tanzu/vm-operator/webhooks/virtualmachine/validation 95%
github.com/vmware-tanzu/vm-operator/webhooks/virtualmachineclass/mutation 62%
github.com/vmware-tanzu/vm-operator/webhooks/virtualmachineclass/validation 89%
github.com/vmware-tanzu/vm-operator/webhooks/virtualmachinepublishrequest/validation 92%
github.com/vmware-tanzu/vm-operator/webhooks/virtualmachinereplicaset/validation 90%
github.com/vmware-tanzu/vm-operator/webhooks/virtualmachineservice/mutation 67%
github.com/vmware-tanzu/vm-operator/webhooks/virtualmachineservice/validation 92%
github.com/vmware-tanzu/vm-operator/webhooks/virtualmachinesetresourcepolicy/validation 89%
github.com/vmware-tanzu/vm-operator/webhooks/virtualmachinewebconsolerequest/v1alpha1/validation 92%
github.com/vmware-tanzu/vm-operator/webhooks/virtualmachinewebconsolerequest/validation 92%
Summary 83% (11471 / 13889)

Minimum allowed line rate is 79%

Copy link
Contributor

@aruneshpa aruneshpa left a comment

Choose a reason for hiding this comment

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

I didn't really review the actual lint fixes that carefully as I am guessing you are simply fixing the warnings. Rest looks good modulo a few questions.

@@ -48,6 +48,13 @@ PROJECT_SLUG := github.com/vmware-tanzu/vm-operator
# ROOT_DIR_IN_GOPATH is non-empty if ROOT_DIR is in the GOPATH.
ROOT_DIR_IN_GOPATH := $(findstring $(GOPATH)/src/$(PROJECT_SLUG),$(ROOT_DIR))

# R_WILDCARD recursively searches for a file that matches $1.
Copy link
Contributor

Choose a reason for hiding this comment

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

Matches $2 in $1 no?

@@ -48,6 +48,13 @@ PROJECT_SLUG := github.com/vmware-tanzu/vm-operator
# ROOT_DIR_IN_GOPATH is non-empty if ROOT_DIR is in the GOPATH.
ROOT_DIR_IN_GOPATH := $(findstring $(GOPATH)/src/$(PROJECT_SLUG),$(ROOT_DIR))

# R_WILDCARD recursively searches for a file that matches $1.
R_WILDCARD=$(wildcard $(1)$(2)) $(foreach DIR,$(wildcard $(1)*),$(call R_WILDCARD,$(DIR)/,$(2)))
Copy link
Contributor

Choose a reason for hiding this comment

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

Also, I think $(foreach DIR,$(wildcard $(1)*) also lists files in $1, so you should change it to $(foreach DIR,$(wildcard $(1)*/)?

continue; \
fi; \
echo "Running golangci-lint in $$dir"; \
(cd $$dir && $(GOLANGCI_LINT_ABS_PATH) run -v $(GOLANGCI_LINT_FLAGS)); \
Copy link
Contributor

Choose a reason for hiding this comment

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

Are GO_MOD_DIRS relative paths? If so I am curious how cd $$dir is working here since you pasted the output of running this target in your Testing Done section.

@@ -16,6 +16,7 @@ package utilconversion
import (
"math/rand"

//nolint:depguard
Copy link
Contributor

Choose a reason for hiding this comment

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

I am curious why we are hitting this. Is this now included in the allowed imports? Or this is because of deprecation etc?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/L Denotes a PR that changes 100-499 lines.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants