From e1c04bb4decf8901e13cfb3bb96a459b1b7a1ea1 Mon Sep 17 00:00:00 2001 From: Geoff Lee Date: Wed, 17 Jul 2024 18:00:56 +0900 Subject: [PATCH] fix: github action to lint (#102) * fix linter action * remove slinky from workflow --- .github/workflows/lint.yml | 38 ++++++++++++---- .golangci.yml | 58 ++++++++++++++++-------- Makefile | 13 ++++++ contrib/launchtools/config.go | 2 +- contrib/launchtools/steps/bridgeinfo.go | 2 +- contrib/launchtools/steps/ibc.go | 4 +- contrib/launchtools/steps/runapp.go | 2 +- contrib/launchtools/steps/stopapp.go | 13 ++++-- contrib/launchtools/types.go | 4 +- x/opchild/keeper/executor_change_test.go | 4 +- x/opchild/keeper/genesis.go | 5 +- x/opchild/keeper/keeper.go | 8 ++-- x/opchild/module.go | 2 +- x/opchild/types/tx.go | 4 +- x/ophost/keeper/keeper.go | 4 +- x/ophost/module.go | 2 +- 16 files changed, 113 insertions(+), 52 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index dfbe4395..b7f66a79 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -24,15 +24,18 @@ jobs: golangci: env: # for private repo access - GOPRIVATE: github.com/initia-labs/* + GOPRIVATE: github.com/initia-labs GITHUB_ACCESS_TOKEN: ${{ secrets.GH_READ_TOKEN }} + GOLANGCI_LINT_VERSION: v1.59.1 name: golangci-lint runs-on: ubuntu-latest steps: - - uses: actions/setup-go@v2.1.4 + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 with: go-version: 1.22 - - uses: technote-space/get-diff-action@v5 + check-latest: true + - uses: technote-space/get-diff-action@v6.1.2 id: git_diff with: PATTERNS: | @@ -41,26 +44,43 @@ jobs: go.sum # for private repo access - run: git config --global url.https://${GITHUB_ACCESS_TOKEN}:x-oauth-basic@github.com/.insteadOf https://github.com/ - - name: run go linters + # install golangci-lint + - run: go install github.com/golangci/golangci-lint/cmd/golangci-lint@${GOLANGCI_LINT_VERSION} + - name: run go linters (long) + if: env.GIT_DIFF + id: lint_long run: | - make tools make lint - if: env.GIT_DIFF + - uses: technote-space/get-diff-action@v6.1.2 + if: steps.lint_long.outcome == 'skipped' + with: + PATTERNS: | + **/**.go + go.mod + go.sum + - name: run go linters (short) + if: steps.lint_long.outcome == 'skipped' && env.GIT_DIFF + run: | + make lint + env: + GIT_DIFF: ${{ env.GIT_DIFF }} + LINT_DIFF: 1 # Use --check or --exit-code when available (Go 1.19?) # https://github.com/golang/go/issues/27005 tidy: env: # for private repo access - GOPRIVATE: github.com/initia-labs/* + GOPRIVATE: github.com/initia-labs,github.com/skip-mev/slinky GITHUB_ACCESS_TOKEN: ${{ secrets.GH_READ_TOKEN }} runs-on: ubuntu-latest name: tidy steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Setup go - uses: actions/setup-go@v3 + uses: actions/setup-go@v5 with: go-version: 1.22 + check-latest: true # for private repo access - run: git config --global url.https://${GITHUB_ACCESS_TOKEN}:x-oauth-basic@github.com/.insteadOf https://github.com/ - run: | diff --git a/.golangci.yml b/.golangci.yml index b261ffbf..00fca633 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,26 +1,44 @@ linters: - disable-all: true enable: + - asciicheck + - bodyclose + - dogsled + - dupl - errcheck - - golint - - ineffassign - - unconvert - - misspell - - govet - # - unused - # - deadcode + - exportloopref - goconst + - gofmt + - goimports - gosec - # - staticcheck -linters-settings: - gocyclo: - min-complexity: 11 - errcheck: - ignore: fmt:.*,io/ioutil:^Read.*,github.com/spf13/cobra:MarkFlagRequired,github.com/spf13/viper:BindPFlag - golint: - min-confidence: 1.1 + - gosimple + - govet + - ineffassign + - misspell + # - nakedret + - nolintlint + # - prealloc + - staticcheck + # - structcheck // to be fixed by golangci-lint + - stylecheck + - typecheck + - unconvert + - unused + issues: - exclude: - - composite -run: - tests: false + exclude-rules: + - linters: + - stylecheck + text: "ST1003:" + max-same-issues: 50 + +linters-settings: + dogsled: + max-blank-identifiers: 3 + #golint: + # min-confidence: 0 + goconst: + ignore-tests: true + #maligned: + # suggest-new: true + misspell: + locale: US diff --git a/Makefile b/Makefile index 268092f8..a4999962 100644 --- a/Makefile +++ b/Makefile @@ -32,6 +32,19 @@ proto-check-breaking: .PHONY: proto-all proto-gen proto-pulsar-gen proto-format proto-lint proto-check-breaking +############################################################################### +### Linting ### +############################################################################### + +lint: + golangci-lint run --out-format=tab --timeout=15m --tests=false + +lint-fix: + golangci-lint run --fix --out-format=tab --timeout=15m --tests=false + +.PHONY: lint lint-fix + + ############################################################################### ### Tests ############################################################################### diff --git a/contrib/launchtools/config.go b/contrib/launchtools/config.go index a3d9a2dd..761eaf79 100644 --- a/contrib/launchtools/config.go +++ b/contrib/launchtools/config.go @@ -393,7 +393,7 @@ func (systemKeys *SystemKeys) Finalize(buf *bufio.Reader) error { var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") func randString(n int) string { - rand := rand.New(rand.NewSource(time.Now().UnixNano())) + rand := rand.New(rand.NewSource(time.Now().UnixNano())) //nolint b := make([]rune, n) for i := range b { diff --git a/contrib/launchtools/steps/bridgeinfo.go b/contrib/launchtools/steps/bridgeinfo.go index c0337cf1..829f2a62 100644 --- a/contrib/launchtools/steps/bridgeinfo.go +++ b/contrib/launchtools/steps/bridgeinfo.go @@ -4,7 +4,7 @@ import ( "github.com/pkg/errors" sdk "github.com/cosmos/cosmos-sdk/types" - clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" + clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types" //nolint:staticcheck tmclient "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint" "github.com/initia-labs/OPinit/contrib/launchtools" diff --git a/contrib/launchtools/steps/ibc.go b/contrib/launchtools/steps/ibc.go index d6d5972a..485e5f1b 100644 --- a/contrib/launchtools/steps/ibc.go +++ b/contrib/launchtools/steps/ibc.go @@ -132,7 +132,7 @@ func initializeChains(config *launchtools.Config, basePath string) func(*Relayer pathName := fmt.Sprintf("chain%d", i) fileName := fmt.Sprintf("%s/%s.json", basePath, pathName) - if err := os.WriteFile(fileName, bz, 0644); err != nil { + if err := os.WriteFile(fileName, bz, 0600); err != nil { panic(errors.New("failed to write chain config")) } } @@ -180,7 +180,7 @@ func initializePaths(config *launchtools.Config, basePath string) func(*Relayer) panic(errors.New("failed to create path config")) } - if err := os.WriteFile(fmt.Sprintf("%s/paths.json", basePath), pathConfigJSON, 0644); err != nil { + if err := os.WriteFile(fmt.Sprintf("%s/paths.json", basePath), pathConfigJSON, 0600); err != nil { panic(errors.New("failed to write path config")) } diff --git a/contrib/launchtools/steps/runapp.go b/contrib/launchtools/steps/runapp.go index 2651eefd..48a4e504 100644 --- a/contrib/launchtools/steps/runapp.go +++ b/contrib/launchtools/steps/runapp.go @@ -23,7 +23,7 @@ func RunApp(_ *launchtools.Config) launchtools.LauncherStepFunc { ctx.ServerContext().Config.Consensus.CreateEmptyBlocks = true ctx.ServerContext().Config.Consensus.CreateEmptyBlocksInterval = CreateEmptyBlocksInterval - // create a channel to synchronise on app creation + // create a channel to synchronize on app creation var syncDone = make(chan interface{}) // create cobra command context diff --git a/contrib/launchtools/steps/stopapp.go b/contrib/launchtools/steps/stopapp.go index c5cdff50..733fd301 100644 --- a/contrib/launchtools/steps/stopapp.go +++ b/contrib/launchtools/steps/stopapp.go @@ -19,12 +19,19 @@ func StopApp(_ *launchertypes.Config) launchertypes.LauncherStepFunc { log.Info("cleanup") log.Info("waiting for app to stop") - syscall.Kill(syscall.Getpid(), syscall.SIGINT) + err := syscall.Kill(syscall.Getpid(), syscall.SIGINT) + if err != nil { + log.Error("failed to raise a kill signal", "error", err) + } // wait for the app to stop - ctx.GetErrorGroup().Wait() - log.Info("cleanup finished") + err = ctx.GetErrorGroup().Wait() + if err != nil { + log.Error("cleanup failed", "error", err) + return err + } + log.Info("cleanup finished") return nil } } diff --git a/contrib/launchtools/types.go b/contrib/launchtools/types.go index 0f8f8473..39bdf28a 100644 --- a/contrib/launchtools/types.go +++ b/contrib/launchtools/types.go @@ -266,7 +266,7 @@ func (l *LauncherContext) FinalizeOutput(config *Config) (string, error) { return "", errors.Wrap(err, "failed to marshal artifacts") } - if err := os.WriteFile(path.Join(l.artifactsDir, "artifacts.json"), bz, os.ModePerm); err != nil { + if err := os.WriteFile(path.Join(l.artifactsDir, "artifacts.json"), bz, 0600); err != nil { return "", errors.Wrap(err, "failed to write artifacts to file") } @@ -276,7 +276,7 @@ func (l *LauncherContext) FinalizeOutput(config *Config) (string, error) { return "", errors.Wrap(err, "failed to marshal config") } - if err := os.WriteFile(path.Join(l.artifactsDir, "config.json"), configBz, os.ModePerm); err != nil { + if err := os.WriteFile(path.Join(l.artifactsDir, "config.json"), configBz, 0600); err != nil { return "", errors.Wrap(err, "failed to write config to file") } diff --git a/x/opchild/keeper/executor_change_test.go b/x/opchild/keeper/executor_change_test.go index 89132b07..563b7ead 100644 --- a/x/opchild/keeper/executor_change_test.go +++ b/x/opchild/keeper/executor_change_test.go @@ -16,8 +16,8 @@ func Test_RegisterExecutorChangePlan(t *testing.T) { _, input := createTestInput(t, false) // Arguments - l1ProposalID := uint64(rand.Uint64()) - height := uint64(rand.Uint64()) + l1ProposalID := rand.Uint64() + height := rand.Uint64() nextValAddr := valAddrsStr[0] nextExecutorAddr := []string{addrsStr[0], addrsStr[1]} consensusPubKey := "l7aqGv+Zjbm0rallfqfqz+3iN31iOmgJCafWV5pGs6o=" diff --git a/x/opchild/keeper/genesis.go b/x/opchild/keeper/genesis.go index b3fbce90..31e55a4f 100644 --- a/x/opchild/keeper/genesis.go +++ b/x/opchild/keeper/genesis.go @@ -153,7 +153,7 @@ func (k Keeper) ExportGenesis(ctx context.Context) *types.GenesisState { } pendingDeposits := []types.PendingDeposits{} - k.PendingDeposits.Walk(ctx, nil, func(key []byte, value types.CoinsWrapper) (stop bool, err error) { + err = k.PendingDeposits.Walk(ctx, nil, func(key []byte, value types.CoinsWrapper) (stop bool, err error) { addr, err := k.addressCodec.BytesToString(key) if err != nil { return false, err @@ -165,6 +165,9 @@ func (k Keeper) ExportGenesis(ctx context.Context) *types.GenesisState { }) return false, nil }) + if err != nil { + panic(err) + } return &types.GenesisState{ Params: params, diff --git a/x/opchild/keeper/keeper.go b/x/opchild/keeper/keeper.go index e4b7de60..5958df1b 100644 --- a/x/opchild/keeper/keeper.go +++ b/x/opchild/keeper/keeper.go @@ -128,8 +128,8 @@ func NewKeeper( } // GetAuthority returns the x/move module's authority. -func (ak Keeper) GetAuthority() string { - return ak.authority +func (k Keeper) GetAuthority() string { + return k.authority } // Logger returns a module-specific logger. @@ -139,8 +139,8 @@ func (k Keeper) Logger(ctx context.Context) log.Logger { } // Router returns the gov keeper's router -func (keeper Keeper) Router() *baseapp.MsgServiceRouter { - return keeper.router +func (k Keeper) Router() *baseapp.MsgServiceRouter { + return k.router } // setDenomMetadata sets an OPinit token's denomination metadata diff --git a/x/opchild/module.go b/x/opchild/module.go index 790f4574..50d83216 100644 --- a/x/opchild/module.go +++ b/x/opchild/module.go @@ -42,7 +42,7 @@ type AppModuleBasic struct { cdc codec.Codec } -func (b AppModuleBasic) RegisterLegacyAminoCodec(amino *codec.LegacyAmino) { //nolint:staticcheck +func (b AppModuleBasic) RegisterLegacyAminoCodec(amino *codec.LegacyAmino) { types.RegisterLegacyAminoCodec(amino) } diff --git a/x/opchild/types/tx.go b/x/opchild/types/tx.go index cbfd22dd..671f3c83 100644 --- a/x/opchild/types/tx.go +++ b/x/opchild/types/tx.go @@ -68,8 +68,8 @@ func (msg MsgExecuteMessages) Validate(ac address.Codec) error { } // UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces -func (m MsgExecuteMessages) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error { - return sdktx.UnpackInterfaces(unpacker, m.Messages) +func (msg MsgExecuteMessages) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error { + return sdktx.UnpackInterfaces(unpacker, msg.Messages) } /* MsgAddValidator */ diff --git a/x/ophost/keeper/keeper.go b/x/ophost/keeper/keeper.go index 8223053f..d89e6131 100644 --- a/x/ophost/keeper/keeper.go +++ b/x/ophost/keeper/keeper.go @@ -86,8 +86,8 @@ func NewKeeper( } // GetAuthority returns the x/move module's authority. -func (ak Keeper) GetAuthority() string { - return ak.authority +func (k Keeper) GetAuthority() string { + return k.authority } // Logger returns a module-specific logger. diff --git a/x/ophost/module.go b/x/ophost/module.go index b5dc6194..1c4896e5 100644 --- a/x/ophost/module.go +++ b/x/ophost/module.go @@ -37,7 +37,7 @@ type AppModuleBasic struct { cdc codec.Codec } -func (b AppModuleBasic) RegisterLegacyAminoCodec(amino *codec.LegacyAmino) { //nolint:staticcheck +func (b AppModuleBasic) RegisterLegacyAminoCodec(amino *codec.LegacyAmino) { types.RegisterLegacyAminoCodec(amino) }