Skip to content

x/tools/gopls: command (perhaps 'go fix') to apply all safe fixes #73623

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

Open
silverwind opened this issue May 7, 2025 · 5 comments
Open

x/tools/gopls: command (perhaps 'go fix') to apply all safe fixes #73623

silverwind opened this issue May 7, 2025 · 5 comments
Labels
FeatureRequest Issues asking for a new feature that does not need a proposal. gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@silverwind
Copy link

silverwind commented May 7, 2025

The modernize analyzer has this handy CLI command to apply all fixes to a codebase:

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

I would like to request a similar CLI command that does the same, but for all analyzers. The closest command I have found is:

go run golang.org/x/tools/gopls@latest codeaction -kind=quickfix -exec -w <files>

Problem with that command is that it does only one fix per invocation, it needs to be ran multiple times for multiple fixes which is very slow and cumbersome to use.

@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels May 7, 2025
@gopherbot gopherbot added this to the Unreleased milestone May 7, 2025
@silverwind silverwind changed the title x/tools/gopls: cli command to apply all codeactions x/tools/gopls: cli command to apply all fixes May 7, 2025
@adonovan
Copy link
Member

adonovan commented May 7, 2025

This seems like a reasonable feature request. I see that you included -kind=quickfix, which is good: I would be nervous about applying all fixes from all analyzers without very careful review. Even the modernizers, which are intended to be safe to apply blindly, still fall short of that goal.

One way to implement this would be to produce a multichecker of all the analyses (much like the modernize command, which is a singlechecker for just one analysis), but the cumbersome go run command above, containing /internal/, was never intended to be a long term solution. Another would be to add interactive logic to gopls itself, similar to #73605. But I think a command-line tool makes sense, so the best solution would be to incorporate this functionality into the go fix subcommand, as I just proposed yesterday in #66370

@adonovan adonovan changed the title x/tools/gopls: cli command to apply all fixes x/tools/gopls: command (perhaps 'go fix') to apply all safe fixes May 7, 2025
@adonovan adonovan added FeatureRequest Issues asking for a new feature that does not need a proposal. Refactoring Issues related to refactoring tools and removed Refactoring Issues related to refactoring tools labels May 7, 2025
@silverwind
Copy link
Author

silverwind commented May 7, 2025

I would be nervous about applying all fixes from all analyzers without very careful review.

Yes of course one must never blindly commit autofixed code without review. I've seen it first-hand that these fixes are not perfect and sometimes break code. Maybe add a disclaimer in the help text.

but the cumbersome go run command above, containing /internal/, was never intended to be a long term solution

Yeah, the current "internal" command gave me a feeling it's not something meant for long-term public consumption.

incorporate this functionality into the go fix subcommand

I agree, it would best fit into the existing (but deprecated) gopls fix subcommand.

@silverwind
Copy link
Author

silverwind commented May 7, 2025

One possible concern about a fix subcommand is how it should output the unfixable issues. Similar tools like for example eslint have the fixing action as a option to the checking.

For gopls that would be gopls check -fix, meaning it would check the code, fix what is automatically fixable, and then output the remaining issues in the same format as the regular check subcommand.

@adonovan
Copy link
Member

adonovan commented May 7, 2025

I agree, it would best fit into the existing (but deprecated) gopls fix subcommand.

Interesting; I said go fix, but we could revive gopls fix too, for analyzers that are part of gopls but not part of the subset that ends up in go fix. Ideally the commands would behave identically except for the analyzer set.

@silverwind
Copy link
Author

go fix is new to me, maybe it could include the gopls fixes.

It's slightly concerning to me that it being part of the go command means one can't easily specify which version to run, like with go run golang.org/x/tools/gopls@version, so I guess I would prefer using it via either gopls fix and/or gopls check -fix for initial experimentation, and possibly later use it via go fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest Issues asking for a new feature that does not need a proposal. gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

3 participants