Skip to content
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

[Linting] Enforce 120 max lines, and import order with custom linting #319

Closed
wants to merge 65 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
a9c5727
feat: enforce 150 max lines and import order via in golangci-lint yml
Jan 12, 2024
9b4e1c5
feat: add the Linter workflow file
Jan 12, 2024
c4b82d2
feat: exclude errors.go file for line length check
Jan 12, 2024
26f3598
chore: fix linter-settings duplication
Jan 12, 2024
cd50b47
chore: fix errors.go exclude linter
Jan 12, 2024
c209ac5
chore: increase linter workflow timeout
Jan 12, 2024
fb7d589
chore: remove linter workflow and move into test workflow
Jan 12, 2024
5830bf3
feat: only apply linting checks to new files
Jan 12, 2024
38dca85
chore: fix golangci_lint errors (part 1)
Jan 12, 2024
5ec364d
chore: run gci on all files and add script and makefile target
Jan 12, 2024
8fd7aaf
chore: remove cmd print in script
Jan 12, 2024
2ba6dfc
chore: remove custom linting section from gci
Jan 12, 2024
24c1195
chore: address more linter errors
Jan 13, 2024
4955f75
feat: add a step to the test workflow where we fix imports
Jan 13, 2024
27217d4
chore: add script, makefile target and gofumpt the repo
Jan 13, 2024
0fa5f81
feat: add formatting to the test workflow
Jan 13, 2024
b3ba99b
feat: add run customisations to config file and remove test files from
Jan 13, 2024
f070da6
chore: catch all linting errors from `make go_lint`
Jan 15, 2024
39734f0
feat: add new filter to gci to ignore files with import block scaffol…
Jan 15, 2024
019799d
chore: fix filter for scaffold imports
Jan 15, 2024
2eb47ca
feat: add Linter workflow separating it from testing, with appropriate
Jan 16, 2024
03ff717
feat: replace scaffold lines with correct gci import ordering
Jan 16, 2024
1866316
chore: enable custom ordering when running `make gci`
Jan 16, 2024
95cb78f
chore final lint changes
Jan 16, 2024
b0cebf5
feat: enable new flag in golangci-lint config
Jan 16, 2024
4e71478
chore: fix linter error
Jan 16, 2024
a6b790b
chore: address comments
Jan 16, 2024
34f9e8a
chore: remove mocked addressing comment
Jan 16, 2024
0574b25
chore: fix golangci-lint config syntax
Jan 16, 2024
38e4df2
Merge branch 'main' into feat/linting-updates
Jan 16, 2024
11c1885
chore: add --version checks to fix CI "hopefully"
Jan 16, 2024
5f5615b
feat: merge linting workflow back into tests workflow
Jan 16, 2024
eed0aed
feat: update workflow to not double lint files
Jan 16, 2024
3c74ca0
chore: Name -> name in workflow
Jan 16, 2024
520e07b
chore: address comments
Jan 16, 2024
1fb1820
chore: add nolint reason on same line
Jan 16, 2024
6a23c05
chore: address comments
Jan 16, 2024
f4f40a2
feat: only filter go files
Jan 16, 2024
321c64e
chore: remove the only new issues
Jan 16, 2024
879b388
feat: add custom golangci-lint script
Jan 17, 2024
a4838e6
feat: add new targets and update their names re: linting
Jan 17, 2024
577c3d7
feat: add golint tool to run golangci-lint selectively
Jan 17, 2024
3021487
chore: remove un-needed sorting
Jan 17, 2024
c77c217
chore: add default args to config and move linting to after tests
Jan 17, 2024
098a1d2
chore: remove un-used permissions
Jan 17, 2024
041f0eb
feat: fail when linter errors are found
Jan 17, 2024
522361d
Merge branch 'main' into feat/linting-updates
Jan 17, 2024
040cec9
feat: exclude certain directories and print error on fail
Jan 17, 2024
cb09498
chore: fix linter errors, add nolints where needed
Jan 17, 2024
51cb379
chore: go mod tidy & ignite chain build
Jan 17, 2024
1953b7e
feat: replace custom linter with golangci-lint action
Jan 17, 2024
0ae08d2
feat: remove custom linter script
Jan 17, 2024
7f32c69
feat: add extra linters
Jan 17, 2024
ecf6ef8
feat: replace golangci-lint install with binary installer, and make
Jan 17, 2024
160e857
chore: reorg steps in test job back to how they were before
Jan 17, 2024
087e6f7
feat: install golangci-lint with go install and correct go version
Jan 18, 2024
4bec743
feat: add mise.local.toml exclusion
Jan 18, 2024
00da2e6
feat: add godoc comments to the formatting tool scripts
Jan 18, 2024
a949347
bug: fix check golangci-lint make target
Jan 18, 2024
f9f0923
chore: update comments
Jan 18, 2024
91d2907
Merge branch 'main' into feat/linting-updates
Jan 18, 2024
943669e
bug: fix golangci-lint Cannot open: File exists error by skipping cache
Jan 18, 2024
ed4ebf1
feat: fix make go_lint
Jan 20, 2024
67a8a6b
Merge branch 'main' into feat/linting-updates
Jan 20, 2024
0ddd1c4
chore: final linting
Jan 20, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.ref_name }}
cancel-in-progress: true

