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

add go linting #1705

Merged
merged 53 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
a0c2ff7
add linters and vscode settings from srsim
k0l11 Sep 11, 2023
80a3501
adjust lint to exclude wasm cli
k0l11 Sep 11, 2023
12ad613
format vscode settings
k0l11 Sep 11, 2023
5551a6d
enable gosimple linter
k0l11 Sep 11, 2023
77ac52c
enable govet linter
k0l11 Sep 11, 2023
6674976
enable ineffassign linter
k0l11 Sep 11, 2023
06e84ae
delete scripts that are not needed
k0l11 Sep 11, 2023
d24af70
enable staticcheck linter
k0l11 Sep 11, 2023
0ac9a8f
enable typecheck linter
k0l11 Sep 11, 2023
2279b80
add .gitattributes for linting just in case
k0l11 Sep 11, 2023
ec87e13
add gofmt linter
k0l11 Sep 11, 2023
5429da3
add goimports linter
k0l11 Sep 11, 2023
8be5d6c
add stylecheck linter
k0l11 Sep 11, 2023
eaeb104
add gocritic linter
k0l11 Sep 12, 2023
a8a4991
add whitespace linter
k0l11 Sep 12, 2023
036fb62
add nolintlint linter
k0l11 Sep 12, 2023
c8bd597
add errname linter
k0l11 Sep 12, 2023
ea8dc51
add nonamedreturns linter
k0l11 Sep 12, 2023
1bf075a
add revive linter
k0l11 Sep 12, 2023
85adda8
add tagalign linter
k0l11 Sep 12, 2023
2accf11
add tagliatelle linter
k0l11 Sep 12, 2023
5674ddd
add nestif linter
k0l11 Sep 12, 2023
6af6381
add asasalint linter
k0l11 Sep 12, 2023
ce3bfc7
add bidichk linter
k0l11 Sep 12, 2023
44141cc
add unused linter
k0l11 Sep 12, 2023
5ec0472
add dogsled linter
k0l11 Sep 12, 2023
f765ece
add errorlint linter
k0l11 Sep 12, 2023
a7bb1dd
add errchkjson linter
k0l11 Sep 12, 2023
c042fa0
add exportloopref linter
k0l11 Sep 12, 2023
62d5730
add makezero linter
k0l11 Sep 12, 2023
9dbdd46
add nilerr linter
k0l11 Sep 12, 2023
430ad71
add nakedret linter
k0l11 Sep 12, 2023
daf109a
add wastedassign linter
k0l11 Sep 12, 2023
14509b0
add unparam linter
k0l11 Sep 12, 2023
bab52c0
add unconvert linter
k0l11 Sep 12, 2023
3520264
add goconst linter
k0l11 Sep 12, 2023
121f24e
add predeclared linter
k0l11 Sep 12, 2023
1926ed6
add reassign linter
k0l11 Sep 12, 2023
d69385a
add musttag linter
k0l11 Sep 12, 2023
1eb0108
add gocheckcompilerdirectives linter
k0l11 Sep 12, 2023
f869c03
add gomodguard linter
k0l11 Sep 12, 2023
e3fe3c4
add mirror linter
k0l11 Sep 12, 2023
cb2367f
add tenv linter
k0l11 Sep 12, 2023
f1a31da
add tparallel linter
k0l11 Sep 12, 2023
76a64a3
add usestdlibvars linter
k0l11 Sep 12, 2023
bda6058
adjust whitespace in linter config
k0l11 Sep 12, 2023
00a03a5
increase linter timeout
k0l11 Sep 12, 2023
ff5ce01
use same go version during linting as in test
k0l11 Sep 12, 2023
9ee6a3e
try using latest golangci-lint version
k0l11 Sep 12, 2023
db395e8
fix gocritic copying rule unintentional shadowing
k0l11 Sep 12, 2023
e87d7ce
fix eval bug introduced by gocritic
k0l11 Sep 12, 2023
e5af3a1
fix icd not working after nestif changes
k0l11 Sep 12, 2023
ff920cf
fix wrong hitlag parsing change via goconst
k0l11 Sep 13, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# https://github.com/golangci/golangci-lint-action#multiple-os-support
*.go text eol=lf
55 changes: 55 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: golangci-lint
on:
push:
branches:
- master
- main
pull_request:

permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
# pull-requests: read

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: "^1.19.3"
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
# Require: The version of golangci-lint to use.
# When `install-mode` is `binary` (default) the value can be v1.2 or v1.2.3 or `latest` to use the latest version.
# When `install-mode` is `goinstall` the value can be v1.2.3, `latest`, or the hash of a commit.
version: "latest"

# Optional: working directory, useful for monorepos
# working-directory: somedir

# Optional: golangci-lint command line arguments.
#
# Note: By default, the `.golangci.yml` file should be at the root of the repository.
# The location of the configuration file can be changed by using `--config=`
# args: --timeout=30m --config=/my/path/.golangci.yml --issues-exit-code=0

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true

