diff --git a/changelog.md b/changelog.md index 270190c06f..3b1666ab2f 100644 --- a/changelog.md +++ b/changelog.md @@ -5,6 +5,7 @@ ### Features - [#4509](https://github.com/ignite/cli/pull/4509) Upgrade to Go 1.24. Running `ignite doctor` migrates the scaffolded `tools.go` to the tool directive in the go.mod +- [#4588](https://github.com/ignite/cli/pull/4588) Run `buf format after scaffold proto files. ### Changes diff --git a/ignite/pkg/cosmosbuf/buf.go b/ignite/pkg/cosmosbuf/buf.go index 14ba384c60..ce42a1fd7f 100644 --- a/ignite/pkg/cosmosbuf/buf.go +++ b/ignite/pkg/cosmosbuf/buf.go @@ -19,7 +19,6 @@ import ( ) const ( - binaryName = "buf" flagTemplate = "template" flagOutput = "output" flagErrorFormat = "error-format" @@ -28,6 +27,7 @@ const ( flagBufGenYaml = "buf-gen-yaml" flagIncludeImports = "include-imports" flagIncludeWellKnownTypes = "include-wkt" + flagWrite = "write" flagPath = "path" fmtJSON = "json" bufGenPrefix = "buf.gen." @@ -35,6 +35,7 @@ const ( // CMDGenerate generate command. CMDGenerate Command = "generate" CMDExport Command = "export" + CMDFormat Command = "format" CMDConfig Command = "config" CMDDep Command = "dep" @@ -45,6 +46,7 @@ var ( commands = map[Command]struct{}{ CMDGenerate: {}, CMDExport: {}, + CMDFormat: {}, CMDConfig: {}, CMDDep: {}, } @@ -211,6 +213,19 @@ func (b Buf) Export(ctx context.Context, protoDir, output string) error { return b.runCommand(ctx, cmd...) } +// Format runs the buf Format command for the files in the provided path. +func (b Buf) Format(ctx context.Context, path string) error { + flags := map[string]string{ + flagWrite: "true", + } + cmd, err := b.command(CMDFormat, flags, path) + if err != nil { + return err + } + + return b.runCommand(ctx, cmd...) +} + // Generate runs the buf Generate command for each file into the proto directory. func (b Buf) Generate( ctx context.Context, diff --git a/ignite/pkg/cosmosgen/cosmosgen.go b/ignite/pkg/cosmosgen/cosmosgen.go index c9e44a2337..15f9093136 100644 --- a/ignite/pkg/cosmosgen/cosmosgen.go +++ b/ignite/pkg/cosmosgen/cosmosgen.go @@ -123,13 +123,13 @@ func (g *generator) cleanup() { // Generate generates code from protoDir of an SDK app residing at appPath with given options. // protoDir must be relative to the projectPath. func Generate(ctx context.Context, cacheStorage cache.Storage, appPath, protoDir, goModPath string, options ...Option) error { - b, err := cosmosbuf.New(cacheStorage, goModPath) + buf, err := cosmosbuf.New(cacheStorage, goModPath) if err != nil { return err } g := &generator{ - buf: b, + buf: buf, appPath: appPath, protoDir: protoDir, goModPath: goModPath, diff --git a/ignite/services/scaffolder/scaffolder.go b/ignite/services/scaffolder/scaffolder.go index 439d4ec61c..58bdc31516 100644 --- a/ignite/services/scaffolder/scaffolder.go +++ b/ignite/services/scaffolder/scaffolder.go @@ -12,6 +12,7 @@ import ( chainconfig "github.com/ignite/cli/v29/ignite/config/chain" "github.com/ignite/cli/v29/ignite/pkg/cache" "github.com/ignite/cli/v29/ignite/pkg/cosmosanalysis" + "github.com/ignite/cli/v29/ignite/pkg/cosmosbuf" "github.com/ignite/cli/v29/ignite/pkg/cosmosgen" "github.com/ignite/cli/v29/ignite/pkg/cosmosver" "github.com/ignite/cli/v29/ignite/pkg/errors" @@ -93,7 +94,7 @@ func (s Scaffolder) PostScaffold(ctx context.Context, cacheStorage cache.Storage return PostScaffold(ctx, cacheStorage, s.appPath, s.protoDir, s.modpath.RawPath, skipProto) } -func PostScaffold(ctx context.Context, cacheStorage cache.Storage, path, protoDir, gomodPath string, skipProto bool) error { +func PostScaffold(ctx context.Context, cacheStorage cache.Storage, path, protoDir, goModPath string, skipProto bool) error { wd, err := os.Getwd() if err != nil { return errors.Errorf("failed to get current working directory: %w", err) @@ -105,11 +106,12 @@ func PostScaffold(ctx context.Context, cacheStorage cache.Storage, path, protoDi } if !skipProto { + // go mod tidy prior and after the proto generation is required. if err := gocmd.ModTidy(ctx, path); err != nil { return err } - if err := protoc(ctx, cacheStorage, path, protoDir, gomodPath); err != nil { + if err := protoc(ctx, cacheStorage, path, protoDir, goModPath); err != nil { return err } } @@ -134,7 +136,7 @@ func PostScaffold(ctx context.Context, cacheStorage cache.Storage, path, protoDi return nil } -func protoc(ctx context.Context, cacheStorage cache.Storage, projectPath, protoDir, gomodPath string) error { +func protoc(ctx context.Context, cacheStorage cache.Storage, projectPath, protoDir, goModPath string) error { confpath, err := chainconfig.LocateDefault(projectPath) if err != nil { return err @@ -174,5 +176,14 @@ func protoc(ctx context.Context, cacheStorage cache.Storage, projectPath, protoD options = append(options, cosmosgen.WithOpenAPIGeneration(openAPIPath)) } - return cosmosgen.Generate(ctx, cacheStorage, projectPath, protoDir, gomodPath, options...) + if err := cosmosgen.Generate(ctx, cacheStorage, projectPath, protoDir, goModPath, options...); err != nil { + return err + } + + buf, err := cosmosbuf.New(cacheStorage, goModPath) + if err != nil { + return err + } + + return buf.Format(ctx, projectPath) }