permissions:
contents: read
pull-requests: read
checks: write

env:
GKE_CLUSTER: protocol-us-central1
GKE_ZONE: us-central1
Expand Down Expand Up @@ -44,8 +49,19 @@ jobs:
- name: Generate mocks
run: make go_mockgen

- name: Run golangci-lint
run: make go_lint
- name: Fix imports
run: make go_gci

- name: Format the repo
run: make go_gofumpt

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
only-new-issues: true
args: --timeout 15m --issues-exit-code=1
skip-cache: true

- name: Test
run: make go_test
74 changes: 61 additions & 13 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
linters-settings:
govet:
check-shadowing: true
run:
timeout: 15m
config: ./.golangci.yml
new: true
color: always
build-tags:
- integration
- test
- tools

# TODO_TECHDEBT: Enable each linter listed, 1 by 1, fixing issues as they appear.
# Don't forget to delete the `disable-all: true` line as well.
linters:
disable-all: true
enable:
# - govet
# - revive
# - errcheck
# - unused
- goimports
# - govet
# - errcheck
# - unused
- revive
- gofumpt
- lll
- gci

linters-settings:
govet:
check-shadowing: true
lll:
line-length: 120
tab-width: 4
gci:
sections:
- standard # std lib
- default # external
- prefix(github.com/pokt-network/poktroll) # local imports
skip-generated: true
custom-order: true
h5law marked this conversation as resolved.
Show resolved Hide resolved
skip-files:
- "*.pb.go"
- "*/vendor/*"

issues:
exclude-use-default: true
Expand All @@ -27,30 +50,55 @@ issues:
# empty import block containing a comment used by ignite CLI.
- path: ^x/.+/genesis\.go$
linters:
- goimports
- gci
# Exclude cosmos-sdk module module.go files as they are generated with unused
# parameters and unchecked errors.
- path: ^x/.+/module\.go$
linters:
- lll
- gci
- revive
- errcheck
# Exclude cosmos-sdk module tx.go files as they are generated with unused
# constants.
- path: ^x/.+/cli/tx\.go$
linters:
- gci
- lll
- unused
# Exclude simulation code as it's generated with lots of unused parameters.
- path: .*/simulation/.*|_simulation\.go$
linters:
- gci
- lll
- revive
# Exclude cosmos-sdk module codec files as they are scaffolded with a unused
# paramerters and a comment used by ignite CLI.
# parameters and a comment used by ignite CLI.
- path: ^x/.+/codec.go$
linters:
- lll
- gci
- revive
- path: _test\.go$
linters:
- errcheck
# Exclude line length check for errors.go files as they are easier to read
# as as single line.
- path: errors\.go$
linters:
- lll
# Exclude app module code as it's generated with lots with long lines and
# improperly formatted imports.
- path: ^app
linters:
- lll
- gci
# Exclude cmd directory code as it's generated with lots with long lines and
# improperly formatted imports.
- path: ^cmd/poc
linters:
- lll
- gci
new: true
# TODO_IMPROVE: see https://golangci-lint.run/usage/configuration/#issues-configuration
#new: true,
#fix: true,
52 changes: 44 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ POCKET_ADDR_PREFIX = pokt