# Optional: if set to true, then all caching functionality will be completely disabled,
# takes precedence over all other caching options.
# skip-cache: true

# Optional: if set to true, then the action won't cache or restore ~/go/pkg.
# skip-pkg-cache: true

# Optional: if set to true, then the action won't cache or restore ~/.cache/go-build.
# skip-build-cache: true

# Optional: The mode to install golangci-lint. It can be 'binary' or 'goinstall'.
# install-mode: "goinstall"
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ internal/characters/replace.txt
./cmd/gcsim/*.json
cmd/gcsim/gcsim
!release-notes.txt
\.vscode/
**/node_modules
.env
**/protos_gen
Expand Down
181 changes: 181 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
run:
timeout: 5m
build-tags:
- codeanalysis

linters:
disable-all: true
enable:
## golangci defaults (core linters)
# - errcheck # checking for unchecked errors, these unchecked errors can be critical bugs in some cases
- gosimple # specializes in simplifying a code
- govet # reports suspicious constructs, such as Printf calls whose arguments do not align with the format string
- ineffassign # detects when assignments to existing variables are not used
- staticcheck # is a go vet on steroids, applying a ton of static analysis checks
- typecheck # like the front-end of a Go compiler, parses and type-checks Go code

## style & formatting
- gofmt # go fmt requirement
- goimports # in addition to fixing imports, goimports also formats your code in the same style as gofmt
- stylecheck # style check rules defined by staticcheck (minimum go standard style)
- gocritic # provides diagnostics that check for bugs, performance and style issues
- whitespace # detects leading and trailing whitespace
- nolintlint # reports ill-formed or insufficient nolint directives
- errname # checks that sentinel errors are prefixed with the Err and error types are suffixed with the Error
- nonamedreturns # reports all named returns
- revive # fast, configurable, extensible, flexible, and beautiful linter for Go, drop-in replacement of golint
- tagalign # checks that struct tags are well aligned
- tagliatelle # checks the struct tags are snake case
# TODO: use this when merged into tagliatelle https://github.com/ldez/tagliatelle/pull/21

## code complexity detectors
- nestif # reports deeply nested if statements
# - lll # reports long lines
# - funlen # tool for detection of long functions
# - gocognit # computes and checks the cognitive complexity of functions
# - gocyclo # computes and checks the cyclomatic complexity of functions
# - cyclop # checks function and package cyclomatic complexity

## code smell (can lead to errors/bugs)
- asasalint # checks for pass []any as any in variadic func(...any)
- bidichk # checks for dangerous unicode character sequences
- unused # checks for unused constants, variables, functions and types
- dogsled # checks assignments with too many blank identifiers (e.g. x, , , _, := f())
- errorlint # finds code that will cause problems with the error wrapping scheme introduced in Go 1.13
- errchkjson # checks for unnecessary json marshal error checks
- exportloopref # checks for pointers to enclosing loop variables
- makezero # finds slice declarations with non-zero initial length
- nilerr # finds the code that returns nil even if it checks that the error is not nil
- nakedret # finds naked returns in functions greater than a specified function length
- wastedassign # finds wasted assignment statements
- unparam # reports unused function parameters
- unconvert # removes unnecessary type conversions
- goconst # finds repeated strings that could be replaced by a constant
- predeclared # finds code that shadows one of Go's predeclared identifiers
- reassign # checks that package variables are not reassigned
# - dupl # tool for code clone detection
# - nilnil # checks that there is no simultaneous return of nil error and an invalid value

## enforcements which can lead to better code
- musttag # enforces field tags in (un)marshaled structs
# - exhaustruct # checks if all structure fields are initialized

## misc linters
- gocheckcompilerdirectives # validates go compiler directive comments (//go:)
- gomodguard # allow and block lists linter for direct Go module dependencies.
- mirror # reports wrong mirror patterns of bytes/strings usage
- tenv # detects using os.Setenv instead of t.Setenv since Go1.17
- tparallel # detects inappropriate usage of t.Parallel() method in your Go test codes
- usestdlibvars # detects the possibility to use variables/constants from the Go standard library

linters-settings:
cyclop:
max-complexity: 30
package-average: 10.0

errcheck:
check-type-assertions: true

exhaustruct:
exclude:
## sim code

## dependencies
- "net/http"
# - 'github\.com/urfave/cli/v2'
- 'google\.golang\.org/protobuf/encoding/protojson'

funlen:
lines: 100
statements: 50

gocognit:
min-complexity: 20

gocritic:
enabled-tags:
- performance # finds places where we can make performance improvements
- opinionated # opinionated style rules
disabled-checks:
- importShadow # covered by another linter
- builtinShadow # covered by another linter
- unnamedResult # opposite of nonamedreturns
- hugeParam # probably want to enable for better performance. Benchmark first
settings:
captLocal:
paramsOnly: false
underef:
skipRecvDeref: false

