diff --git a/.golangci.yml b/.golangci.yml index 858014b..c206b39 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -16,49 +16,45 @@ formatters: linters: default: all disable: - - decorder # Don't care about this - - dupl # Basically every table driven test ever triggers this - - dupword # Messes with test cases more often than not - - err113 # Out of date - - exhaustruct # No - - forbidigo # Nothing to forbid - - funlen # Bad metric for complexity - - ginkgolinter # I don't use whatever this is - - gochecknoglobals # Globals are fine sometimes, use common sense - - gocyclo # cyclop does this instead - - godox # "todo" and "fixme" comments are allowed - - goheader # No need - - gosmopolitan # No need - - grouper # Imports take care of themselves, rest is common sense - - ireturn # This is just not necessary or practical in a real codebase - - lll # Auto formatters do this and what they can't do I don't care about - - maintidx # This is just the inverse of complexity... which is cyclop - - nestif # cyclop does this - - nlreturn # Similar to wsl, I think best left to judgement - - noinlineerr # Inline errors are fine - - nonamedreturns # Named returns are often helpful, it's naked returns that are the issue - - paralleltest # I've never had Go tests take longer than a few seconds, it's fine - - unparam # gopls is better and more subtle - - varnamelen # Lots of false positives of things that are fine - - wrapcheck # Not every error must be wrapped - - wsl # Very aggressive, some of this I like but tend to do anyway + - cyclop # gocognit is the single complexity linter, cognitive beats cyclomatic + - decorder # Don't care about this + - err113 # Not everything needs this + - exhaustruct # This is just a bad idea + - forbidigo # Nothing to forbid + - funlen # Bad metric for complexity + - goconst # Got significantly more annoying recently + - gocyclo # Prefer cognitive complexity over naive metrics + - godox # "todo" and "fixme" comments are allowed + - gomodguard # Deprecated + - ireturn # AST is full of interfaces we need to return (e.g. Expression) + - lll # Auto formatters do this and what they can't do I don't care about + - maintidx # Prefer cognitive + - mnd # Annoying and too sensitive + - nestif # Nesting depth is the dominant factor in gocognit's score already + - noinlineerr # Inline errors are fine + - nonamedreturns # Named returns are often helpful documentation, it's naked returns that are the issue + - paralleltest # I've never had Go tests take longer than a few seconds, it's fine + - unparam # gopls is better and more subtle + - varnamelen # Lots of false positives of things that are fine + - wrapcheck # Not every error must be wrapped + - wsl # Deprecated exclusions: presets: # See https://golangci-lint.run/usage/false-positives/#exclusion-presets - - std-error-handling - common-false-positives + - std-error-handling rules: - path: _test\.go linters: - - prealloc # These kinds of optimisations will make no difference to test code - - gosec # Tests don't need security stuff - - goconst # Sometimes repetition is okay in tests + - dupl # Common false positives with table driven tests + - dupword # Messes with test cases more often than not + - gochecknoglobals # Flags and env vars + - goconst # Sometimes repetition is okay in tests + - gosec # Tests don't need security stuff + - prealloc # These kinds of optimisations will make no difference to test code settings: - cyclop: - max-complexity: 20 - depguard: rules: main: @@ -70,100 +66,50 @@ linters: desc: use math/rand/v2 instead errcheck: - check-type-assertions: true check-blank: true + check-type-assertions: true exhaustive: check: - - switch - map + - switch default-signifies-exhaustive: true - staticcheck: - checks: - - all - gosec: excludes: - - G104 # Errors not checked, handled by errcheck + - G104 # Errors not checked, handled by errcheck govet: enable-all: true nakedret: - max-func-lines: 0 # Disallow any naked returns + max-func-lines: 0 # Disallow any naked returns nolintlint: allow-unused: false require-explanation: true require-specific: true + revive: + # every other revive rule overlaps one or more of the other + # linters. These are the ones nothing else covers. + enable-all-rules: false + rules: + - name: context-as-argument # ctx must be the first parameter + - name: deep-exit # os.Exit / log.Fatal outside main and init + - name: modifies-parameter # don't mutate value parameters + - name: modifies-value-receiver # value receiver mutations are lost + - name: unexported-return # exported func returning an unexported type + + staticcheck: + checks: + - all + usetesting: context-background: true context-todo: true os-chdir: true + os-create-temp: true os-mkdir-temp: true os-setenv: true - os-create-temp: true os-temp-dir: true - - revive: - max-open-files: 256 - enable-all-rules: true - rules: - - name: add-constant - disabled: true # goconst does this - - - name: argument-limit - arguments: - - 5 - - - name: cognitive-complexity - disabled: true # gocognit does this - - - name: comment-spacings - arguments: - - "nolint:" - - - name: cyclomatic - disabled: true # cyclop does this - - - name: exported - arguments: - - checkPrivateReceivers - - checkPublicInterface - - - name: function-length - disabled: true # Bad proxy for complexity - - - name: function-result-limit - arguments: - - 3 - - - name: import-shadowing - disabled: true # predeclared does this - - - name: line-length-limit - disabled: true # gofmt/golines handles this well enough - - - name: max-public-structs - disabled: true # This is a dumb rule - - - name: redefines-builtin-id - disabled: true # predeclared does this - - - name: unhandled-error - arguments: - - fmt\.(Fp|P)rint(ln|f)? - - strings.Builder.Write(String|Byte)? - - bytes.Buffer.Write(String|Byte)? - - go/printer.(Fp|P)rint(ln|f)? - - - name: flag-parameter - disabled: true # As far as I can work out this just doesn't like bools - - - name: unused-parameter - disabled: true # The gopls unused analyzer covers this better - - - name: unused-receiver - disabled: true # As above diff --git a/.tangled/workflows/Lint.yml b/.tangled/workflows/Lint.yml new file mode 100644 index 0000000..b92f442 --- /dev/null +++ b/.tangled/workflows/Lint.yml @@ -0,0 +1,22 @@ +when: + - event: [push] + branch: [main] + - event: [pull_request] + branch: [main] + +engine: nixery + +dependencies: + nixpkgs/nixpkgs-unstable: + - go + - golangci-lint + +environment: + GOEXPERIMENT: "jsonv2" + +steps: + - name: Format + command: golangci-lint fmt ./... --diff-colored + + - name: Lint + command: golangci-lint run ./... diff --git a/.tangled/workflows/Test.yml b/.tangled/workflows/Test.yml new file mode 100644 index 0000000..9a2c069 --- /dev/null +++ b/.tangled/workflows/Test.yml @@ -0,0 +1,20 @@ +when: + - event: [push] + branch: [main] + - event: [pull_request] + branch: [main] + +engine: nixery + +dependencies: + nixpkgs/nixpkgs-unstable: + - go + nixpkgs: + - gcc # Needed for CGO_ENABLED=1 (go test -race) + +steps: + - name: Test + environment: + CGO_ENABLED: "1" + GOEXPERIMENT: "jsonv2" + command: go test -race ./... diff --git a/Taskfile.yml b/Taskfile.yml deleted file mode 100644 index 941a797..0000000 --- a/Taskfile.yml +++ /dev/null @@ -1,100 +0,0 @@ -# https://taskfile.dev - -version: "3" - -vars: - COV_DATA: coverage.out - -tasks: - default: - desc: List all available tasks - silent: true - cmds: - - task --list - - tidy: - desc: Tidy dependencies in go.mod and go.sum - sources: - - "**/*.go" - - go.mod - - go.sum - cmds: - - go mod tidy - - fmt: - desc: Run go fmt on all source files - sources: - - "**/*.go" - cmds: - - go fmt ./... - - test: - desc: Run the test suite - sources: - - "**/*.go" - - go.mod - - go.sum - - "**/testdata/**/*" - cmds: - - go test -race ./... {{ .CLI_ARGS }} - - bench: - desc: Run all project benchmarks - sources: - - "**/*.go" - cmds: - - go test ./... -run None -benchmem -bench . {{ .CLI_ARGS }} - - lint: - desc: Run linting - sources: - - "**/*.go" - - .golangci.yml - preconditions: - - sh: command -v golangci-lint - msg: golangci-lint not installed, see https://golangci-lint.run/usage/install/#local-installation - - - sh: command -v typos - msg: requires typos-cli, run `brew install typos-cli` - cmds: - - golangci-lint run --fix - - typos - - doc: - desc: Render the pkg docs locally - preconditions: - - sh: command -v pkgsite - msg: pkgsite not installed, run `go install golang.org/x/pkgsite/cmd/pkgsite@latest` - cmds: - - pkgsite -open - - cov: - desc: Calculate test coverage and render the html - generates: - - "{{ .COV_DATA }}" - cmds: - - go test -race -cover -covermode atomic -coverprofile {{ .COV_DATA }} ./... - - go tool cover -html {{ .COV_DATA }} - - check: - desc: Run tests and linting in one - cmds: - - task: test - - task: lint - - sloc: - desc: Print lines of code - cmds: - - fd . -e go | xargs wc -l | sort -nr | head - - clean: - desc: Remove build artifacts and other clutter - cmds: - - go clean ./... - - rm -rf {{ .COV_DATA }} - - update: - desc: Updates dependencies in go.mod and go.sum - cmds: - - go get -u ./... - - go mod tidy diff --git a/mise.toml b/mise.toml new file mode 100644 index 0000000..eee3dba --- /dev/null +++ b/mise.toml @@ -0,0 +1,81 @@ +# Dev tasks. Run `mise tasks` to list, `mise run ` to execute. +# Extra args after `--` are appended to the task's command: +# mise run test -- -run TestFoo +# mise run bench -- -benchtime 5s + +[tools] +typos = "latest" +"go:golang.org/x/pkgsite/cmd/pkgsite" = "latest" +golangci-lint = "latest" + +[env] +GOEXPERIMENT = "jsonv2" + +[vars] +COV_DATA = "coverage.out" + +[tasks.tidy] +description = "Tidy dependencies in go.mod and go.sum" +sources = ["**/*.go", "go.mod", "go.sum"] +run = "go mod tidy" + +[tasks.gen] +description = "Run go generate" +sources = ["**/*.go"] +run = "go generate ./..." + +[tasks.fmt] +description = "Run go fmt on all source files" +sources = ["**/*.go", ".golangci.yml"] +run = "golangci-lint fmt ./..." + +[tasks.test] +description = "Run the test suite" +sources = [ + "**/*.go", + "go.mod", + "go.sum", + "**/testdata/**/*", +] +# -race needs CGO (https://go.dev/doc/articles/race_detector#Requirements) +env = { CGO_ENABLED = "1" } +run = "go test -race ./..." + +[tasks.bench] +description = "Run all project benchmarks" +sources = ["**/*.go"] +run = "go test ./... -run None -benchmem -bench ." + +[tasks.lint] +description = "Run linting" +depends = ["fmt"] +sources = ["**/*.go", ".golangci.yml"] +run = ["golangci-lint run --fix", "typos"] + +[tasks.doc] +description = "Render the pkg docs locally" +run = "pkgsite -open" + +[tasks.cov] +description = "Calculate test coverage and render the html" +outputs = ["{{ vars.COV_DATA }}"] +run = [ + "go test -race -cover -covermode atomic -coverprofile {{ vars.COV_DATA }} ./...", + "go tool cover -html {{ vars.COV_DATA }}", +] + +[tasks.check] +description = "Run tests and linting in one" +depends = ["test", "lint"] + +[tasks.sloc] +description = "Print lines of code" +run = "fd . -e go | xargs wc -l | sort -nr | head" + +[tasks.clean] +description = "Remove build artifacts and other clutter" +run = ["go clean ./...", "rm -rf {{ vars.COV_DATA }} ./bin ./dist"] + +[tasks.update] +description = "Updates dependencies in go.mod and go.sum" +run = ["go get -u ./...", "go mod tidy"]