fix(cmd/gf): improve init command with version retry and gofmt support#4592
Merged
fix(cmd/gf): improve init command with version retry and gofmt support#4592
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR enhances the gf init command by adding version retry logic when a specified version fails to download, and ensures consistent code formatting across all generated project files.
- Added automatic version selection fallback when a user-specified version fails to download
- Integrated
utils.GoFmtto format generated Go files in both custom and built-in template flows - Replaced
go/printerwithgo/formatin AST replacer for standard formatting
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| cmd/gf/internal/cmd/geninit/geninit_generator.go | Added utils.GoFmt call to format all generated Go files after project generation |
| cmd/gf/internal/cmd/geninit/geninit_ast.go | Switched from printer.Config to format.Node for standard Go formatting in AST-based import replacement |
| cmd/gf/internal/cmd/geninit/geninit.go | Implemented version retry logic that offers version selection when a user-specified version download fails |
| cmd/gf/internal/cmd/cmd_init.go | Added utils.GoFmt call for built-in template path and updated usage examples to demonstrate version specification |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Contributor
Addresses feedback from [PR #4592](#4592 (comment)) regarding inefficient double formatting of Go files during project generation. ## Changes - Replace `format.Node` with `printer.Fprint` in AST replacer to skip intermediate formatting - Defer all formatting to final `utils.GoFmt` call which uses `golang.org/x/tools/imports` Previously, files modified by the AST replacer were formatted twice—once during import path replacement and again by the final `utils.GoFmt` call. Now the AST replacer writes unformatted AST, and a single `utils.GoFmt` pass handles both formatting and import organization. ```go // Before: formatted output if err := format.Node(&buf, r.fset, file); err != nil { return err } // After: unformatted output, formatting deferred if err := printer.Fprint(&buf, r.fset, file); err != nil { return err } ``` <!-- START COPILOT CODING AGENT TIPS --> --- ✨ Let Copilot coding agent [set things up for you](https://github.com/gogf/gf/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo. --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: hailaz <[email protected]>
houseme
approved these changes
Jan 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.