revive:
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md
rules:
- name: context-as-argument
- name: context-keys-type
- name: dot-imports
- name: error-return
- name: error-strings
- name: error-naming
- name: exported
- name: if-return
- name: var-naming
- name: var-declaration
- name: package-comments
- name: range
- name: receiver-naming
- name: time-naming
- name: unexported-return
- name: indent-error-flow
- name: errorf
- name: empty-block
- name: superfluous-else
- name: unreachable-code
- name: redefines-builtin-id

govet:
enable-all: true
check-shadowing: false
disable:
- fieldalignment # great for micro-optimizations, too much work for us to maintain
- shadow # can help to find hard to find bugs, but annoying to make golang code compatible

gomodguard:
blocked:
modules:
- github.com/golang/protobuf:
recommendations:
- google.golang.org/protobuf
reason: "see https://protobuf.dev/reference/go/faq/#modules"

nakedret:
max-func-lines: 0

nolintlint:
allow-no-explanation: [funlen, gocognit, lll]
require-explanation: true
require-specific: true

tenv:
all: true

tagliatelle:
case:
use-field-name: true
rules:
json: snake

severity:
default-severity: error

issues:
max-same-issues: 50
exclude-rules:
- source: "//noinspection"
linters: [gocritic]
- linters:
- stylecheck
- revive
text: ".*Id should be .*ID"
- linters:
- tagliatelle
text: "got '_id' want 'id'" # play nice with MongoDB
13 changes: 13 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"recommendations": [
"bradlc.vscode-tailwindcss",
"zxh404.vscode-proto3",
"redhat.vscode-yaml",
"mgmcdermott.vscode-language-babel",
"dbaeumer.vscode-eslint",
"golang.go",
"visualstudioexptteam.vscodeintellicode",
"eriklynd.json-tools",
"esbenp.prettier-vscode"
]
}
31 changes: 31 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},

"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},

k0l11 marked this conversation as resolved.
Show resolved Hide resolved
"go.lintTool": "golangci-lint",
"go.lintFlags": ["--fast", "--fix"],
"go.lintOnSave": "package",

"search.exclude": {
"**/.yarn": true,
"**/.pnp.*": true
},
"typescript.preferences.importModuleSpecifier": "shortest",
"eslint.workingDirectories": [
{
"mode": "auto"
}
]
}
33 changes: 27 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Character PR checklist:
- [ ] Normal Attack
- [ ] Charge Attack / Aimed Shot
- [ ] Skill
- [ ] Burst
- [ ] Burst
- [ ] A1
- [ ] A4
- [ ] C0
Expand Down Expand Up @@ -127,26 +127,47 @@ Artifact PR checklist:
- [ ] 2pc
- [ ] 4pc
- [ ] Update documentation
```

````
</details>


# Git/Github Quick Reference Guide
For those who are new to git/github

```git checkout -b newbranchname``` creates a new branch from your current branch
If you have committed code, but upon finishing your feature, the main branch has progressed, you are encouraged to rebase it to ensure it still works.
If you have committed code, but upon finishing your feature, the main branch has progressed, you are encouraged to rebase it to ensure it still works.
Please reach out for help if you are not sure how to do this step, the following steps can be dangerous and you can lose your work if not done correctly.

To rebase your branch you will need to run the command
```
````

git rebase --onto <newparent> <oldparent>
git push -f
```

````
Where new parent is the commitment hash of the newest commit on the main branch and old parent is the commitment hash of the oldest common commitment between your feature branch and the main branch.


# Dev Environment Setup

Install the following:
- Golang v1.21+: https://go.dev/doc/install
- Alternative: https://github.com/stefanmaric/g this will keep golang up to date and configure all the necessary paths for you
- Git: https://github.com/git-guides/install-git
- (optional) Protobuf compiler v21+: https://github.com/protocolbuffers/protobuf/releases
- vscode is the “officially supported” IDE for the repo: https://code.visualstudio.com/
- Install the recommended extensions.
- golangci-lint: If using vscode, should give popup to install
- Alternative: manual install https://golangci-lint.run/usage/install/#local-installation
- Linters that use diffs (for example: gofmt) require [additional setup on Windows](https://github.com/golangci/golangci-lint/issues/307#issuecomment-1001301930) (This assumes you already installed Git for Windows!):
- Open PowerShell as Administrator
- execute the following command: setx /M PATH "$($env:path);C:\Program Files\git\usr\bin"
- Restart VS Code if it is currently open for the changes to take effect.

# Local Testing/Building
0. Create a Config file
1. Navigate to ```./gcsim/cmd/gcsim```
2. Run ```go build``` to build the executable and then feed your config file in e.g. ```./gcsim.exe -c config.txt -sample config -gz``` OR run ```go run . --c config.txt -sample config -gz```
2. Run ```go build``` to build the executable and then feed your config file in e.g. ```./gcsim.exe -c config.txt -sample config -gz``` OR run ```go run . --c config.txt -sample config -gz```
3. Upload the generated sample file to the [Sample page](https://gcsim.app/sample/upload) to confirm everything is working accordingly, and optionally share the sample file in discord for debugging help.
````
Loading