# TODO: Add other dependencies (ignite, docker, k8s, etc) here
.PHONY: install_ci_deps
install_ci_deps: ## Installs `mockgen`
go install "github.com/golang/mock/[email protected]" && mockgen --version
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest && golangci-lint --version
go install golang.org/x/tools/cmd/goimports@latest
install_ci_deps: ## Installs `mockgen` and `golangci-lint` and other linters
go install "github.com/golang/mock/[email protected]" && mockgen --version; \
go install github.com/golangci/golangci-lint/cmd/[email protected] && golangci-lint --version; \
go install github.com/daixiang0/gci@latest && gci --version; \
go install mvdan.cc/gofumpt@latest && gofumpt --version

########################
### Makefile Helpers ###
Expand Down Expand Up @@ -71,6 +72,36 @@ check_godoc:
fi; \
}

.PHONY: check_golanci_lint
# Internal helper target - check if golangci-lint is installed
check_golanci_lint:
{ \
if ( ! ( command -v golangci-lint >/dev/null )); then \
echo "Seems like you don't have golanci-lint installed. Make sure you install it via 'go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest' before continuing"; \
exit 1; \
fi; \
}

.PHONY: check_gci
# Internal helper target - check if gci is installed
check_gci:
{ \
if ( ! ( command -v gci >/dev/null )); then \
echo "Seems like you don't have gci installed. Make sure you install it via 'go install github.com/daixiang0/gci@latest' before continuing"; \
exit 1; \
fi; \
}

.PHONY: check_gofumpt
# Internal helper target - check if gofumpt is installed
check_gofumpt:
{ \
if ( ! ( command -v gofumpt >/dev/null )); then \
echo "Seems like you don't have gofumpt installed. Make sure you install it via 'go install mvdan.cc/gofumpt@latest' before continuing"; \
exit 1; \
fi; \
}

.PHONY: check_npm
# Internal helper target - check if npm is installed
check_npm:
Expand Down Expand Up @@ -149,11 +180,16 @@ localnet_regenesis: ## Regenerate the localnet genesis file
###############

.PHONY: go_lint
go_lint: ## Run all go linters
golangci-lint run --timeout 5m --build-tags test
go_lint: check_golanci_lint ## Run all go linters
golangci-lint run --allow-parallel-runners -c .golangci.yml ./...

.PHONY: go_gci
go_gci: check_gci ## Run gci (import ordering) on all applicable files, this writes changes in place
go run ./tools/scripts/gci

go_imports: check_go_version ## Run goimports on all go files
go run ./tools/scripts/goimports
.PHONY: go_gofumpt
go_gofumpt: check_gofumpt ## Run gofumpt (stricter gofmt) on all applicable files, this writes changes in place
go run ./tools/scripts/gofumpt

#############
### Tests ###
Expand Down
18 changes: 13 additions & 5 deletions app/simulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ func fauxMerkleModeOpt(bapp *baseapp.BaseApp) {
// Running using starport command:
// `starport chain simulate -v --numBlocks 200 --blockSize 50`
// Running as go benchmark test:
// `go test -benchmem -run=^$ -bench ^BenchmarkSimulation ./app -NumBlocks=200 -BlockSize 50 -Commit=true -Verbose=true -Enabled=true`
//
// $ go test -benchmem -run=^$ -bench ^BenchmarkSimulation ./app -NumBlocks=200 \
// -BlockSize 50 -Commit=true -Verbose=true -Enabled=true
func BenchmarkSimulation(b *testing.B) {
simcli.FlagSeedValue = time.Now().Unix()
simcli.FlagVerboseValue = true
Expand Down Expand Up @@ -209,7 +211,8 @@ func TestAppStateDeterminism(t *testing.T) {
if j != 0 {
require.Equal(
t, string(appHashList[0]), string(appHashList[j]),
"non-determinism in seed %d: %d/%d, attempt: %d/%d\n", config.Seed, i+1, numSeeds, j+1, numTimesToRunPerSeed,
"non-determinism in seed %d: %d/%d, attempt: %d/%d\n",
config.Seed, i+1, numSeeds, j+1, numTimesToRunPerSeed,
)
}
}
Expand Down Expand Up @@ -344,7 +347,8 @@ func TestAppImportExport(t *testing.T) {
bApp.GetKey(stakingtypes.StoreKey), newApp.GetKey(stakingtypes.StoreKey),
[][]byte{
stakingtypes.UnbondingQueueKey, stakingtypes.RedelegationQueueKey, stakingtypes.ValidatorQueueKey,
stakingtypes.HistoricalInfoKey, stakingtypes.UnbondingIDKey, stakingtypes.UnbondingIndexKey, stakingtypes.UnbondingTypeKey, stakingtypes.ValidatorUpdatesKey,
stakingtypes.HistoricalInfoKey, stakingtypes.UnbondingIDKey, stakingtypes.UnbondingIndexKey,
stakingtypes.UnbondingTypeKey, stakingtypes.ValidatorUpdatesKey,
},
}, // ordering may change but it doesn't matter
{bApp.GetKey(slashingtypes.StoreKey), newApp.GetKey(slashingtypes.StoreKey), [][]byte{}},
Expand All @@ -355,7 +359,9 @@ func TestAppImportExport(t *testing.T) {
{bApp.GetKey(govtypes.StoreKey), newApp.GetKey(govtypes.StoreKey), [][]byte{}},
{bApp.GetKey(evidencetypes.StoreKey), newApp.GetKey(evidencetypes.StoreKey), [][]byte{}},
{bApp.GetKey(capabilitytypes.StoreKey), newApp.GetKey(capabilitytypes.StoreKey), [][]byte{}},
{bApp.GetKey(authzkeeper.StoreKey), newApp.GetKey(authzkeeper.StoreKey), [][]byte{authzkeeper.GrantKey, authzkeeper.GrantQueuePrefix}},
{bApp.GetKey(authzkeeper.StoreKey), newApp.GetKey(authzkeeper.StoreKey), [][]byte{
authzkeeper.GrantKey, authzkeeper.GrantQueuePrefix,
}},
}

for _, skp := range storeKeysPrefixes {
Expand All @@ -366,7 +372,9 @@ func TestAppImportExport(t *testing.T) {
require.Equal(t, len(failedKVAs), len(failedKVBs), "unequal sets of key-values to compare")

fmt.Printf("compared %d different key/value pairs between %s and %s\n", len(failedKVAs), skp.A, skp.B)
require.Equal(t, 0, len(failedKVAs), simtestutil.GetSimulationLog(skp.A.Name(), bApp.SimulationManager().StoreDecoders, failedKVAs, failedKVBs))
require.Equal(t, 0, len(failedKVAs), simtestutil.GetSimulationLog(
skp.A.Name(), bApp.SimulationManager().StoreDecoders, failedKVAs, failedKVBs),
)
}
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/pocketd/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (

// InitSDKConfig initializes the SDK's config with the appropriate parameters
// and prefixes so everything is named appropriately for Pocket Network.
// TODO_DISCUSS: Exporting publically for testing purposes only.
// Consider adding a helper per the discussion here: https://github.com/pokt-network/poktroll/pull/59#discussion_r1357816798
// TODO_DISCUSS: Exporting publicly for testing purposes only.
// Consider adding a helper per the discussion here:
// https://github.com/pokt-network/poktroll/pull/59#discussion_r1357816798
func InitSDKConfig() {
// Set prefixes
accountPubKeyPrefix := app.AccountAddressPrefix + "pub"
Expand Down
19 changes: 14 additions & 5 deletions cmd/pocketd/cmd/genaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,20 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
},
}

cmd.Flags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test)")
cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")
cmd.Flags().String(flagVestingAmt, "", "amount of coins for vesting accounts")
cmd.Flags().Int64(flagVestingStart, 0, "schedule start time (unix epoch) for vesting accounts")
cmd.Flags().Int64(flagVestingEnd, 0, "schedule end time (unix epoch) for vesting accounts")
cmd.Flags().
String(
flags.FlagKeyringBackend,
flags.DefaultKeyringBackend,
"Select keyring's backend (os|file|kwallet|pass|test)",
)
cmd.Flags().
String(flags.FlagHome, defaultNodeHome, "The application home directory")
cmd.Flags().
String(flagVestingAmt, "", "amount of coins for vesting accounts")
cmd.Flags().
Int64(flagVestingStart, 0, "schedule start time (unix epoch) for vesting accounts")
cmd.Flags().
Int64(flagVestingEnd, 0, "schedule end time (unix epoch) for vesting accounts")
flags.AddQueryFlagsToCmd(cmd)

return cmd
Expand Down
Loading
Loading