Skip to content

Run gopls modernize on the codebase#33739

Closed
silverwind wants to merge 4 commits into
go-gitea:mainfrom
silverwind:modernize
Closed

Run gopls modernize on the codebase#33739
silverwind wants to merge 4 commits into
go-gitea:mainfrom
silverwind:modernize

Conversation

@silverwind

@silverwind silverwind commented Feb 27, 2025

Copy link
Copy Markdown
Member

gopls has a internal modernize code action which we can run on the CLI which I have done here:

go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix ./...

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Feb 27, 2025
@silverwind silverwind added the type/refactoring Existing code has been cleaned up. There should be no new functionality. label Feb 27, 2025
@silverwind

silverwind commented Feb 27, 2025

Copy link
Copy Markdown
Member Author

It seems that at least one fix that was applied was unsafe. I suppose this is a bug in the module:

Error: services/actions/rerun.go:25:36: in call to slices.ContainsFunc, type func(values ...string) bool of rerunJobsIDSet.Contains does not match func(E) bool

Tool output:

$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix ./...

-: # code.gitea.io/gitea/services/actions
services/actions/rerun.go:25:36: in call to slices.ContainsFunc, type func(values ...string) bool of rerunJobsIDSet.Contains does not match func(E) bool
/Users/silverwind/git/gitea/services/actions/rerun.go:25:36: in call to slices.ContainsFunc, type func(values ...string) bool of rerunJobsIDSet.Contains does not match func(E) bool
-: # code.gitea.io/gitea/services/actions [code.gitea.io/gitea/models/db.test]
services/actions/rerun.go:25:36: in call to slices.ContainsFunc, type func(values ...string) bool of rerunJobsIDSet.Contains does not match func(E) bool
-: # code.gitea.io/gitea/services/actions [code.gitea.io/gitea/services/actions.test]
services/actions/rerun.go:25:36: in call to slices.ContainsFunc, type func(values ...string) bool of rerunJobsIDSet.Contains does not match func(E) bool
/Users/silverwind/git/gitea/services/actions/rerun.go:25:36: in call to slices.ContainsFunc, type func(values ...string) bool of rerunJobsIDSet.Contains does not match func(E) bool
exit status 1

@silverwind silverwind marked this pull request as draft February 27, 2025 11:11
@silverwind

silverwind commented Feb 27, 2025

Copy link
Copy Markdown
Member Author

Given the "internal" nature of this thing, maybe we should just take this as a one-time refactoring action for now. Maybe in the future modernize will become part of the official gopls CLI.

@delvh delvh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree with your assessment, it is fine as a one time action, but we should not add it to the CI while it is inofficial.

Comment on lines 82 to +83
// the config system may change the environment variables, so get a copy first, to be used later
env := append([]string{}, os.Environ()...)
env := os.Environ()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This change was also erroneous.
Although the code itself should probably use the maps.Clone function anyway…

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Reported upstream: golang/go#72009

@silverwind silverwind Feb 27, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Actually no, I think the fix is correct. os.Environ() returns string[], so maps.Clone will not work.

I think it does not make sense to clone a string[] here, or do you? I think even if the environment changes after this call, the value will stay the same.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's not a gopls bug. os.Environ always returns a new slice, so no need to "clone"

Comment thread modules/setting/mirror.go
} else {
Mirror.DefaultInterval = time.Hour * 8
}
Mirror.DefaultInterval = max(time.Hour*8, Mirror.MinInterval)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree with the tool, that's what the code is doing.
However, it's not what it should be doing:
I think the code is supposed to be

Suggested change
Mirror.DefaultInterval = max(time.Hour*8, Mirror.MinInterval)
Mirror.DefaultInterval = Mirror.MinInterval

Comparing against the default value looks strange

@silverwind silverwind Feb 27, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I hope to keep behaviour changes to a minimum in this PR.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, avoid unnecessary changes.

@silverwind

Copy link
Copy Markdown
Member Author

Okay, I have reverted all the tooling changes and added the command to run to the PR message above for future reference.

@silverwind silverwind changed the title Add gopls modernize formatting Run gopls modernize on the codebase Feb 27, 2025
Comment thread services/auth/source/oauth2/urlmapping.go
Comment thread modules/packages/npm/metadata.go
@silverwind

Copy link
Copy Markdown
Member Author

I think best course of action will be to report the bugs upstream to gopls and then re-run again here. Currently I don't have motivation to do that, so if someone can see the bugs introduced here, feel free to file gopls bugs.

@silverwind

Copy link
Copy Markdown
Member Author

I'll revisit this at a later date when modernize has hopefully worked out their bugs, it seems to be in active development, so I expect its quality to get better eventually.

@silverwind silverwind closed this Mar 9, 2025
@go-gitea go-gitea locked as resolved and limited conversation to collaborators Jun 7, 2025
@silverwind silverwind deleted the modernize branch June 17, 2025 17:29
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. type/refactoring Existing code has been cleaned up. There should be no new functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants