From c70732d6af83e89ada2f342290dd0d2ed4de5e5a Mon Sep 17 00:00:00 2001 From: ~wispem-wantex Date: Sat, 19 Sep 2026 21:36:31 -0700 Subject: [PATCH] lint: enable custom linters from go-lint project --- .custom-gcl.yaml | 10 ++++++++++ .golangci.yaml | 13 +++++++++++++ ops/build-lint.sh | 39 +++++++++++++++++++++++++++++++++++++++ ops/lint.sh | 16 ++++++++++++++++ pkg/db/migration_test.go | 6 +++--- pkg/must/must.go | 4 ++-- 6 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 .custom-gcl.yaml create mode 100755 ops/build-lint.sh create mode 100755 ops/lint.sh diff --git a/.custom-gcl.yaml b/.custom-gcl.yaml new file mode 100644 index 0000000..237069e --- /dev/null +++ b/.custom-gcl.yaml @@ -0,0 +1,10 @@ +# Builds a golangci-lint binary with the shared custom linters from go-lint. +# +# The version MUST stay in lockstep with the installed golangci-lint version: +# plugins link against golangci-lint internals. +version: v2.13.1 +name: golint +destination: ./bin +plugins: + - module: git.offline-twitter.com/offline-labs/go-lint + version: v0.0.0-20261018231414-f5b6076dce76 diff --git a/.golangci.yaml b/.golangci.yaml index aa7d2fc..c6c005f 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -20,7 +20,20 @@ linters: - whitespace - wrapcheck + # Shared custom linters from ../go-lint. + - ifinit + - boolnames + settings: + custom: + ifinit: + type: module + description: if-statement init clauses must be on a single line + boolnames: + type: module + description: boolean variables and functions should use is, has, was, and similar prefixes + settings: + allowed_prefixes: ["is", "has", "can", "was", "should"] depguard: rules: main: diff --git a/ops/build-lint.sh b/ops/build-lint.sh new file mode 100755 index 0000000..2345eeb --- /dev/null +++ b/ops/build-lint.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env sh + +set -e + +SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd) +PROJECT_ROOT=$(cd "$SCRIPT_DIR/.." && pwd) +cd "$PROJECT_ROOT" # .custom-gcl.yaml is only discovered in the project root. + +# go-lint is public, but the public checksum database has not indexed it yet. +# Preserve existing exclusions while fetching the module through the normal +# proxy-then-direct resolution path. +export GONOSUMDB="${GONOSUMDB:+$GONOSUMDB,}git.offline-twitter.com" + +BIN="bin/golint" +STAMP="bin/.golint.stamp" + +# The pinned plugin version is recorded in .custom-gcl.yaml. +current_stamp() { + sha256sum .custom-gcl.yaml | cut -d' ' -f1 +} + +if [ "$1" = "--if-needed" ] && + [ -x "$BIN" ] && + [ -f "$STAMP" ] && + [ "$(cat "$STAMP")" = "$(current_stamp)" ]; then + exit 0 +fi + +if ! [ -x "$(command -v golangci-lint)" ]; then + echo "golangci-lint is not installed. Please install it from https://golangci-lint.run/welcome/install/" + exit 1 +fi + +mkdir -p bin +echo "Building golint (golangci-lint + shared custom linters)..." +golangci-lint custom +current_stamp > "$STAMP" + +echo "Done: $PROJECT_ROOT/$BIN" diff --git a/ops/lint.sh b/ops/lint.sh new file mode 100755 index 0000000..397c689 --- /dev/null +++ b/ops/lint.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env sh + +set -e + +SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd) +PROJECT_ROOT=$(cd "$SCRIPT_DIR/.." && pwd) +cd "$PROJECT_ROOT" + +./ops/build-lint.sh --if-needed +GOLANGCI="./bin/golint" + +echo "Linting $(go list ./... | wc -w) packages" + +# Catches config keys in the wrong place, which are otherwise silently ignored. +"$GOLANGCI" config verify +"$GOLANGCI" run $@ diff --git a/pkg/db/migration_test.go b/pkg/db/migration_test.go index bdbd69a..6275819 100644 --- a/pkg/db/migration_test.go +++ b/pkg/db/migration_test.go @@ -39,9 +39,9 @@ func assertForeignKeysAreGood(t *testing.T, conn *sqlx.DB) { t.Helper() // FKs should be enabled and intact at all times - var fkEnabled bool - require.NoError(t, conn.Get(&fkEnabled, `pragma foreign_keys`)) - assert.True(t, fkEnabled) + var isFkEnabled bool + require.NoError(t, conn.Get(&isFkEnabled, `pragma foreign_keys`)) + assert.True(t, isFkEnabled) assert.NoError(t, db.PragmaForeignKeyCheck(conn)) // ...and enforcement should be in effect again, not merely reported as on diff --git a/pkg/must/must.go b/pkg/must/must.go index cfc02b6..a2e97f7 100644 --- a/pkg/must/must.go +++ b/pkg/must/must.go @@ -16,8 +16,8 @@ func Get[T any](val T, err error) T { return val } -func Be(condition bool, msg string, args ...any) { - if !condition { +func Be(ok bool, msg string, args ...any) { + if !ok { panic(fmt.Errorf(msg, args...)) //nolint:err113 // not a returned error } }