Skip to content

modernize sources #63

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

Merged
merged 6 commits into from
Apr 30, 2025
Merged

Conversation

sivchari
Copy link
Member

@sivchari sivchari commented Apr 12, 2025

copy #58

I executed modernize analyzer to this project.
This analyzer makes sources simplified.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Apr 12, 2025
@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Apr 12, 2025
@sivchari
Copy link
Member Author

/kind cleanup

@k8s-ci-robot k8s-ci-robot added the kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. label Apr 12, 2025
Copy link
Contributor

@JoelSpeed JoelSpeed left a comment

Choose a reason for hiding this comment

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

Do we need to upgrade to golangci-lint v2 to be able to use modernize? Are we going to regress if we merge this now without enforcing it within the configuration?

@everettraven
Copy link
Contributor

@sivchari thanks for creating this PR, but what gains do we get from doing this?

How are we going to enforce that new changes adhere to what the modernize analyzer would expect?

While it looks like there is a single change that may produce some minor performance benefits (i.e replacing Split in "for range strings.Split(...)" by go1.24's more efficient SplitSeq;), does the syntactic sugar of interface{} --> any (which seems like the largest change in the PR) actually do anything for us than make us more "modern"?

@k8s-ci-robot k8s-ci-robot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Apr 14, 2025
@sivchari
Copy link
Member Author

sivchari commented Apr 14, 2025

Hi, @everettraven

The motivation that I want to add modernize analyzer is to reduce reviewing pr about fixing tiny change.
If we introduce this linter, I believe we won't review such as replace interface{} with any.
In addition, the modernize is maintained by Go team, so we can forward to latest syntax.

@JoelSpeed

Do we need to upgrade to golangci-lint v2 to be able to use modernize? Are we going to regress if we merge this now without enforcing it within the configuration?

No, it's not necessary to migrate v2 since the linter is independent from golangci-lint.

@sivchari sivchari force-pushed the modernize-project branch from 278f09c to 54b14a6 Compare April 18, 2025 13:53
@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Apr 21, 2025
@sivchari sivchari force-pushed the modernize-project branch from 54b14a6 to cb4e912 Compare April 22, 2025 03:18
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Apr 22, 2025
go.mod Outdated
@@ -209,3 +210,5 @@ require (
mvdan.cc/gofumpt v0.7.0 // indirect
mvdan.cc/unparam v0.0.0-20250301125049-0df0534333a4 // indirect
)

tool golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we create a tools directory and module to include our tooling and keep the top level separate? We can move golangci-lint there too

Copy link
Member Author

@sivchari sivchari Apr 25, 2025

Choose a reason for hiding this comment

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

golang/go#73279 describes that modernize shouldn't be contained as dependency, then they recommended to just run by using go run. So I use tool directive for golangci-lint not modernize.
And it's not possible to run golangci-lint located in tools directory to root directory, so I added tools directory to root go.mod

- name: modernize
run: |
if [ -n "$(go tool modernize -diff ./...)" ]; then
echo "The changes can't be modernized. Please run `go tool modernize -fix ./...`"
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a Makefile target make modernize that runs this

@sivchari sivchari force-pushed the modernize-project branch 3 times, most recently from 00a5538 to e44a106 Compare April 25, 2025 16:56
@JoelSpeed
Copy link
Contributor

Should we be concerned about this rule from modernize?

replacing omitempty by omitzero on structs, added in go1.24;

Since omitempty is important in Kube APIs, I wonder if this will affect us.

I see that the modernize isn't complaining about test data at the moment, so that's good 🤔

Makefile Outdated
Comment on lines 43 to 49
.PHONY: lint
lint: ## Run golangci-lint over the codebase.
${GOLANGCI_LINT} run ./... --timeout 5m -v
go tool golangci-lint run ./... --timeout 5m -v

.PHONY: lint-fix
lint-fix: ## Run golangci-lint over the codebase and run auto-fixers if supported by the linter
${GOLANGCI_LINT} run ./... --timeout 5m -v --fix
go tool golangci-lint run ./../... --timeout 5m -v --fix
Copy link
Contributor

Choose a reason for hiding this comment

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

As an end user, I want to be able to run all linting tools with one target. So I'd expect make lint to run both modernize and golangci-lint.

Perhaps we need to adjust this so we have something like below, WDYT

lint: modernize golangci-lint

lint-fix: modernize-fix golangci-lint-fix

golangci-lint:
    go tool golangci-lint run ./../... --timeout 5m -v

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

@sivchari
Copy link
Member Author

This PR will be merged after #72 is merged.

Signed-off-by: sivchari <shibuuuu5@gmail.com>
Signed-off-by: sivchari <shibuuuu5@gmail.com>
Signed-off-by: sivchari <shibuuuu5@gmail.com>
Signed-off-by: sivchari <shibuuuu5@gmail.com>
@sivchari sivchari force-pushed the modernize-project branch from b41c987 to 38ed5cb Compare April 29, 2025 17:27
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Apr 29, 2025
Makefile Outdated

.PHONY: modernize
modernize: ## Run modernize on the codebase.
go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@v${MODERNIZE_VERSION} -diff ./...
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it not need to be added to the tools module? Like the tool directive for golangci-lint?

@sivchari sivchari force-pushed the modernize-project branch from df35c27 to efdc833 Compare April 30, 2025 13:39
Signed-off-by: sivchari <shibuuuu5@gmail.com>
Signed-off-by: sivchari <shibuuuu5@gmail.com>
@sivchari sivchari force-pushed the modernize-project branch from efdc833 to a45bde9 Compare April 30, 2025 13:40
@@ -74,12 +77,12 @@ require (
github.com/golangci/dupl v0.0.0-20250308024227-f665c8d69b32 // indirect
github.com/golangci/go-printf-func-name v0.1.0 // indirect
github.com/golangci/gofmt v0.0.0-20250106114630-d62b90e6713d // indirect
github.com/golangci/golangci-lint/v2 v2.1.5 // indirect
github.com/golangci/golangci-lint/v2 v2.0.2 // indirect
Copy link
Member Author

Choose a reason for hiding this comment

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

After v2.1.0, golangci-lint use golang.org/x/tools v0.32.0, but the modernize depending on the version must be failed since the distribution is broken ref. So I downgrade the golangci-lint from v2.1.5 to v2.0.2. The addition between 2 versions is almost about new feature or upgrading modules, so this downgrade doesn't happen critical changes.

@JoelSpeed
Copy link
Contributor

/approve
/lgtm

Thanks @sivchari

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 30, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: JoelSpeed, sivchari

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Apr 30, 2025
@k8s-ci-robot k8s-ci-robot merged commit 0c6bb03 into kubernetes-sigs:main Apr 30, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants