From c335ea4d4654b8132b965ffc957081958e252297 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Mon, 3 Feb 2025 20:06:45 -0300 Subject: [PATCH 1/7] use bug.gen.yaml v2 --- ignite/cmd/chain.go | 5 ++++- ignite/pkg/cosmosbuf/buf.go | 15 +++++++++++++++ ignite/services/chain/proto.go | 17 ++++++++++++----- .../app/files/{{protoDir}}/buf.gen.gogo.yaml | 9 ++++----- .../app/files/{{protoDir}}/buf.gen.sta.yaml | 4 ++-- .../app/files/{{protoDir}}/buf.gen.swagger.yaml | 6 +++--- .../app/files/{{protoDir}}/buf.gen.ts.yaml | 4 ++-- 7 files changed, 42 insertions(+), 18 deletions(-) diff --git a/ignite/cmd/chain.go b/ignite/cmd/chain.go index 02dc27992d..0cab9481d0 100644 --- a/ignite/cmd/chain.go +++ b/ignite/cmd/chain.go @@ -192,11 +192,14 @@ func toolsMigrationPreRunHandler(cmd *cobra.Command, session *cliui.Session, app func bufMigrationPreRunHandler(cmd *cobra.Command, session *cliui.Session, appPath, protoDir string) error { // check if the buf files exist. - hasFiles, err := chain.CheckBufFiles(appPath, protoDir) + hasFiles, needMigration, err := chain.CheckBufFiles(appPath, protoDir) if err != nil { return err } + if needMigration { + + } if !hasFiles { if !getYes(cmd) { if err := session.AskConfirm(msgMigrationBuf); err != nil { diff --git a/ignite/pkg/cosmosbuf/buf.go b/ignite/pkg/cosmosbuf/buf.go index d7a2a1081f..5d25970c63 100644 --- a/ignite/pkg/cosmosbuf/buf.go +++ b/ignite/pkg/cosmosbuf/buf.go @@ -26,6 +26,7 @@ const ( flagOutput = "output" flagErrorFormat = "error-format" flagLogFormat = "log-format" + flagWorkspace = "workspace" flagIncludeImports = "include-imports" flagIncludeWellKnownTypes = "include-wkt" flagPath = "path" @@ -33,6 +34,7 @@ const ( // CMDGenerate generate command. CMDGenerate Command = "generate" + CMDMigrate Command = "migrate" CMDExport Command = "export" CMDDep Command = "dep" @@ -42,6 +44,7 @@ const ( var ( commands = map[Command]struct{}{ CMDGenerate: {}, + CMDMigrate: {}, CMDExport: {}, CMDDep: {}, } @@ -167,6 +170,18 @@ func (b Buf) Update(ctx context.Context, modDir string) error { return b.runCommand(ctx, cmd...) } +// Migrate runs the buf Migrate command for the files in the proto directory. +func (b Buf) Migrate(ctx context.Context, appDir string) error { + flags := map[string]string{ + flagWorkspace: appDir, + } + cmd, err := b.command(CMDMigrate, flags, "config") + if err != nil { + return err + } + return b.runCommand(ctx, cmd...) +} + // Export runs the buf Export command for the files in the proto directory. func (b Buf) Export(ctx context.Context, protoDir, output string) error { files, err := xos.FindFilesExtension(protoDir, xos.ProtoFile) diff --git a/ignite/services/chain/proto.go b/ignite/services/chain/proto.go index a157a1763e..4d0c2dfe87 100644 --- a/ignite/services/chain/proto.go +++ b/ignite/services/chain/proto.go @@ -9,6 +9,9 @@ import ( "github.com/ignite/cli/v29/ignite/templates/app" ) +// oldBufWorkFile represents the v1 buf work file, may this check should be remove in v30 +const oldBufWorkFile = "buf.work.yaml" + // CheckBufProtoDir check if the proto path exist into the directory list in the buf.work.yaml file. func CheckBufProtoDir(appPath, protoDir string) (bool, []string, error) { bufCfg, err := cosmosbuf.ParseBufConfig(appPath) @@ -44,11 +47,15 @@ func RemoveBufProtoDirs(appPath string, protoDirs ...string) error { return workFile.RemoveProtoDirs(protoDirs...) } -// CheckBufFiles check if the buf files exist. -func CheckBufFiles(appPath, protoDir string) (bool, error) { +// CheckBufFiles check if the buf files exist, and if needs a migration to v2. +func CheckBufFiles(appPath, protoDir string) (bool, bool, error) { files, err := app.BufFiles() if err != nil { - return false, nil + return false, false, nil + } + // if the buf.work.yaml exist, we only need the migration + if xos.FileExists(filepath.Join(appPath, oldBufWorkFile)) { + return false, true, nil } for _, bufFile := range files { bufFile, ok := app.CutTemplatePrefix(bufFile) @@ -56,10 +63,10 @@ func CheckBufFiles(appPath, protoDir string) (bool, error) { bufFile = filepath.Join(protoDir, bufFile) } if !xos.FileExists(filepath.Join(appPath, bufFile)) { - return false, nil + return false, false, nil } } - return true, nil + return true, false, nil } // BoxBufFiles box all buf files. diff --git a/ignite/templates/app/files/{{protoDir}}/buf.gen.gogo.yaml b/ignite/templates/app/files/{{protoDir}}/buf.gen.gogo.yaml index f1af034c7f..bad929cc94 100644 --- a/ignite/templates/app/files/{{protoDir}}/buf.gen.gogo.yaml +++ b/ignite/templates/app/files/{{protoDir}}/buf.gen.gogo.yaml @@ -3,16 +3,15 @@ # # buf.gen.gogo.yaml # -version: v1 +version: v2 plugins: - - name: gocosmos + - local: protoc-gen-gocosmos out: . opt: - plugins=grpc - - Mgoogle/protobuf/any.proto=github.com/cosmos/gogoproto/types/any + - Mgoogle/protobuf/any.proto=github.com/cosmos/cosmos-sdk/codec/types - Mcosmos/orm/v1/orm.proto=cosmossdk.io/orm - - Mcosmos/app/v1alpha1/module.proto=cosmossdk.io/api/cosmos/app/v1alpha1 - - name: grpc-gateway + - local: protoc-gen-grpc-gateway out: . opt: - logtostderr=true diff --git a/ignite/templates/app/files/{{protoDir}}/buf.gen.sta.yaml b/ignite/templates/app/files/{{protoDir}}/buf.gen.sta.yaml index 4444f5e75c..016ed92655 100644 --- a/ignite/templates/app/files/{{protoDir}}/buf.gen.sta.yaml +++ b/ignite/templates/app/files/{{protoDir}}/buf.gen.sta.yaml @@ -3,9 +3,9 @@ # # buf.gen.sta.yaml # -version: v1 +version: v2 plugins: - - name: openapiv2 + - local: protoc-gen-openapiv2 out: . opt: - logtostderr=true diff --git a/ignite/templates/app/files/{{protoDir}}/buf.gen.swagger.yaml b/ignite/templates/app/files/{{protoDir}}/buf.gen.swagger.yaml index 58d30d86e8..f3148ab8d3 100644 --- a/ignite/templates/app/files/{{protoDir}}/buf.gen.swagger.yaml +++ b/ignite/templates/app/files/{{protoDir}}/buf.gen.swagger.yaml @@ -3,12 +3,12 @@ # # buf.gen.swagger.yaml # -version: v1 +version: v2 plugins: - - name: openapiv2 + - local: protoc-gen-openapiv2 out: . opt: - logtostderr=true - openapi_naming_strategy=fqn - json_names_for_fields=false - - generate_unbound_methods=true \ No newline at end of file + - generate_unbound_methods=true diff --git a/ignite/templates/app/files/{{protoDir}}/buf.gen.ts.yaml b/ignite/templates/app/files/{{protoDir}}/buf.gen.ts.yaml index c484fb3ad3..bc2f01b93c 100644 --- a/ignite/templates/app/files/{{protoDir}}/buf.gen.ts.yaml +++ b/ignite/templates/app/files/{{protoDir}}/buf.gen.ts.yaml @@ -3,11 +3,11 @@ # # buf.gen.ts.yaml # -version: v1 +version: v2 managed: enabled: true plugins: - - plugin: buf.build/community/stephenh-ts-proto + - remote: buf.build/community/stephenh-ts-proto out: . opt: - logtostderr=true From 37d0a8f49ed4af1fe8afb16cb2cc7db731584092 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Mon, 3 Feb 2025 23:28:17 -0300 Subject: [PATCH 2/7] fix the migrate. command --- ignite/cmd/chain.go | 22 +++++++- ignite/pkg/cosmosbuf/buf.go | 34 ++++++++---- ignite/pkg/cosmosgen/generate_openapi.go | 2 +- ignite/pkg/xos/files.go | 53 +++++++++++++----- ignite/pkg/xos/files_test.go | 68 +++++++++++++++++++++--- ignite/services/chain/proto.go | 2 +- 6 files changed, 149 insertions(+), 32 deletions(-) diff --git a/ignite/cmd/chain.go b/ignite/cmd/chain.go index 0cab9481d0..2d1dd196f2 100644 --- a/ignite/cmd/chain.go +++ b/ignite/cmd/chain.go @@ -14,6 +14,7 @@ import ( "github.com/ignite/cli/v29/ignite/pkg/cliui" "github.com/ignite/cli/v29/ignite/pkg/cliui/colors" "github.com/ignite/cli/v29/ignite/pkg/cliui/icons" + "github.com/ignite/cli/v29/ignite/pkg/cosmosbuf" "github.com/ignite/cli/v29/ignite/pkg/cosmosgen" "github.com/ignite/cli/v29/ignite/pkg/errors" "github.com/ignite/cli/v29/ignite/pkg/goanalysis" @@ -28,13 +29,17 @@ const ( msgMigrationPrefix = "Your blockchain config version is v%d and the latest is v%d." msgMigrationPrompt = "Would you like to upgrade your config file to v%d" msgMigrationBuf = "Now ignite supports the `buf.build` (https://buf.build) registry to manage the protobuf dependencies. The embed protoc binary was deprecated and, your blockchain is still using it. Would you like to upgrade and add the `buf.build` config files to `proto/` folder" + msgMigrationBufV2 = "Now ignite supports the new `buf.build` (https://buf.build) v2 configuration. Would you like to upgrade your buf files to v2 using the `buf config migrate` command" msgMigrationBufProtoDir = "Ignite proto directory path from the chain config doesn't match the proto directory path from the chain `buf.work.yaml`. Do you want to add the proto path `%[1]v` to the directories list from the buf work file" msgMigrationBufProtoDirs = "Chain `buf.work.yaml` file contains directories that don't exist anymore (%[1]v). Do you want to delete them" msgMigrationAddTools = "Some required imports are missing in %s file: %s. Would you like to add them" msgMigrationRemoveTools = "File %s contains deprecated imports: %s. Would you like to remove them" ) -var ErrProtocUnsupported = errors.New("code generation using protoc is only supported by Ignite CLI v0.26.1 or older") +var ( + ErrProtocUnsupported = errors.New("code generation using protoc is only supported by Ignite CLI v0.26.1 or older") + ErrBufConfigVersionUnsupported = errors.New("buf config version must be v2") +) // NewChain returns a command that groups sub commands related to compiling, serving // blockchains and so on. @@ -198,8 +203,23 @@ func bufMigrationPreRunHandler(cmd *cobra.Command, session *cliui.Session, appPa } if needMigration { + if !getYes(cmd) { + if err := session.AskConfirm(msgMigrationBufV2); err != nil { + return ErrBufConfigVersionUnsupported + } + } + cacheStorage, err := newCache(cmd) + b, err := cosmosbuf.New(cacheStorage, appPath) + if err != nil { + return err + } + + if err := b.Migrate(cmd.Context(), protoDir); err != nil { + return err + } } + if !hasFiles { if !getYes(cmd) { if err := session.AskConfirm(msgMigrationBuf); err != nil { diff --git a/ignite/pkg/cosmosbuf/buf.go b/ignite/pkg/cosmosbuf/buf.go index 5d25970c63..bd1e1205bf 100644 --- a/ignite/pkg/cosmosbuf/buf.go +++ b/ignite/pkg/cosmosbuf/buf.go @@ -27,15 +27,17 @@ const ( flagErrorFormat = "error-format" flagLogFormat = "log-format" flagWorkspace = "workspace" + flagBufGenYaml = "buf-gen-yaml" flagIncludeImports = "include-imports" flagIncludeWellKnownTypes = "include-wkt" flagPath = "path" fmtJSON = "json" + bufGenPrefix = "buf.gen." // CMDGenerate generate command. CMDGenerate Command = "generate" - CMDMigrate Command = "migrate" CMDExport Command = "export" + CMDConfig Command = "config" CMDDep Command = "dep" specCacheNamespace = "generate.buf" @@ -44,8 +46,8 @@ const ( var ( commands = map[Command]struct{}{ CMDGenerate: {}, - CMDMigrate: {}, CMDExport: {}, + CMDConfig: {}, CMDDep: {}, } @@ -155,7 +157,7 @@ func (c Command) String() string { // Update updates module dependencies. // By default updates all dependencies unless one or more dependencies are specified. func (b Buf) Update(ctx context.Context, modDir string) error { - files, err := xos.FindFilesExtension(modDir, xos.ProtoFile) + files, err := xos.FindFiles(modDir, xos.WithExtension(xos.ProtoFile)) if err != nil { return err } @@ -170,12 +172,26 @@ func (b Buf) Update(ctx context.Context, modDir string) error { return b.runCommand(ctx, cmd...) } -// Migrate runs the buf Migrate command for the files in the proto directory. -func (b Buf) Migrate(ctx context.Context, appDir string) error { +// Migrate runs the buf Migrate command for the files in the app directory. +func (b Buf) Migrate(ctx context.Context, protoDir string) error { + yamlFiles, err := xos.FindFiles(protoDir, xos.WithExtension(xos.YAMLFile), xos.WithPrefix(bufGenPrefix)) + if err != nil { + return err + } + ymlfiles, err := xos.FindFiles(protoDir, xos.WithExtension(xos.YMLFile), xos.WithPrefix(bufGenPrefix)) + if err != nil { + return err + } + yamlFiles = append(yamlFiles, ymlfiles...) + flags := map[string]string{ - flagWorkspace: appDir, + flagWorkspace: ".", } - cmd, err := b.command(CMDMigrate, flags, "config") + if len(yamlFiles) > 0 { + flags[flagBufGenYaml] = strings.Join(yamlFiles, ",") + } + + cmd, err := b.command(CMDConfig, flags, "migrate") if err != nil { return err } @@ -184,7 +200,7 @@ func (b Buf) Migrate(ctx context.Context, appDir string) error { // Export runs the buf Export command for the files in the proto directory. func (b Buf) Export(ctx context.Context, protoDir, output string) error { - files, err := xos.FindFilesExtension(protoDir, xos.ProtoFile) + files, err := xos.FindFiles(protoDir, xos.WithExtension(xos.ProtoFile)) if err != nil { return err } @@ -217,7 +233,7 @@ func (b Buf) Generate( } // find all proto files into the path. - foundFiles, err := xos.FindFilesExtension(protoPath, xos.ProtoFile) + foundFiles, err := xos.FindFiles(protoPath, xos.WithExtension(xos.ProtoFile)) if err != nil || len(foundFiles) == 0 { return err } diff --git a/ignite/pkg/cosmosgen/generate_openapi.go b/ignite/pkg/cosmosgen/generate_openapi.go index ca89ca7faf..3fff16da03 100644 --- a/ignite/pkg/cosmosgen/generate_openapi.go +++ b/ignite/pkg/cosmosgen/generate_openapi.go @@ -94,7 +94,7 @@ func (g *generator) generateOpenAPISpec(ctx context.Context) error { return errors.Wrapf(err, "failed to generate openapi spec %s, probally you need to exclude some proto files", protoPath) } - specs, err := xos.FindFilesExtension(dir, xos.JSONFile) + specs, err := xos.FindFiles(dir, xos.WithExtension(xos.JSONFile)) if err != nil { return err } diff --git a/ignite/pkg/xos/files.go b/ignite/pkg/xos/files.go index 0ef8ea661c..e4f875698f 100644 --- a/ignite/pkg/xos/files.go +++ b/ignite/pkg/xos/files.go @@ -4,39 +4,64 @@ import ( "fmt" "os" "path/filepath" + "strings" ) const ( JSONFile = "json" ProtoFile = "proto" + YAMLFile = "yaml" + YMLFile = "yml" ) -func FindFiles(directory string) ([]string, error) { +type findFileOptions struct { + extension string + prefix string +} + +type FindFileOptions func(o *findFileOptions) + +func WithExtension(extension string) FindFileOptions { + return func(o *findFileOptions) { + o.extension = extension + } +} + +func WithPrefix(prefix string) FindFileOptions { + return func(o *findFileOptions) { + o.prefix = prefix + } +} + +// FindFiles searches for files in the specified directory based on the given options. +// It supports filtering files by extension and prefix. Returns a list of matching files or an error. +func FindFiles(directory string, options ...FindFileOptions) ([]string, error) { + opts := findFileOptions{} + for _, apply := range options { + apply(&opts) + } + files := make([]string, 0) return files, filepath.Walk(directory, func(path string, info os.FileInfo, err error) error { if err != nil { return err } - if !info.IsDir() { - files = append(files, path) + // Filter by file extension if provided + if opts.extension != "" && filepath.Ext(path) != fmt.Sprintf(".%s", opts.extension) { + return nil // Skip files that don't match the extension } - return nil - }) -} -func FindFilesExtension(directory, extension string) ([]string, error) { - files := make([]string, 0) - return files, filepath.Walk(directory, func(path string, info os.FileInfo, err error) error { - if err != nil { - return err + // Filter by file prefix if provided + if opts.prefix != "" && !strings.HasPrefix(filepath.Base(path), opts.prefix) { + return nil // Skip files that don't match the prefix } + // Add file to the result list if it is not a directory if !info.IsDir() { - if filepath.Ext(path) == fmt.Sprintf(".%s", extension) { - files = append(files, path) - } + files = append(files, path) } + return nil }) } diff --git a/ignite/pkg/xos/files_test.go b/ignite/pkg/xos/files_test.go index cbd5a34903..fcad2e8470 100644 --- a/ignite/pkg/xos/files_test.go +++ b/ignite/pkg/xos/files_test.go @@ -16,44 +16,92 @@ func TestFindFiles(t *testing.T) { name string files []string extension string + prefix string want []string err error }{ { - name: "test 3 json files", + name: "test zero files", + files: []string{}, + want: []string{}, + err: nil, + }, + { + name: "test one file", + files: []string{"file.json"}, + want: []string{"file.json"}, + err: nil, + }, + { + name: "test 3 files", + files: []string{"file1.json", "file2.txt", "file3.json"}, + want: []string{"file1.json", "file2.txt", "file3.json"}, + err: nil, + }, + { + name: "test file prefix", + files: []string{"file.prefix.test.json"}, + prefix: "file.prefix", + want: []string{"file.prefix.test.json"}, + err: nil, + }, + { + name: "test bigger file prefix", + files: []string{"file.prefix.test.json"}, + prefix: "file.prefix.test", + want: []string{"file.prefix.test.json"}, + err: nil, + }, + { + name: "test 3 files prefix", + files: []string{"test.file1.json", "test.file2.txt", "test.file3.json"}, + prefix: "test.file", + want: []string{"test.file1.json", "test.file2.txt", "test.file3.json"}, + err: nil, + }, + { + name: "test 3 extension json files", files: []string{"file1.json", "file2.txt", "file3.json", "file4.json"}, extension: "json", want: []string{"file1.json", "file3.json", "file4.json"}, err: nil, }, { - name: "test 3 json files with subfolder", + name: "test 3 extension json files with subfolder", files: []string{"testdata/file1.json", "file2.txt", "foo/file3.json", "file4.json"}, extension: "json", want: []string{"testdata/file1.json", "foo/file3.json", "file4.json"}, err: nil, }, { - name: "test 1 txt files", + name: "test 1 extension txt files", files: []string{"file1.json", "file2.txt", "file3.json", "file4.json"}, extension: "txt", want: []string{"file2.txt"}, err: nil, }, { - name: "test 1 json files", + name: "test 1 extension json files", files: []string{"file1.json"}, extension: "json", want: []string{"file1.json"}, err: nil, }, { - name: "test no files", + name: "test invalid files extension", files: []string{"file1.json", "file2.json", "file3.json", "file4.json"}, extension: "txt", want: []string{}, err: nil, }, + { + name: "test file prefix and extension", + files: []string{"test.file1.json", "test.file2.txt", "test.file3.json"}, + prefix: "test.file", + extension: "json", + want: []string{"test.file1.json", "test.file3.json"}, + err: nil, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -72,7 +120,15 @@ func TestFindFiles(t *testing.T) { require.NoError(t, file.Close()) } - gotFiles, err := xos.FindFilesExtension(tempDir, tt.extension) + opts := make([]xos.FindFileOptions, 0) + if tt.prefix != "" { + opts = append(opts, xos.WithPrefix(tt.prefix)) + } + if tt.extension != "" { + opts = append(opts, xos.WithExtension(tt.extension)) + } + + gotFiles, err := xos.FindFiles(tempDir, opts...) if tt.err != nil { require.Error(t, err) require.ErrorIs(t, err, tt.err) diff --git a/ignite/services/chain/proto.go b/ignite/services/chain/proto.go index 4d0c2dfe87..5dba7860d6 100644 --- a/ignite/services/chain/proto.go +++ b/ignite/services/chain/proto.go @@ -55,7 +55,7 @@ func CheckBufFiles(appPath, protoDir string) (bool, bool, error) { } // if the buf.work.yaml exist, we only need the migration if xos.FileExists(filepath.Join(appPath, oldBufWorkFile)) { - return false, true, nil + return true, true, nil } for _, bufFile := range files { bufFile, ok := app.CutTemplatePrefix(bufFile) From fde0a9d4926fc5b6f34cb1103ecbd5e56dbd0dba Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Mon, 3 Feb 2025 23:31:04 -0300 Subject: [PATCH 3/7] add changelog --- changelog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/changelog.md b/changelog.md index 580c0676f7..f06e88240e 100644 --- a/changelog.md +++ b/changelog.md @@ -24,6 +24,7 @@ - [#4289](https://github.com/ignite/cli/pull/4289), [#4423](https://github.com/ignite/cli/pull/4423), [#4432](https://github.com/ignite/cli/pull/4432) Cosmos SDK v0.52 support - [#4477](https://github.com/ignite/cli/pull/4477) IBC v10 support - [#4166](https://github.com/ignite/cli/issues/4166) Migrate buf config files to v2 +- [#4494](https://github.com/ignite/cli/pull/4494) Automatic migrate the buf configs to v2 ### Changes From cf560029b8a90a58d278ee8743d2764e77d1acc5 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Tue, 4 Feb 2025 01:04:49 -0300 Subject: [PATCH 4/7] fix proto generation excluding module.proto --- ignite/pkg/cosmosgen/generate_go.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/ignite/pkg/cosmosgen/generate_go.go b/ignite/pkg/cosmosgen/generate_go.go index 5647730fb9..4cbd781754 100644 --- a/ignite/pkg/cosmosgen/generate_go.go +++ b/ignite/pkg/cosmosgen/generate_go.go @@ -20,10 +20,6 @@ func (g *generator) protoPath() string { } func (g *generator) generateGoGo(ctx context.Context) error { - return g.generate(ctx, g.gogoTemplate(), g.goModPath) -} - -func (g *generator) generate(ctx context.Context, template, fromPath string, excluded ...string) error { // create a temporary dir to locate generated code under which later only some of them will be moved to the // app's source code. this also prevents having leftover files in the app's source code or its parent dir - when // command executed directly there - in case of an interrupt. @@ -38,14 +34,14 @@ func (g *generator) generate(ctx context.Context, template, fromPath string, exc ctx, g.protoPath(), tmp, - template, - cosmosbuf.ExcludeFiles(excluded...), + g.gogoTemplate(), + cosmosbuf.ExcludeFiles("*/module.proto"), ); err != nil { return err } // move generated code for the app under the relative locations in its source code. - path := filepath.Join(tmp, fromPath) + path := filepath.Join(tmp, g.goModPath) if _, err := os.Stat(path); err == nil { err = copy.Copy(path, g.appPath) if err != nil { From 59595eb744a3812903c47a0d84756a3b77d7e455 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Tue, 4 Feb 2025 12:03:55 -0300 Subject: [PATCH 5/7] fix wrong imports for go proto files --- ignite/pkg/cosmosgen/generate_go.go | 2 -- ignite/templates/app/files/{{protoDir}}/buf.gen.gogo.yaml | 3 ++- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/ignite/pkg/cosmosgen/generate_go.go b/ignite/pkg/cosmosgen/generate_go.go index 4cbd781754..dafc7cc462 100644 --- a/ignite/pkg/cosmosgen/generate_go.go +++ b/ignite/pkg/cosmosgen/generate_go.go @@ -7,7 +7,6 @@ import ( "github.com/otiai10/copy" - "github.com/ignite/cli/v29/ignite/pkg/cosmosbuf" "github.com/ignite/cli/v29/ignite/pkg/errors" ) @@ -35,7 +34,6 @@ func (g *generator) generateGoGo(ctx context.Context) error { g.protoPath(), tmp, g.gogoTemplate(), - cosmosbuf.ExcludeFiles("*/module.proto"), ); err != nil { return err } diff --git a/ignite/templates/app/files/{{protoDir}}/buf.gen.gogo.yaml b/ignite/templates/app/files/{{protoDir}}/buf.gen.gogo.yaml index bad929cc94..9b8b406b10 100644 --- a/ignite/templates/app/files/{{protoDir}}/buf.gen.gogo.yaml +++ b/ignite/templates/app/files/{{protoDir}}/buf.gen.gogo.yaml @@ -9,8 +9,9 @@ plugins: out: . opt: - plugins=grpc - - Mgoogle/protobuf/any.proto=github.com/cosmos/cosmos-sdk/codec/types + - Mgoogle/protobuf/any.proto=github.com/cosmos/gogoproto/types/any - Mcosmos/orm/v1/orm.proto=cosmossdk.io/orm + - Mcosmos/app/v1alpha1/module.proto=cosmossdk.io/api/cosmos/app/v1alpha1 - local: protoc-gen-grpc-gateway out: . opt: From 5ae7aa4e145b61fcd6b555d1cd5ceaa8ccb51f5f Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Wed, 5 Feb 2025 20:42:20 -0300 Subject: [PATCH 6/7] add migrate buf command to the cli doctor command --- ignite/cmd/chain.go | 27 +------------ ignite/cmd/doctor.go | 23 ++++++++++- ignite/config/chain/parse.go | 17 +++++++++ ignite/services/chain/proto.go | 17 +++------ ignite/services/doctor/doctor.go | 65 ++++++++++++++++++++++++++------ 5 files changed, 99 insertions(+), 50 deletions(-) diff --git a/ignite/cmd/chain.go b/ignite/cmd/chain.go index 2d1dd196f2..02dc27992d 100644 --- a/ignite/cmd/chain.go +++ b/ignite/cmd/chain.go @@ -14,7 +14,6 @@ import ( "github.com/ignite/cli/v29/ignite/pkg/cliui" "github.com/ignite/cli/v29/ignite/pkg/cliui/colors" "github.com/ignite/cli/v29/ignite/pkg/cliui/icons" - "github.com/ignite/cli/v29/ignite/pkg/cosmosbuf" "github.com/ignite/cli/v29/ignite/pkg/cosmosgen" "github.com/ignite/cli/v29/ignite/pkg/errors" "github.com/ignite/cli/v29/ignite/pkg/goanalysis" @@ -29,17 +28,13 @@ const ( msgMigrationPrefix = "Your blockchain config version is v%d and the latest is v%d." msgMigrationPrompt = "Would you like to upgrade your config file to v%d" msgMigrationBuf = "Now ignite supports the `buf.build` (https://buf.build) registry to manage the protobuf dependencies. The embed protoc binary was deprecated and, your blockchain is still using it. Would you like to upgrade and add the `buf.build` config files to `proto/` folder" - msgMigrationBufV2 = "Now ignite supports the new `buf.build` (https://buf.build) v2 configuration. Would you like to upgrade your buf files to v2 using the `buf config migrate` command" msgMigrationBufProtoDir = "Ignite proto directory path from the chain config doesn't match the proto directory path from the chain `buf.work.yaml`. Do you want to add the proto path `%[1]v` to the directories list from the buf work file" msgMigrationBufProtoDirs = "Chain `buf.work.yaml` file contains directories that don't exist anymore (%[1]v). Do you want to delete them" msgMigrationAddTools = "Some required imports are missing in %s file: %s. Would you like to add them" msgMigrationRemoveTools = "File %s contains deprecated imports: %s. Would you like to remove them" ) -var ( - ErrProtocUnsupported = errors.New("code generation using protoc is only supported by Ignite CLI v0.26.1 or older") - ErrBufConfigVersionUnsupported = errors.New("buf config version must be v2") -) +var ErrProtocUnsupported = errors.New("code generation using protoc is only supported by Ignite CLI v0.26.1 or older") // NewChain returns a command that groups sub commands related to compiling, serving // blockchains and so on. @@ -197,29 +192,11 @@ func toolsMigrationPreRunHandler(cmd *cobra.Command, session *cliui.Session, app func bufMigrationPreRunHandler(cmd *cobra.Command, session *cliui.Session, appPath, protoDir string) error { // check if the buf files exist. - hasFiles, needMigration, err := chain.CheckBufFiles(appPath, protoDir) + hasFiles, err := chain.CheckBufFiles(appPath, protoDir) if err != nil { return err } - if needMigration { - if !getYes(cmd) { - if err := session.AskConfirm(msgMigrationBufV2); err != nil { - return ErrBufConfigVersionUnsupported - } - } - - cacheStorage, err := newCache(cmd) - b, err := cosmosbuf.New(cacheStorage, appPath) - if err != nil { - return err - } - - if err := b.Migrate(cmd.Context(), protoDir); err != nil { - return err - } - } - if !hasFiles { if !getYes(cmd) { if err := session.AskConfirm(msgMigrationBuf); err != nil { diff --git a/ignite/cmd/doctor.go b/ignite/cmd/doctor.go index 8f052e0e9d..278c1092c1 100644 --- a/ignite/cmd/doctor.go +++ b/ignite/cmd/doctor.go @@ -3,22 +3,38 @@ package ignitecmd import ( "github.com/spf13/cobra" + chainconfig "github.com/ignite/cli/v29/ignite/config/chain" "github.com/ignite/cli/v29/ignite/pkg/cliui" "github.com/ignite/cli/v29/ignite/services/doctor" ) func NewDoctor() *cobra.Command { - return &cobra.Command{ + c := &cobra.Command{ Use: "doctor", Short: "Fix chain configuration", Hidden: true, RunE: func(cmd *cobra.Command, _ []string) error { session := cliui.New() defer session.End() + appPath := flagGetPath(cmd) doc := doctor.New(doctor.CollectEvents(session.EventBus())) - if err := doc.MigrateConfig(cmd.Context()); err != nil { + cacheStorage, err := newCache(cmd) + if err != nil { + return err + } + + configPath, err := chainconfig.LocateDefault(appPath) + if err != nil { + return err + } + + if err := doc.MigrateChainConfig(configPath); err != nil { + return err + } + + if err := doc.MigrateBufConfig(cmd.Context(), cacheStorage, appPath, configPath); err != nil { return err } @@ -29,4 +45,7 @@ func NewDoctor() *cobra.Command { return doc.FixDependencyTools(cmd.Context()) }, } + + flagSetPath(c) + return c } diff --git a/ignite/config/chain/parse.go b/ignite/config/chain/parse.go index fbba0e3c3b..652048589d 100644 --- a/ignite/config/chain/parse.go +++ b/ignite/config/chain/parse.go @@ -9,6 +9,7 @@ import ( "github.com/cosmos/cosmos-sdk/types/bech32" + "github.com/ignite/cli/v29/ignite/config/chain/defaults" "github.com/ignite/cli/v29/ignite/config/chain/version" "github.com/ignite/cli/v29/ignite/pkg/errors" ) @@ -103,6 +104,22 @@ func ReadConfigVersion(configFile io.Reader) (version.Version, error) { return c.Version, err } +// ReadProtoPath reads the proto path. +func ReadProtoPath(configFile io.Reader) (string, error) { + c := struct { + Build struct { + Proto struct { + Path string `yaml:"path"` + } `yaml:"proto"` + } `yaml:"build"` + }{} + + c.Build.Proto.Path = defaults.ProtoDir + err := yaml.NewDecoder(configFile).Decode(&c) + + return c.Build.Proto.Path, err +} + func decodeConfig(r io.Reader, version version.Version) (version.Converter, error) { c, ok := Versions[version] if !ok { diff --git a/ignite/services/chain/proto.go b/ignite/services/chain/proto.go index 5dba7860d6..a157a1763e 100644 --- a/ignite/services/chain/proto.go +++ b/ignite/services/chain/proto.go @@ -9,9 +9,6 @@ import ( "github.com/ignite/cli/v29/ignite/templates/app" ) -// oldBufWorkFile represents the v1 buf work file, may this check should be remove in v30 -const oldBufWorkFile = "buf.work.yaml" - // CheckBufProtoDir check if the proto path exist into the directory list in the buf.work.yaml file. func CheckBufProtoDir(appPath, protoDir string) (bool, []string, error) { bufCfg, err := cosmosbuf.ParseBufConfig(appPath) @@ -47,15 +44,11 @@ func RemoveBufProtoDirs(appPath string, protoDirs ...string) error { return workFile.RemoveProtoDirs(protoDirs...) } -// CheckBufFiles check if the buf files exist, and if needs a migration to v2. -func CheckBufFiles(appPath, protoDir string) (bool, bool, error) { +// CheckBufFiles check if the buf files exist. +func CheckBufFiles(appPath, protoDir string) (bool, error) { files, err := app.BufFiles() if err != nil { - return false, false, nil - } - // if the buf.work.yaml exist, we only need the migration - if xos.FileExists(filepath.Join(appPath, oldBufWorkFile)) { - return true, true, nil + return false, nil } for _, bufFile := range files { bufFile, ok := app.CutTemplatePrefix(bufFile) @@ -63,10 +56,10 @@ func CheckBufFiles(appPath, protoDir string) (bool, bool, error) { bufFile = filepath.Join(protoDir, bufFile) } if !xos.FileExists(filepath.Join(appPath, bufFile)) { - return false, false, nil + return false, nil } } - return true, false, nil + return true, nil } // BoxBufFiles box all buf files. diff --git a/ignite/services/doctor/doctor.go b/ignite/services/doctor/doctor.go index c18468148c..742853fd6b 100644 --- a/ignite/services/doctor/doctor.go +++ b/ignite/services/doctor/doctor.go @@ -8,8 +8,10 @@ import ( "path" chainconfig "github.com/ignite/cli/v29/ignite/config/chain" + "github.com/ignite/cli/v29/ignite/pkg/cache" "github.com/ignite/cli/v29/ignite/pkg/cliui/colors" "github.com/ignite/cli/v29/ignite/pkg/cliui/icons" + "github.com/ignite/cli/v29/ignite/pkg/cosmosbuf" "github.com/ignite/cli/v29/ignite/pkg/cosmosgen" "github.com/ignite/cli/v29/ignite/pkg/errors" "github.com/ignite/cli/v29/ignite/pkg/events" @@ -49,41 +51,82 @@ func CollectEvents(ev events.Bus) Option { } } -// MigrateConfig migrates the chain config if required. -func (d *Doctor) MigrateConfig(_ context.Context) error { +// MigrateBufConfig migrates the buf chain config if required. +func (d *Doctor) MigrateBufConfig(ctx context.Context, cacheStorage cache.Storage, appPath, configPath string) error { errf := func(err error) error { - return errors.Errorf("doctor migrate config: %w", err) + return errors.Errorf("doctor migrate buf config: %w", err) } - d.ev.Send("Checking chain config file:") + d.ev.Send("Checking buf config file version") + + // Check if the appPath contains the buf.work.yaml file in the root folder. + bufWorkFile := path.Join(appPath, "buf.work.yaml") + if _, err := os.Stat(bufWorkFile); os.IsNotExist(err) { + return nil + } else if err != nil { + return errf(fmt.Errorf("unable to check if buf.work.yaml exists: %w", err)) + } + + d.ev.Send("Migrating buf config file to v2") + + configFile, err := os.Open(configPath) + if err != nil { + return err + } + defer configFile.Close() - configPath, err := chainconfig.LocateDefault(".") + protoPath, err := chainconfig.ReadProtoPath(configFile) if err != nil { return errf(err) } - f, err := os.Open(configPath) + b, err := cosmosbuf.New(cacheStorage, appPath) if err != nil { return errf(err) } - defer f.Close() - version, err := chainconfig.ReadConfigVersion(f) + if err := b.Migrate(ctx, protoPath); err != nil { + return errf(err) + } + + d.ev.Send( + fmt.Sprintf("buf config files migrated"), + events.Icon(icons.OK), + events.Indent(1), + events.ProgressFinish(), + ) + + return nil +} + +// MigrateChainConfig migrates the chain config if required. +func (d *Doctor) MigrateChainConfig(configPath string) error { + errf := func(err error) error { + return errors.Errorf("doctor migrate config: %w", err) + } + + d.ev.Send("Checking chain config file:") + configFile, err := os.Open(configPath) + if err != nil { + return err + } + defer configFile.Close() + + version, err := chainconfig.ReadConfigVersion(configFile) if err != nil { return errf(err) } status := "OK" - if version != chainconfig.LatestVersion { - _, err := f.Seek(0, 0) + _, err := configFile.Seek(0, 0) if err != nil { return errf(errors.Errorf("failed to reset the file: %w", err)) } // migrate config file // Convert the current config to the latest version and update the YAML file var buf bytes.Buffer - if err := chainconfig.MigrateLatest(f, &buf); err != nil { + if err := chainconfig.MigrateLatest(configFile, &buf); err != nil { return errf(err) } From 74cb32872428209c85e2bb81989532ceaef44afe Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Thu, 6 Feb 2025 12:32:12 -0300 Subject: [PATCH 7/7] fix vars for integrations tests --- ignite/services/doctor/doctor.go | 4 ++-- integration/doctor/doctor_test.go | 3 +++ integration/doctor/testdata/config-missing.txt | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/ignite/services/doctor/doctor.go b/ignite/services/doctor/doctor.go index 742853fd6b..a91c4fc764 100644 --- a/ignite/services/doctor/doctor.go +++ b/ignite/services/doctor/doctor.go @@ -64,7 +64,7 @@ func (d *Doctor) MigrateBufConfig(ctx context.Context, cacheStorage cache.Storag if _, err := os.Stat(bufWorkFile); os.IsNotExist(err) { return nil } else if err != nil { - return errf(fmt.Errorf("unable to check if buf.work.yaml exists: %w", err)) + return errf(errors.Errorf("unable to check if buf.work.yaml exists: %w", err)) } d.ev.Send("Migrating buf config file to v2") @@ -90,7 +90,7 @@ func (d *Doctor) MigrateBufConfig(ctx context.Context, cacheStorage cache.Storag } d.ev.Send( - fmt.Sprintf("buf config files migrated"), + "buf config files migrated", events.Icon(icons.OK), events.Indent(1), events.ProgressFinish(), diff --git a/integration/doctor/doctor_test.go b/integration/doctor/doctor_test.go index d206b3837d..0f4d5a4223 100644 --- a/integration/doctor/doctor_test.go +++ b/integration/doctor/doctor_test.go @@ -12,6 +12,8 @@ import ( envtest "github.com/ignite/cli/v29/integration" ) +const envDoNotTrack = "DO_NOT_TRACK" + func TestDoctor(t *testing.T) { // Ensure ignite binary is compiled envtest.New(t) @@ -19,6 +21,7 @@ func TestDoctor(t *testing.T) { params := testscript.Params{ Setup: func(env *testscript.Env) error { env.Vars = append(env.Vars, + envDoNotTrack+"=true", // Pass ignite binary path "IGNITE="+envtest.IgniteApp, // Pass ignite config dir diff --git a/integration/doctor/testdata/config-missing.txt b/integration/doctor/testdata/config-missing.txt index 4b6572ff00..31d6806801 100644 --- a/integration/doctor/testdata/config-missing.txt +++ b/integration/doctor/testdata/config-missing.txt @@ -1,4 +1,4 @@ # Test fix config # config is missing ! exec $IGNITE doctor -stdout 'could not locate a config.yml' +stdout 'Could not locate a config.yml in your chain'