Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Enforce and maintain ratchet policies (#81319)

Chris Truterbc0200ece0d3
diff --git a/.clj-kondo/ratchets.edn b/.clj-kondo/ratchets.edn
index 2e1a302e983..81c2b836c15 100644
--- a/.clj-kondo/ratchets.edn
+++ b/.clj-kondo/ratchets.edn
@@ -1,7 +1,8 @@
;; Budgets for kondo suppressions: inline `:clj-kondo/ignore` forms per linter (:ignore-counts),
;; and config-level waivers in .clj-kondo/config.edn (:config-counts -- :off switches and :exclude
-;; entries). metabase.core.kondo-ratchet-test fails when either drifts from reality, or when an
-;; ignore outside :comment-exempt lacks an explanatory comment directly above or trailing on its line.
+;; entries). Each :ignore-counts value is a non-negative integer budget, or :unlimited for no ceiling.
+;; CI rejects counts above a numeric budget; local tests require those counts to match exactly.
+;; Any ignore outside :comment-exempt needs an explanatory comment directly above or trailing on its line.
;; `./bin/mage fix-kondo-ratchets` lowers budgets and drops stale exemptions; local test runs do it
;; automatically. Raising a budget, adding one (`--seed` for inline, by hand for config), or
;; widening the exemptions is a hand edit to defend in your PR.
@@ -41,14 +42,13 @@
:uninitialized-var 1
:unquote-not-syntax-quoted 1
:unresolved-namespace 3
- :unresolved-require 1
:unresolved-symbol 11
- :unsorted-required-namespaces 1
+ :unsorted-required-namespaces :unlimited
:unused-alias 2
:unused-binding 9
:unused-excluded-var 2
:unused-namespace 2
- :unused-private-var 9
+ :unused-private-var 8
:unused-value 1}
:config-counts {:constant-condition 1
:deprecated-namespace 7
@@ -76,6 +76,4 @@
:metabase/modules
:metabase/validate-defendpoint-has-response-schema
:metabase/validate-defendpoint-query-params-use-kebab-case
- :metabase/validate-defendpoint-route-uses-kebab-case
- :unused-excluded-var
- :unused-private-var}}
+ :metabase/validate-defendpoint-route-uses-kebab-case}}
diff --git a/.github/file-paths.yaml b/.github/file-paths.yaml
index 9ad7a4328c0..16e144c0a65 100644
--- a/.github/file-paths.yaml
+++ b/.github/file-paths.yaml
@@ -64,6 +64,9 @@ frontend_specs: &frontend_specs
- "frontend/**/*.unit.*"
- "enterprise/frontend/**/*.unit.*"
- ".github/**/*.unit.spec.*"
+ # file-paths.unit.spec.ts asserts what this file's own patterns match, so a change to it has to
+ # run the specs that guard it.
+ - ".github/file-paths.yaml"
- "jest.config.js"
- "jest.tz.unit.conf.json"
@@ -112,7 +115,10 @@ backend_sources: &backend_sources
- "src/**"
- "modules/drivers/{*,*/{*,!(test)/**}}"
- "resources/**"
- - ".clj-kondo/**"
+ # The ratchets file drives project_ratchet_checks alone, so the automation PR that lowers the budgets
+ # runs the Project tests job by itself. Patterns are OR-ed, so a separate "!..." entry would match
+ # every other path instead of excluding this one.
+ - ".clj-kondo/{!(ratchets.edn),*/**}"
backend_specs: &backend_specs
- *shared_specs
diff --git a/.github/scripts/file-paths.unit.spec.ts b/.github/scripts/file-paths.unit.spec.ts
new file mode 100644
index 00000000000..f7922c92625
--- /dev/null
+++ b/.github/scripts/file-paths.unit.spec.ts
@@ -0,0 +1,57 @@
+import { readFileSync } from "fs";
+import { join } from "path";
+
+import { load } from "js-yaml";
+import { isMatch } from "micromatch";
+
+const FILTERS = join(__dirname, "..", "file-paths.yaml");
+
+type Filter = string | Filter[] | { [changeType: string]: Filter };
+
+// A filter is a list of patterns, and an entry may name the change types it applies to as
+// { "added|modified": pattern }. Either way the patterns are what matters here.
+const patterns = (value: Filter): string[] => {
+ if (typeof value === "string") return [value];
+ if (Array.isArray(value)) return value.flatMap(patterns);
+ return Object.values(value).flatMap(patterns);
+};
+
+describe("file-paths.yaml", () => {
+ const filters = load(readFileSync(FILTERS, "utf8")) as Record<string, Filter>;
+
+ // dorny/paths-filter matches with picomatch, which micromatch wraps, so this asks the same
+ // question CI asks of a changed path.
+ const matches = (name: string, path: string) =>
+ patterns(filters[name]).some((pattern) =>
+ isMatch(path, pattern, { dot: true }),
+ );
+
+ // dorny/paths-filter ORs the patterns in a filter, so a "!..." entry does not subtract from the
+ // other entries - it matches every path they don't, turning the filter permanently on. Excluding
+ // a path means writing the exclusion inside one pattern, the way backend_sources keeps
+ // ratchets.edn out with ".clj-kondo/{!(ratchets.edn),*/**}".
+ it("never excludes a path with a standalone negation", () => {
+ const negated = Object.entries(filters).flatMap(([name, filter]) =>
+ patterns(filter)
+ .filter((pattern) => pattern.startsWith("!"))
+ .map((pattern) => `${name}: ${pattern}`),
+ );
+
+ expect(negated).toEqual([]);
+ });
+
+ it.each(["backend_sources", "backend_all"])(
+ "%s skips a ratchets-only change and still follows the rest of .clj-kondo",
+ (name) => {
+ expect(matches(name, ".clj-kondo/ratchets.edn")).toBe(false);
+ expect(matches(name, ".clj-kondo/config.edn")).toBe(true);
+ expect(matches(name, ".clj-kondo/config/modules/config.edn")).toBe(true);
+ },
+ );
+
+ it("runs the ratchet check on the ratchets file", () => {
+ expect(matches("project_ratchet_checks", ".clj-kondo/ratchets.edn")).toBe(
+ true,
+ );
+ });
+});
diff --git a/.github/workflows/kondo-ratchets-self-heal.yml b/.github/workflows/kondo-ratchets-self-heal.yml
deleted file mode 100644
index 8309c901c43..00000000000
--- a/.github/workflows/kondo-ratchets-self-heal.yml
+++ /dev/null
@@ -1,89 +0,0 @@
-name: Kondo Ratchets Self-Heal
-
-# Stale budgets in .clj-kondo/ratchets.edn (above the actual ignore counts) fail CI. On PRs
-# labelled kondo-ratchets-self-healing, this commits the lowered budgets to the branch;
-# unlabelled PRs rely on local test runs to tighten the file.
-#
-# Two jobs so the automation token never shares an environment with PR-controlled code: the
-# ratchet-down job runs ./bin/mage from the PR branch and only exports the fixed file as an
-# artifact; the push job starts from a clean runner, executes nothing from the PR, and pushes.
-permissions:
- contents: read
-
-on:
- pull_request:
- types: [opened, synchronize, reopened, labeled, unlabeled]
- paths:
- - "**.clj"
- - "**.cljc"
- - "**.cljs"
- # config-level suppressions are ratcheted too, so removing one here can leave a stale budget
- - ".clj-kondo/config.edn"
-
-concurrency:
- group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
- cancel-in-progress: true
-
-jobs:
- ratchet-down:
- runs-on: ${{ vars.DEFAULT_RUNNER_KEY }}
- timeout-minutes: 10
- # fork PRs don't get the automation token, so we couldn't push to them anyway
- if: >-
- contains(github.event.pull_request.labels.*.name, 'kondo-ratchets-self-healing')
- && github.event.pull_request.head.repo.full_name == github.repository
- outputs:
- changed: ${{ steps.diff.outputs.changed }}
- steps:
- - uses: actions/checkout@v6
- with:
- ref: ${{ github.event.pull_request.head.ref }}
- persist-credentials: false
- - name: Prepare back-end environment
- uses: ./.github/actions/prepare-backend
- - name: Lower ignore budgets to match the source tree
- run: ./bin/mage fix-kondo-ratchets
- - name: Check for changes
- id: diff
- run: |
- if git diff --quiet .clj-kondo/ratchets.edn; then
- echo "budgets already tight"
- echo "changed=false" >> "$GITHUB_OUTPUT"
- else
- echo "changed=true" >> "$GITHUB_OUTPUT"
- fi
- - uses: actions/upload-artifact@v7
- if: steps.diff.outputs.changed == 'true'
- with:
- name: ratchets-edn
- path: .clj-kondo/ratchets.edn
- # dot-directories are excluded by default, which would ship an empty artifact
- include-hidden-files: true
-
- push:
- needs: ratchet-down
- if: needs.ratchet-down.outputs.changed == 'true'
- runs-on: ${{ vars.DEFAULT_RUNNER_KEY }}
- timeout-minutes: 5
- steps:
- - uses: actions/checkout@v6
- with:
- ref: ${{ github.event.pull_request.head.ref }}
- persist-credentials: false
- - uses: actions/download-artifact@v8
- with:
- name: ratchets-edn
- path: .clj-kondo/
- - name: Commit to the PR branch
- env:
- PR_BRANCH: ${{ github.event.pull_request.head.ref }}
- REPO: ${{ github.repository }}
- # METABASE_AUTOMATION_USER_TOKEN so the self-healing commit triggers CI
- # (a GITHUB_TOKEN push would not)
- AUTOMATION_TOKEN: ${{ secrets.METABASE_AUTOMATION_USER_TOKEN }}
- run: |
- git config --global user.name "Metabase Automation"
- git config --global user.email "[email protected]"
- git add .clj-kondo/ratchets.edn
- git commit -m "Ratchet down kondo ignore budgets" --no-verify
- git push "https://x-access-token:${AUTOMATION_TOKEN}@github.com/${REPO}.git" "HEAD:$PR_BRANCH"
diff --git a/.github/workflows/kondo-ratchets-shrink.yml b/.github/workflows/kondo-ratchets-shrink.yml
new file mode 100644
index 00000000000..dfc2bd2b2c8
--- /dev/null
+++ b/.github/workflows/kondo-ratchets-shrink.yml
@@ -0,0 +1,160 @@
+name: Shrink ratchets
+
+# Lowers the budgets in .clj-kondo/ratchets.edn to match master and delivers the result through one
+# automation PR, because the rulesets require a pull request for every change to master. The PR is
+# approved by github-actions[bot] and merges itself once the ratchet check passes. A merge of that PR
+# touches only ratchets.edn, which matches `.clj-kondo/**` below, so it triggers one more run that finds
+# nothing to lower and stops: a single no-op, not a loop.
+
+on:
+ push:
+ branches: [master]
+ paths:
+ - "**.clj"
+ - "**.cljc"
+ - "**.cljs"
+ - ".clj-kondo/**"
+ - "bb.edn"
+ workflow_dispatch:
+
+permissions:
+ # checkout and the fetch of the automation branch
+ contents: read
+ # the compare step reads whether the automation PR is still open, and the approve step reviews it as
+ # github-actions[bot]; pushes and the PR itself use the automation user's token
+ pull-requests: write
+
+# When master is pushed again, the newest run cancels the older one, checks out the latest master it can
+# see, and force-updates the automation branch. The PR therefore contains one commit on top of master.
+concurrency:
+ group: kondo-ratchets-shrink
+ cancel-in-progress: true
+
+env:
+ AUTOMATION_BRANCH: automation/shrink-ratchets
+ RATCHETS_FILE: .clj-kondo/ratchets.edn
+
+jobs:
+ shrink:
+ name: Shrink ratchets
+ # A fork with Actions enabled has no automation token, so this would only ever fail at the push.
+ if: github.repository == 'metabase/metabase'
+ runs-on: ${{ vars.DEFAULT_RUNNER_KEY }}
+ timeout-minutes: 10
+ steps:
+ # `ref: master` checks out the branch head at checkout time rather than the pushed commit, so a run
+ # that queued behind another one starts from the newest master it can see.
+ - uses: actions/checkout@v6
+ with:
+ ref: master
+ # The persisted GITHUB_TOKEN header would override the automation token in the push URL and
+ # get the force-push rejected. Nothing here needs it: the repo is public, so the fetch of the
+ # automation branch is anonymous, and every gh call carries its own token.
+ persist-credentials: false
+ - name: Set up Java for cold dependency resolution
+ uses: actions/[email protected]
+ with:
+ distribution: temurin
+ java-version: 25
+ - name: Shrink ignore limits
+ run: ./bin/mage fix-kondo-ratchets
+ # Two independent outputs: `push` says the branch needs a new commit, `pr` names the open automation
+ # PR if there is one. Approval and auto-merge key on `pr` alone, so a run that failed after opening
+ # the PR is repaired by the next run instead of leaving the PR stranded.
+ - name: Compare with master and the automation PR
+ id: compare
+ env:
+ GH_TOKEN: ${{ github.token }}
+ run: |
+ # The REST `head=owner:branch` filter is applied server-side and only matches this repository's
+ # branch, so a fork carrying a branch of the same name can never hand its PR number to the
+ # approval and auto-merge steps, however many such forks exist. Only an open PR counts; a branch
+ # left behind by a PR closed without merging is overwritten below and gets a new PR, so a closed
+ # one never stops the budgets from being lowered.
+ open_pr=$(gh api "repos/${GITHUB_REPOSITORY}/pulls?state=open&base=master&head=${GITHUB_REPOSITORY_OWNER}:${AUTOMATION_BRANCH}" \
+ --jq '.[0].number // empty')
+ echo "pr=$open_pr" >> "$GITHUB_OUTPUT"
+
+ if git diff --quiet -- "$RATCHETS_FILE"; then
+ echo "Ratchet budgets already match master $(git rev-parse --short HEAD)"
+ [ -z "$open_pr" ] || echo "The open automation PR #$open_pr is re-approved below"
+ echo "push=false" >> "$GITHUB_OUTPUT"
+ exit 0
+ fi
+
+ # The ratchet file is the whole payload, so an open PR that already carries this result is left
+ # alone. Rebasing it onto every master push would restart its CI and churn the diff without
+ # changing what it delivers. The diff is against the working tree, holding what
+ # fix-kondo-ratchets produced above, and so asks whether the PR would deliver the same file.
+ #
+ # A conflicted PR is the exception: only a fresh commit clears it. Mergeability is computed
+ # lazily, so a pending UNKNOWN reads as not conflicted and the next run settles it.
+ if [ -n "$open_pr" ] \
+ && git fetch --quiet --depth=1 origin "refs/heads/$AUTOMATION_BRANCH" 2>/dev/null \
+ && git diff --quiet FETCH_HEAD -- "$RATCHETS_FILE" \
+ && [ "$(gh pr view "$open_pr" --repo "$GITHUB_REPOSITORY" \
+ --json mergeable --jq .mergeable)" != CONFLICTING ]; then
+ echo "The automation PR #$open_pr already carries this result"
+ echo "push=false" >> "$GITHUB_OUTPUT"
+ echo "head=$(git rev-parse FETCH_HEAD)" >> "$GITHUB_OUTPUT"
+ exit 0
+ fi
+
+ git diff --stat -- "$RATCHETS_FILE"
+ echo "push=true" >> "$GITHUB_OUTPUT"
+ - name: Update the automation PR
+ id: deliver
+ if: steps.compare.outputs.push == 'true'
+ # The automation user's token pushes the branch and owns the PR: a branch pushed with
+ # GITHUB_TOKEN starts no pull_request workflows, so the PR would never get its checks. The commit
+ # identity is the docs generator's, which merges with one approval under the unattributed-changes
+ # rule. Nothing raises a budget.
+ env:
+ GH_TOKEN: ${{ secrets.METABASE_AUTOMATION_USER_TOKEN }}
+ OPEN_PR: ${{ steps.compare.outputs.pr }}
+ run: |
+ git config user.name github-actions
+ git config user.email [email protected]
+ git add -- "$RATCHETS_FILE"
+ git commit --quiet -m "Tighten ratchets"
+ git push --force --quiet \
+ "https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" \
+ "HEAD:refs/heads/$AUTOMATION_BRANCH"
+ echo "head=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
+
+ if [ -n "$OPEN_PR" ]; then
+ echo "Updated the automation PR #$OPEN_PR"
+ echo "pr=$OPEN_PR" >> "$GITHUB_OUTPUT"
+ exit 0
+ fi
+
+ url=$(gh pr create \
+ --base master \
+ --head "$AUTOMATION_BRANCH" \
+ --title "Tighten ratchets" \
+ --label "no-backport" \
+ --body "\`./bin/mage fix-kondo-ratchets\` lowered \`$RATCHETS_FILE\` to match master.
+ The shrink workflow force-updates this branch from master whenever the result changes, so the
+ diff is always one commit on top of a recent master. It approves and merges itself once the
+ ratchet check passes; nothing is raised.")
+ echo "Opened the automation PR $url"
+ echo "pr=${url##*/}" >> "$GITHUB_OUTPUT"
+ - name: Approve the automation PR
+ if: (steps.deliver.outputs.pr || steps.compare.outputs.pr) != ''
+ # GITHUB_TOKEN reviews as github-actions[bot], a different identity from the automation user that
+ # owns the PR, so the approval counts; a user cannot approve their own PR. Reviews survive a push,
+ # so approving an already-open PR again is harmless and keeps a reopened one covered.
+ env:
+ GH_TOKEN: ${{ github.token }}
+ PR_NUMBER: ${{ steps.deliver.outputs.pr || steps.compare.outputs.pr }}
+ run: gh pr review --approve "$PR_NUMBER"
+ # Armed only against the commit this run pushed or compared: the branch name is fixed, and anyone
+ # with push access could otherwise collect this run's approval for their own commit.
+ - name: Enable auto-merge
+ if: (steps.deliver.outputs.pr || steps.compare.outputs.pr) != ''
+ && (steps.deliver.outputs.head || steps.compare.outputs.head) != ''
+ env:
+ GH_TOKEN: ${{ secrets.METABASE_AUTOMATION_USER_TOKEN }}
+ PR_NUMBER: ${{ steps.deliver.outputs.pr || steps.compare.outputs.pr }}
+ HEAD_SHA: ${{ steps.deliver.outputs.head || steps.compare.outputs.head }}
+ run: gh pr merge --squash --auto --match-head-commit "$HEAD_SHA" "$PR_NUMBER"
diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml
index 531899a8e0d..229f20504d7 100644
--- a/.github/workflows/run-tests.yml
+++ b/.github/workflows/run-tests.yml
@@ -68,8 +68,10 @@ jobs:
project_backend_checks: ${{ steps.changes.outputs.project_backend_checks }}
project_migration_checks: ${{ steps.changes.outputs.project_migration_checks }}
project_ratchet_checks: ${{ steps.changes.outputs.project_ratchet_checks }}
+ # A ratchets-only change (the shrink automation PR) needs the ratchet check, not an uberjar.
no_source_code_changes: >-
- ${{ steps.changes.outputs.documentation == 'true'
+ ${{ (steps.changes.outputs.documentation == 'true'
+ || steps.changes.outputs.project_ratchet_checks == 'true')
&& steps.changes.outputs.backend_all == 'false'
&& steps.changes.outputs.frontend_all == 'false'
&& steps.changes.outputs.e2e_all == 'false' }}
diff --git a/CLAUDE.md b/CLAUDE.md
index 75280e89f2b..37d5e0bfa02 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -87,24 +87,33 @@ Run the repository-level module, ratchet, and migration checks with:
`.clj-kondo/ratchets.edn` records, per linter, how many inline `:clj-kondo/ignore` forms the backend source
tree may contain, and how many config-level suppressions (`:off` switches and `:exclude` entries in
-`.clj-kondo/config.edn`) exist. `metabase.core.kondo-ratchet-test` fails when either budget drifts from the
-actual counts, in either direction. Prefer fixing the underlying warning over adding an ignore.
+`.clj-kondo/config.edn`) exist. Each `:ignore-counts` value is either an exact integer budget or `:unlimited`,
+which has no count ceiling. This count policy is independent of `:comment-exempt`, described below.
+Development enforces the file exactly, while CI rejects only over-budget suppressions and leaves reductions
+and stale exemptions for the shrink workflow to record.
+Prefer fixing the underlying warning over adding an ignore. Every policy key must name a linter: one of the
+pinned clj-kondo version's built-ins, a linter configured under `.clj-kondo/`, or an external diagnostic
+such as `:clojure-lsp/unused-public-var`.
+The test and the fixer refuse unknown names rather than dropping them.
The ratchets apply only to `master`. When a release branch is cut, `.clj-kondo/ratchets.edn` is replaced
with `{:disabled true}`. The test and fixer recognize this explicit opt-out, while a missing file still
causes an error on `master`.
-Budget too high (you removed ignores): a local run of the test tightens the file for you — commit the
-change. PRs labelled `kondo-ratchets-self-healing` get the lowered budgets committed to the branch by CI.
-To tighten by hand (babashka, no JVM; a no-op prints `unchanged`):
+Budget too high (you removed ignores): once the change lands on `master`, the shrink workflow lowers the
+budgets in the `Tighten ratchets` automation PR, which approves and merges itself once the ratchet check
+passes. A bounded budget that reaches zero is dropped; an `:unlimited` entry stays even with no ignores
+left, and the check and fixer print one warning naming such entries so you can delete them by hand. To
+tighten by hand (babashka, no JVM; a no-op prints `unchanged`):
```bash
./bin/mage fix-kondo-ratchets
```
-Budget too low (you added an ignore): the task only raises a budget when told to. If the ignore is
-genuinely required, run `./bin/mage fix-kondo-ratchets --seed :the-linter` and defend the increase in the
-PR.
+Budget too low (you added an ignore): the task only raises an `:ignore-counts` budget when told to. If the
+ignore is genuinely required, run `./bin/mage fix-kondo-ratchets --seed :the-linter` and defend the increase
+in the PR. Set a linter's `:ignore-counts` value to `:unlimited` only when increasing its ignore count should
+not require a budget change.
The ignore must be the first key in its map; noncanonical forms fail the ratchet instead of being guessed
at. Ignores of linters outside the file's `:comment-exempt` set need an explanatory `;;` comment directly
diff --git a/bb.edn b/bb.edn
index 9fdf426a0eb..ce9eb4a4786 100644
--- a/bb.edn
+++ b/bb.edn
@@ -303,6 +303,12 @@
:requires [[dev.kondo-ratchet]]
:task (task! (dev.kondo-ratchet/fix! (:options parsed)))}
+ check-kondo-ratchets
+ {:doc "Fail when suppressions exceed a bounded budget or the ratchets file isn't normalized (babashka only, no JVM)."
+ :examples [["./bin/mage check-kondo-ratchets" "Exit 1 when a policy is exceeded, the file is missing, or its formatting is not normalized"]]
+ :requires [[dev.kondo-ratchet]]
+ :task (task! (dev.kondo-ratchet/check))}
+
kondo-redundant-ignores
{:doc "Report inline kondo ignores that are no longer necessary (slow: full kondo run)."
:examples [["./bin/mage kondo-redundant-ignores" "Scan the backend tree for removable ignores"]
diff --git a/dev/src/dev/kondo_ratchet.clj b/dev/src/dev/kondo_ratchet.clj
index 807ba2f1db3..202a458b604 100644
--- a/dev/src/dev/kondo_ratchet.clj
+++ b/dev/src/dev/kondo_ratchet.clj
@@ -1,16 +1,16 @@
(ns dev.kondo-ratchet
"Ratchet on inline kondo ignore forms.
- Per-linter budgets live in `.clj-kondo/ratchets.edn`, along with the set of linters whose ignores don't
- need a justification comment.
- `metabase.core.kondo-ratchet-test` fails when either drifts from the tree;
+ Per-linter policies live in `.clj-kondo/ratchets.edn`, with the linters whose ignores need no comment.
+ Local tests require an exact match; CI rejects only increases and lets the shrink workflow record the rest.
`./bin/mage fix-kondo-ratchets` lowers budgets and drops stale exemptions, never the reverse.
Loaded by both the bb task and the JVM test, so keep it dependency-free."
{:clj-kondo/config '{:linters {:discouraged-var {clojure.core/println {:level :off}}}}}
(:require
[clojure.edn :as edn]
[clojure.java.io :as io]
- [clojure.string :as str]))
+ [clojure.string :as str]
+ [clojure.walk :as walk]))
(set! *warn-on-reflection* true)
@@ -18,6 +18,25 @@
"The budgets file, relative to the repo root."
".clj-kondo/ratchets.edn")
+(defn- read-ratchets-form
+ "The one EDN map in `file`.
+ An empty file, a non-map, or a second form is an error rather than an empty policy set, so a damaged
+ file can never read as \"no budgets\"."
+ [^java.io.File file]
+ (with-open [reader (java.io.PushbackReader. (io/reader file))]
+ (let [eof (Object.)
+ form (edn/read {:eof eof} reader)]
+ (when (identical? eof form)
+ (throw (ex-info (str *ratchets-file* " is empty; expected one map of policies")
+ {:file *ratchets-file*})))
+ (when-not (map? form)
+ (throw (ex-info (str *ratchets-file* " must hold a map of policies, not " (pr-str form))
+ {:file *ratchets-file*, :form form})))
+ (when-not (identical? eof (edn/read {:eof eof} reader))
+ (throw (ex-info (str *ratchets-file* " holds more than one form; expected one map of policies")
+ {:file *ratchets-file*})))
+ form)))
+
(defn read-ratchets
"Parsed contents of [[*ratchets-file*]], with empty defaults for omitted keys.
Throws when the file is missing; only an explicit `{:disabled true}` opts out of enforcement."
@@ -26,8 +45,29 @@
(when-not (.exists file)
(throw (ex-info (str *ratchets-file* " is missing -- only {:disabled true} opts out of enforcement")
{:file *ratchets-file*})))
- (merge {:ignore-counts {}, :config-counts {}, :comment-exempt #{}}
- (edn/read-string (slurp file)))))
+ (let [ratchets (merge {:ignore-counts {}, :config-counts {}, :comment-exempt #{}}
+ (read-ratchets-form file))]
+ (when-not (map? (:ignore-counts ratchets))
+ (throw (ex-info ":ignore-counts must be a map of linter policies"
+ {:ignore-counts (:ignore-counts ratchets)})))
+ (doseq [[linter policy] (:ignore-counts ratchets)]
+ (when-not (or (= policy :unlimited)
+ (and (integer? policy) (not (neg? policy))))
+ (throw (ex-info (format "%s has invalid policy %s; expected a non-negative integer or :unlimited"
+ linter (pr-str policy))
+ {:linter linter, :policy policy}))))
+ (when-not (map? (:config-counts ratchets))
+ (throw (ex-info ":config-counts must be a map of linter budgets"
+ {:config-counts (:config-counts ratchets)})))
+ (doseq [[linter budget] (:config-counts ratchets)]
+ (when-not (and (integer? budget) (not (neg? budget)))
+ (throw (ex-info (format "%s has invalid config budget %s; expected a non-negative integer"
+ linter (pr-str budget))
+ {:linter linter, :budget budget}))))
+ (when-not (set? (:comment-exempt ratchets))
+ (throw (ex-info ":comment-exempt must be a set of linters"
+ {:comment-exempt (:comment-exempt ratchets)})))
+ ratchets)))
(defn disabled?
"Whether `ratchets` (default: [[read-ratchets]]) explicitly disables the ratchets."
@@ -36,6 +76,141 @@
([ratchets]
(true? (:disabled ratchets))))
+(def ^:private deps-file
+ "deps.edn")
+
+(def ^:private kondo-config-source
+ "Source file of clj-kondo's default config, on the classpath once the clj-kondo jar is."
+ "clj_kondo/impl/config.clj")
+
+(defn- pinned-kondo-version
+ "The clj-kondo version the `:kondo` alias in [[deps-file]] lints with."
+ []
+ (or (get-in (edn/read-string (slurp deps-file))
+ [:aliases :kondo :replace-deps 'clj-kondo/clj-kondo :mvn/version])
+ (throw (ex-info (str "no clj-kondo pin under the :kondo alias in " deps-file) {:file deps-file}))))
+
+(defn- kondo-config-resource
+ "URL of [[kondo-config-source]]. Throws when the clj-kondo jar is not on the classpath and cannot be added."
+ []
+ (or (io/resource kondo-config-source)
+ ;; the JVM `:dev` alias already has the jar; babashka adds the pinned version on demand, resolving
+ ;; through the same Maven cache the JVM uses
+ (when (System/getProperty "babashka.version")
+ ((requiring-resolve 'babashka.deps/add-deps)
+ {:deps {'clj-kondo/clj-kondo {:mvn/version (pinned-kondo-version)}}})
+ (io/resource kondo-config-source))
+ (throw (ex-info (str kondo-config-source " is not on the classpath; add the clj-kondo dependency")
+ {:resource kondo-config-source}))))
+
+(defn builtin-linters
+ "Names of every linter clj-kondo ships, read from the `default-config` literal in the pinned jar."
+ []
+ ;; reading the source rather than loading the namespace keeps this babashka-compatible
+ (binding [*read-eval* false]
+ (with-open [r (java.io.PushbackReader. (io/reader (kondo-config-resource)))]
+ (loop []
+ (let [form (read {:eof ::eof} r)]
+ (cond
+ (= form ::eof)
+ (throw (ex-info (str "default-config not found in " kondo-config-source) {}))
+
+ (and (seq? form) (= 'def (first form)) (= 'default-config (second form)))
+ ;; the value is a quoted literal, so the def reads as (def default-config (quote {...}))
+ (set (keys (:linters (second (nth form 2)))))
+
+ :else
+ (recur)))))))
+
+(def ^:private kondo-config-dir
+ ".clj-kondo")
+
+(defn- tracked-files
+ "The git-tracked files under `dir` that are present on disk.
+ A file deleted but not yet staged, or outside a sparse checkout, is skipped."
+ [dir]
+ ;; only tracked files keep validation the same everywhere: the dependency configs `mage kondo` copies
+ ;; in are gitignored, and a clean checkout has none of them
+ (let [^java.util.List command ["git" "ls-files" "-z"]
+ process (.start (doto (ProcessBuilder. command)
+ (.directory ^java.io.File (io/file dir))
+ (.redirectErrorStream true)))
+ out (slurp (.getInputStream process))]
+ (when-not (zero? (.waitFor process))
+ (throw (ex-info (str "git ls-files failed in " dir ": " (str/trim out)) {:dir dir})))
+ (->> (str/split out #"\x00")
+ (remove str/blank?)
+ (map #(io/file dir %))
+ (filter #(.isFile ^java.io.File %)))))
+
+(defn- linters-map-keys
+ "Keys of every `:linters` map nested anywhere in `config`, so scoped linters under `:config-in-ns`,
+ `:config-in-call`, and library configs count too."
+ [config]
+ (let [found (atom #{})]
+ (walk/prewalk (fn [x]
+ (when (and (map? x) (map? (:linters x)))
+ (swap! found into (keys (:linters x))))
+ x)
+ config)
+ @found))
+
+(defn repository-linters
+ "Names of every linter configured in a tracked `.edn` file under `dir` (default: `.clj-kondo`), which is
+ how the repository's own hook linters get a level."
+ ([]
+ (repository-linters kondo-config-dir))
+ ([dir]
+ (into #{}
+ (comp (filter (fn [^java.io.File f]
+ (str/ends-with? (.getName f) ".edn")))
+ (mapcat (fn [^java.io.File f]
+ (linters-map-keys (edn/read-string (slurp f))))))
+ (tracked-files dir))))
+
+(def external-linters
+ "Diagnostics from tools other than kondo that ignores still name, plus `:all` for the vector-less form."
+ #{:all :clojure-lsp/unused-public-var})
+
+(defn known-linters
+ "Every linter name a policy may use: kondo's built-ins, the repository's own, and [[external-linters]]."
+ []
+ (into (builtin-linters) cat [(repository-linters) external-linters]))
+
+(defn unknown-linters
+ "Policy keys in `ratchets` that are not in `known`, sorted."
+ [{:keys [ignore-counts config-counts comment-exempt]} known]
+ (into (sorted-set-by #(compare (str %1) (str %2)))
+ (remove known)
+ (concat (keys ignore-counts) (keys config-counts) comment-exempt)))
+
+(defn- known-linter-hint []
+ (format "policies must name a kondo built-in, a linter configured under %s, or one of %s"
+ kondo-config-dir
+ (str/join ", " (sort-by str external-linters))))
+
+(defn validate-linters!
+ "Throw one error naming every policy key in `ratchets` that is not a known linter."
+ [ratchets known]
+ (let [unknown (unknown-linters ratchets known)]
+ (when (seq unknown)
+ (throw (ex-info (format "%s names %d unknown linter%s: %s -- %s"
+ *ratchets-file*
+ (count unknown)
+ (if (= 1 (count unknown)) "" "s")
+ (str/join ", " unknown)
+ (known-linter-hint))
+ {:unknown (vec unknown)})))))
+
+(defn validate-seed!
+ "Throw when a linter in `seeded` is not known, so `--seed` never writes a policy the check rejects."
+ [seeded known]
+ (when-let [unknown (seq (remove known seeded))]
+ (throw (ex-info (format "cannot seed %s: not a known linter -- %s"
+ (str/join ", " unknown)
+ (known-linter-hint))
+ {:unknown (vec unknown)}))))
+
(def ^:private source-roots
["src" "test" "enterprise" "modules/drivers" "dev" "bin" "mage"])
@@ -255,7 +430,8 @@
(for [linter (into (set (keys actual)) (keys recorded))
:let [budget (get recorded linter 0)
n (get actual linter 0)]
- :when (not= budget n)]
+ :when (and (not= budget :unlimited)
+ (not= budget n))]
[linter (cond-> {:recorded budget, :actual n}
(> n budget)
(assoc :examples (->> occurrences
@@ -264,6 +440,14 @@
(take 5)
vec)))]))))
+(defn over-budget
+ "Linters whose actual count exceeds their bounded policy."
+ [policies occurrences]
+ (sorted-by-str
+ (for [[linter {:keys [recorded actual] :as entry}] (drift policies occurrences)
+ :when (> actual recorded)]
+ [linter entry])))
+
(defn unjustified
"Occurrences that need a justification comment but lack one, and suppress at least one linter outside
the `exempt` set."
@@ -338,11 +522,20 @@
:when (not= budget n)]
[linter {:recorded budget, :actual n}])))
+(defn config-over-budget
+ "Linters whose config-suppression count exceeds its budget."
+ [budgets counts]
+ (sorted-by-str
+ (for [[linter {:keys [recorded actual] :as entry}] (config-drift budgets counts)
+ :when (> actual recorded)]
+ [linter entry])))
+
(def ^:private header
(str ";; Budgets for kondo suppressions: inline `" ignore-marker "` forms per linter (:ignore-counts),\n"
";; and config-level waivers in .clj-kondo/config.edn (:config-counts -- :off switches and :exclude\n"
- ";; entries). metabase.core.kondo-ratchet-test fails when either drifts from reality, or when an\n"
- ";; ignore outside :comment-exempt lacks an explanatory comment directly above or trailing on its line.\n"
+ ";; entries). Each :ignore-counts value is a non-negative integer budget, or :unlimited for no ceiling.\n"
+ ";; CI rejects counts above a numeric budget; local tests require those counts to match exactly.\n"
+ ";; Any ignore outside :comment-exempt needs an explanatory comment directly above or trailing on its line.\n"
";; `./bin/mage fix-kondo-ratchets` lowers budgets and drops stale exemptions; local test runs do it\n"
";; automatically. Raising a budget, adding one (`--seed` for inline, by hand for config), or\n"
";; widening the exemptions is a hand edit to defend in your PR.\n"
@@ -357,7 +550,7 @@
(str "{"
(str/join (str "\n" indent)
(for [[linter n] entries]
- (format (str "%-" width "s %d") (str linter) n)))
+ (format (str "%-" width "s %s") (str linter) (str n))))
"}"))))
(defn render
@@ -379,42 +572,68 @@
"}\n")))
(defn lowered-counts
- "`recorded` with each budget lowered to its actual count; entries with no ignores left are dropped.
- Linters in `seeded` get their budget set to the actual count outright — the explicit escape hatch for
- landing a new linter. Otherwise never raises a budget, never adds one."
+ "`recorded` with each bounded budget lowered to its actual count; bounded entries with no ignores go.
+ An `:unlimited` policy is kept as written, even at zero: it records a decision about the linter, not a count.
+ Linters in `seeded` get their budget set outright — the explicit escape hatch for landing a new linter.
+ Otherwise never raises a budget, never adds one."
[recorded actual seeded]
- (into (sorted-by-str
- (for [linter seeded
- :when (pos? (get actual linter 0))]
- [linter (get actual linter)]))
- (keep (fn [[linter budget]]
- (let [n (get actual linter 0)]
- (cond
- (contains? (set seeded) linter) nil
- (zero? n) nil
- (< n budget) [linter n]
- :else [linter budget]))))
- recorded))
+ (let [seeded? (set seeded)]
+ (into (sorted-by-str
+ (for [linter seeded
+ :when (pos? (get actual linter 0))]
+ [linter (get actual linter)]))
+ (keep (fn [[linter budget]]
+ (let [n (get actual linter 0)]
+ (cond
+ (seeded? linter) nil
+ (= budget :unlimited) [linter budget]
+ (zero? n) nil
+ (< n budget) [linter n]
+ :else [linter budget]))))
+ recorded)))
+
+(defn unexercised-unlimited
+ "Linters with an `:unlimited` policy in `ignore-counts` and no ignores in `actual`, sorted."
+ [ignore-counts actual]
+ (into (sorted-set-by #(compare (str %1) (str %2)))
+ (for [[linter policy] ignore-counts
+ :when (and (= policy :unlimited)
+ (zero? (get actual linter 0)))]
+ linter)))
+
+(defn unexercised-unlimited-warning
+ "One informational line naming the [[unexercised-unlimited]] linters, or nil when there are none.
+ The policies stay in place; the line only makes a stale one visible."
+ [ignore-counts actual]
+ (let [linters (unexercised-unlimited ignore-counts actual)]
+ (when (seq linters)
+ (str "WARNING: :unlimited policies with no ignores left: " (str/join ", " linters)
+ " -- delete an entry by hand once its linter no longer needs one"))))
(defn change-report
"The lines [[fix!]] prints: lowered/dropped/seeded budgets, dropped exemptions, plus warnings for
- anything over budget."
+ anything over budget and for `:unlimited` policies nothing uses any more."
[{:keys [ignore-counts config-counts comment-exempt]} occurrences config-actual seeded]
(let [actual (actual-counts occurrences)]
(concat
(for [linter seeded
:let [n (get actual linter 0)]]
- (if (pos? n)
- (format "seeded %s at %d" linter n)
- (format "WARNING: %s has no inline ignores -- nothing to seed" linter)))
+ (cond
+ (pos? n) (format "seeded %s at %d" linter n)
+ (contains? ignore-counts linter) (format "WARNING: %s has no inline ignores -- dropping its policy"
+ linter)
+ :else (format "WARNING: %s has no inline ignores -- nothing to seed"
+ linter)))
(for [[linter budget] (sort-by (comp str first) (apply dissoc ignore-counts seeded))
:let [n (get actual linter 0)]
- :when (not= n budget)]
+ ;; a hand-written 0 with no ignores is dropped too, so it needs a line
+ :when (and (integer? budget) (or (not= n budget) (zero? budget)))]
(cond
(zero? n) (format "dropped %s (no ignores left)" linter)
(< n budget) (format "lowered %s %d -> %d" linter budget n)
:else (format "WARNING: %s is over budget (%d recorded, %d actual) -- remove ignores, or accept them all with `--seed %s`"
linter budget n linter)))
+ (some-> (unexercised-unlimited-warning (apply dissoc ignore-counts seeded) actual) vector)
(for [[linter n] (sort-by (comp str first) (apply dissoc actual (concat seeded (keys ignore-counts))))]
(format "WARNING: %s has %d ignores but no budget entry -- seed one with `./bin/mage fix-kondo-ratchets --seed %s`"
linter n linter))
@@ -429,7 +648,8 @@
(defn fix!
"Rewrite [[*ratchets-file*]]: lower budgets, drop stale comment exemptions, normalize formatting.
- `--seed LINTER` (`{:seed \"...\"}` here) sets that budget to the actual count, adding or raising it.
+ Refuses to touch a file naming an unknown linter, whether seeded or recorded, and never removes one.
+ `--seed LINTER` (`{:seed \"...\"}` here) sets that budget to the actual count and bounds an unlimited one.
Prints the [[change-report]], or `unchanged` on a no-op.
Does nothing, including seeding, when the file sets `:disabled` to `true`."
([]
@@ -438,8 +658,11 @@
(let [{:keys [ignore-counts config-counts comment-exempt] :as ratchets} (read-ratchets)]
(if (disabled? ratchets)
(println (str *ratchets-file* " is disabled -- nothing to do"))
- (let [occurrences (scan)
+ (let [known (known-linters)
+ _ (validate-linters! ratchets known)
seeded (if seed [(keyword (str/replace-first seed #"^:" ""))] [])
+ _ (validate-seed! seeded known)
+ occurrences (scan)
actual (actual-counts occurrences)
config-actual (config-suppressions)
text (render {:ignore-counts (lowered-counts ignore-counts actual seeded)
@@ -451,3 +674,67 @@
(println "unchanged")
(do (spit *ratchets-file* text)
(println (str "wrote " *ratchets-file*)))))))))
+
+(defn check-report
+ "The lines [[check]] prints when inline ignores or config suppressions (`config-actual`) exceed their
+ budgets, or `text` is not normalized.
+ Lower counts are allowed because the shrink workflow records them after the change lands."
+ [{:keys [ignore-counts config-counts] :as ratchets} occurrences config-actual text]
+ (let [over (over-budget ignore-counts occurrences)
+ config-over (config-over-budget config-counts config-actual)
+ linter-line (fn [[linter {:keys [recorded actual]}]]
+ (format " %s: %d recorded, %d actual" linter recorded actual))]
+ (concat
+ (when (seq over)
+ (cons (str "over budget -- remove an ignore, or seed the budget with"
+ " `./bin/mage fix-kondo-ratchets --seed <linter>` and defend it in the PR:")
+ (mapcat (fn [[_ {:keys [examples]} :as entry]]
+ (cons (linter-line entry) (map #(str " " %) examples)))
+ over)))
+ (when (seq config-over)
+ (cons (str "config suppressions over budget -- remove the entry from " kondo-config-file
+ ", or raise the budget by hand and defend it in the PR:")
+ (map linter-line config-over)))
+ (when (not= text (render ratchets))
+ [(str *ratchets-file* " is not normalized -- run `./bin/mage fix-kondo-ratchets`"
+ " to fix the formatting")]))))
+
+(defn- exit!
+ "Fail the babashka task with `message`, without mage's default stack trace."
+ [message]
+ (throw (ex-info message {:babashka/exit 1, :mage/quiet true})))
+
+(defn- fail!
+ "[[exit!]], for a `message` the run has not already printed."
+ [message]
+ (println message)
+ (exit! message))
+
+(defn check
+ "Fail the babashka task when the ratchets file is missing, a policy names an unknown linter, inline
+ ignores or config suppressions exceed a bounded budget, or the file is not normalized.
+ Only an explicit `{:disabled true}` opts out of enforcement.
+ An unused `:unlimited` policy is reported but does not fail the check."
+ []
+ (if-not (.exists (io/file *ratchets-file*))
+ (fail! (str *ratchets-file* " is missing -- only {:disabled true} opts out of enforcement"))
+ ;; read-ratchets, validate-linters! and scan all throw on malformed input; without this they reach
+ ;; the user as a babashka stack dump.
+ (try
+ (let [ratchets (read-ratchets)]
+ (if (disabled? ratchets)
+ (println (str *ratchets-file* " is disabled -- nothing to check"))
+ (do
+ (validate-linters! ratchets (known-linters))
+ (let [occurrences (scan)
+ lines (check-report ratchets occurrences (config-suppressions) (slurp *ratchets-file*))]
+ (some-> (unexercised-unlimited-warning (:ignore-counts ratchets) (actual-counts occurrences)) println)
+ (if (empty? lines)
+ (println (format "ok -- %d ignore forms within %d policies"
+ (count occurrences) (count (:ignore-counts ratchets))))
+ (do (run! println lines)
+ (exit! (str *ratchets-file* " drifted from the source tree"))))))))
+ (catch clojure.lang.ExceptionInfo e
+ (if (:babashka/exit (ex-data e))
+ (throw e)
+ (fail! (ex-message e)))))))
diff --git a/dev/test/metabase/core/kondo_ratchet_check_test.clj b/dev/test/metabase/core/kondo_ratchet_check_test.clj
new file mode 100644
index 00000000000..697d3092920
--- /dev/null
+++ b/dev/test/metabase/core/kondo_ratchet_check_test.clj
@@ -0,0 +1,155 @@
+(ns metabase.core.kondo-ratchet-check-test
+ "The Babashka check on the ignore budgets."
+ (:require
+ [clojure.java.io :as io]
+ [clojure.string :as str]
+ [clojure.test :refer :all]
+ [dev.kondo-ratchet :as kondo-ratchet]))
+
+(set! *warn-on-reflection* true)
+
+(defn- occurrences
+ "One single-linter ignore form per unit in `linter->count`, all in `f.clj` on successive lines."
+ [linter->count]
+ (for [[linter n] linter->count
+ i (range n)]
+ {:file "f.clj", :line (inc i), :linters [linter]}))
+
+(defn- report-lines
+ ([ratchets occurrences text]
+ (report-lines ratchets occurrences {} text))
+ ([ratchets occurrences config-actual text]
+ (vec (kondo-ratchet/check-report ratchets occurrences config-actual text))))
+
+(deftest ^:parallel clean-test
+ (let [ratchets {:ignore-counts {:a 2, :b 1}}]
+ (is (= []
+ (report-lines ratchets (occurrences {:a 2, :b 1}) (kondo-ratchet/render ratchets))))))
+
+(deftest ^:parallel over-budget-test
+ (let [ratchets {:ignore-counts {:a 1}}]
+ (is (= ["over budget -- remove an ignore, or seed the budget with `./bin/mage fix-kondo-ratchets --seed <linter>` and defend it in the PR:"
+ " :a: 1 recorded, 3 actual"
+ " f.clj:1"
+ " f.clj:2"
+ " f.clj:3"
+ " :new: 0 recorded, 1 actual"
+ " f.clj:1"]
+ (report-lines ratchets (occurrences {:a 3, :new 1}) (kondo-ratchet/render ratchets)))
+ "an unbudgeted linter counts as over a budget of 0")))
+
+(deftest ^:parallel unlimited-test
+ (let [ratchets {:ignore-counts {:bounded 1, :free :unlimited, :empty :unlimited}}]
+ (is (= []
+ (report-lines ratchets
+ (occurrences {:bounded 1, :free 2})
+ (kondo-ratchet/render ratchets)))
+ "unlimited linters do not fail the CI report, even when their actual count reaches zero")))
+
+(deftest ^:parallel config-over-budget-test
+ (let [ratchets {:ignore-counts {}, :config-counts {:a 1, :b 2}}]
+ (is (= ["config suppressions over budget -- remove the entry from .clj-kondo/config.edn, or raise the budget by hand and defend it in the PR:"
+ " :a: 1 recorded, 2 actual"
+ " :new: 0 recorded, 1 actual"]
+ (report-lines ratchets [] {:a 2, :b 1, :new 1} (kondo-ratchet/render ratchets)))
+ "a lowered config count passes; growth and new entries are reported")))
+
+(defn- check-with!
+ "Output lines of [[kondo-ratchet/check]] against `ratchets` written to a temp file, with `occurrences`
+ standing in for the tree scan; `:thrown?` says whether it failed."
+ [ratchets occurrences]
+ (let [dir (.toFile (java.nio.file.Files/createTempDirectory
+ "kondo-ratchet-check-test"
+ (make-array java.nio.file.attribute.FileAttribute 0)))
+ budgets (doto (io/file dir "ratchets.edn")
+ (spit (kondo-ratchet/render (merge {:config-counts {}, :comment-exempt #{}} ratchets))))
+ thrown? (atom false)]
+ (binding [kondo-ratchet/*ratchets-file* (.getPath budgets)]
+ (with-redefs [kondo-ratchet/known-linters (constantly (set (keys (:ignore-counts ratchets))))
+ kondo-ratchet/config-suppressions (constantly {})
+ kondo-ratchet/scan (constantly occurrences)]
+ {:lines (str/split-lines
+ (with-out-str
+ (try
+ (kondo-ratchet/check)
+ (catch clojure.lang.ExceptionInfo _
+ (reset! thrown? true)))))
+ :thrown? @thrown?}))))
+
+(deftest check-reports-empty-unlimited-test
+ (let [ratchets {:ignore-counts {:z-empty :unlimited, :a-empty :unlimited, :free :unlimited, :over 1}}]
+ (is (= {:lines ["WARNING: :unlimited policies with no ignores left: :a-empty, :z-empty -- delete an entry by hand once its linter no longer needs one"
+ "ok -- 2 ignore forms within 4 policies"]
+ :thrown? false}
+ (check-with! ratchets (occurrences {:free 1, :over 1})))
+ "the warning comes first, sorted, and does not fail the check")
+ (is (= {:lines ["WARNING: :unlimited policies with no ignores left: :a-empty, :z-empty -- delete an entry by hand once its linter no longer needs one"
+ "over budget -- remove an ignore, or seed the budget with `./bin/mage fix-kondo-ratchets --seed <linter>` and defend it in the PR:"
+ " :over: 1 recorded, 2 actual"
+ " f.clj:1"
+ " f.clj:2"]
+ :thrown? true}
+ (check-with! ratchets (occurrences {:free 1, :over 2})))
+ "a real failure still fails, after the warning")
+ (is (= {:lines ["ok -- 4 ignore forms within 4 policies"]
+ :thrown? false}
+ (check-with! ratchets (occurrences {:z-empty 1, :a-empty 1, :free 1, :over 1})))
+ "no warning when every unlimited policy is in use")))
+
+(deftest ^:parallel stale-test
+ (let [ratchets {:ignore-counts {:a 5, :gone 2}}]
+ (is (= []
+ (report-lines ratchets (occurrences {:a 3}) (kondo-ratchet/render ratchets))))))
+
+(deftest ^:parallel not-normalized-test
+ (let [ratchets {:ignore-counts {:a 1}}
+ text (kondo-ratchet/render ratchets)]
+ (is (= [(str kondo-ratchet/*ratchets-file* " is not normalized -- run `./bin/mage fix-kondo-ratchets`"
+ " to fix the formatting")]
+ (report-lines ratchets (occurrences {:a 1}) (str/replace text "{:a 1}" "{:a 1}")))
+ "same data, different whitespace")))
+
+(deftest ^:parallel combined-test
+ (testing "over-budget and formatting problems are reported together"
+ (let [ratchets {:ignore-counts {:over 1, :stale 2}}]
+ (is (= ["over budget -- remove an ignore, or seed the budget with `./bin/mage fix-kondo-ratchets --seed <linter>` and defend it in the PR:"
+ " :over: 1 recorded, 2 actual"
+ " f.clj:1"
+ " f.clj:2"
+ (str kondo-ratchet/*ratchets-file* " is not normalized -- run `./bin/mage fix-kondo-ratchets`"
+ " to fix the formatting")]
+ (report-lines ratchets (occurrences {:over 2, :stale 1}) "{:ignore-counts {}}\n"))))))
+
+(deftest check-disabled-test
+ (let [dir (.toFile (java.nio.file.Files/createTempDirectory
+ "kondo-ratchet-check-test"
+ (make-array java.nio.file.attribute.FileAttribute 0)))
+ budgets (doto (io/file dir "ratchets.edn") (spit "{:disabled true}\n"))]
+ (binding [kondo-ratchet/*ratchets-file* (.getPath budgets)]
+ (is (= (str (.getPath budgets) " is disabled -- nothing to check\n")
+ (with-out-str (kondo-ratchet/check)))))))
+
+(deftest check-unknown-linter-test
+ (let [dir (.toFile (java.nio.file.Files/createTempDirectory
+ "kondo-ratchet-check-test"
+ (make-array java.nio.file.attribute.FileAttribute 0)))
+ budgets (doto (io/file dir "ratchets.edn")
+ (spit (kondo-ratchet/render {:ignore-counts {:a 1, :bogus 1}
+ :config-counts {}
+ :comment-exempt #{}})))]
+ (binding [kondo-ratchet/*ratchets-file* (.getPath budgets)]
+ (with-redefs [kondo-ratchet/known-linters (constantly #{:a})]
+ (let [out (with-out-str
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"names 1 unknown linter: :bogus"
+ (kondo-ratchet/check))))]
+ (is (str/includes? out (str (.getPath budgets) " names 1 unknown linter: :bogus -- policies must name"))
+ "the message is printed for the task output, then the task exits nonzero"))))))
+
+(deftest check-missing-file-test
+ (binding [kondo-ratchet/*ratchets-file* "target/does-not-exist/ratchets.edn"]
+ (is (str/includes?
+ (with-out-str
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo
+ #"only \{:disabled true\} opts out"
+ (kondo-ratchet/check))))
+ "is missing -- only {:disabled true} opts out of enforcement"))))
diff --git a/dev/test/metabase/core/kondo_ratchet_test.clj b/dev/test/metabase/core/kondo_ratchet_test.clj
index 9c05d904520..c213f22c92e 100644
--- a/dev/test/metabase/core/kondo_ratchet_test.clj
+++ b/dev/test/metabase/core/kondo_ratchet_test.clj
@@ -5,6 +5,7 @@
(:require
[clojure.edn :as edn]
[clojure.java.io :as io]
+ [clojure.java.shell :as sh]
[clojure.string :as str]
[clojure.test :refer :all]
[dev.kondo-ratchet :as kondo-ratchet]))
@@ -25,10 +26,34 @@
(defn- ratchets-enabled? []
(not (kondo-ratchet/disabled?)))
+;; Local tooling sets CI=false and CI=, and both are truthy when tested for presence.
+(defn- ci? []
+ (= "true" (System/getenv "CI")))
+
+(defn- budget-drift
+ [ci? policies occurrences]
+ (if ci?
+ (kondo-ratchet/over-budget policies occurrences)
+ (kondo-ratchet/drift policies occurrences)))
+
+(defn- config-budget-drift
+ [ci? budgets counts]
+ (if ci?
+ (kondo-ratchet/config-over-budget budgets counts)
+ (kondo-ratchet/config-drift budgets counts)))
+
+;; Stale exemptions fail local runs only; the shrink workflow drops them, so CI must not fail when
+;; another PR has already justified the last ignore.
+(defn- stale-exemptions
+ [ci? exempt occurrences]
+ (if ci?
+ #{}
+ (kondo-ratchet/stale-exemptions exempt occurrences)))
+
;; Outside CI, tighten the ratchets before asserting — the fix rides along in your next commit.
-;; The self-heal workflow does the same for labelled PRs.
+;; The shrink workflow performs the same update on master.
(use-fixtures :once (fn [thunk]
- (when-not (System/getenv "CI")
+ (when-not (ci?)
(kondo-ratchet/fix!))
(reset! tree-scan-cache (delay (kondo-ratchet/scan)))
(try
@@ -42,15 +67,15 @@
(deftest ^:parallel budgets-match-actual-counts-test
(when (ratchets-enabled?)
- (testing (str "\nBudgets in " kondo-ratchet/*ratchets-file* " must match the actual inline ignore counts.\n"
+ (testing (str "\nBudgets in " kondo-ratchet/*ratchets-file* " must match the actual inline ignore counts in development.\n"
"Budget too low: remove an ignore, or seed the budget with\n"
"`./bin/mage fix-kondo-ratchets --seed <linter>` and defend it in the PR.\n"
- "Budget too high: run `./bin/mage fix-kondo-ratchets`, or label the PR\n"
- "kondo-ratchets-self-healing and CI commits the fix to your branch. Too high in a local\n"
- "run means `fix!` itself is broken, since the test fixture just ran it.")
- (is (= {}
- (kondo-ratchet/drift (:ignore-counts (kondo-ratchet/read-ratchets))
- (tree-scan)))))))
+ "Budget too high: run `./bin/mage fix-kondo-ratchets`; the shrink workflow records\n"
+ "lower counts after the change lands. Too high in a local run means\n"
+ "`fix!` itself is broken, since the test fixture just ran it.")
+ (let [{:keys [ignore-counts]} (kondo-ratchet/read-ratchets)]
+ (is (= {}
+ (budget-drift (ci?) ignore-counts (tree-scan))))))))
(deftest ^:parallel ignores-are-justified-test
(when (ratchets-enabled?)
@@ -66,11 +91,10 @@
(deftest ^:parallel no-stale-exemptions-test
(when (ratchets-enabled?)
(testing (str "\nEvery linter in :comment-exempt still has at least one unjustified ignore; once the last\n"
- "one gains a comment, the exemption goes. Run `./bin/mage fix-kondo-ratchets`, or label\n"
- "the PR kondo-ratchets-self-healing.")
+ "one gains a comment, the exemption goes. Run `./bin/mage fix-kondo-ratchets`.")
(let [{:keys [comment-exempt]} (kondo-ratchet/read-ratchets)]
(is (= #{}
- (kondo-ratchet/stale-exemptions comment-exempt (tree-scan))))))))
+ (stale-exemptions (ci?) comment-exempt (tree-scan))))))))
(deftest ^:parallel config-budgets-match-actual-test
(when (ratchets-enabled?)
@@ -79,8 +103,24 @@
"Budget too low: remove the new config suppression, or raise the budget by hand and\n"
"defend it in the PR. Budget too high: run `./bin/mage fix-kondo-ratchets`.")
(is (= {}
- (kondo-ratchet/config-drift (:config-counts (kondo-ratchet/read-ratchets))
- (kondo-ratchet/config-suppressions)))))))
+ (config-budget-drift (ci?)
+ (:config-counts (kondo-ratchet/read-ratchets))
+ (kondo-ratchet/config-suppressions)))))))
+
+(deftest ^:parallel ci-tolerates-reductions-test
+ (let [occurrences [{:file "f.clj", :line 1, :linters [:a], :justified? false}
+ {:file "g.clj", :line 1, :linters [:b], :justified? true}]]
+ (testing "inline budgets"
+ (is (= {} (budget-drift true {:a 3, :b 1} occurrences)))
+ (is (= {:a {:recorded 3, :actual 1}} (budget-drift false {:a 3, :b 1} occurrences)))
+ (is (= {:a {:recorded 0, :actual 1, :examples ["f.clj:1"]}} (budget-drift true {:b 1} occurrences))))
+ (testing "config budgets"
+ (is (= {} (config-budget-drift true {:cfg 3} {:cfg 1})))
+ (is (= {:cfg {:recorded 3, :actual 1}} (config-budget-drift false {:cfg 3} {:cfg 1})))
+ (is (= {:cfg {:recorded 1, :actual 2}} (config-budget-drift true {:cfg 1} {:cfg 2}))))
+ (testing "stale exemptions"
+ (is (= #{} (stale-exemptions true #{:a :b} occurrences)))
+ (is (= #{:b} (stale-exemptions false #{:a :b} occurrences))))))
(deftest ^:parallel ratchets-file-normalized-test
(when (ratchets-enabled?)
@@ -90,6 +130,14 @@
(is (= (kondo-ratchet/render (kondo-ratchet/read-ratchets))
(slurp kondo-ratchet/*ratchets-file*))))))
+(deftest ^:parallel policies-name-known-linters-test
+ (when (ratchets-enabled?)
+ (testing (str "\nEvery policy key in " kondo-ratchet/*ratchets-file* " must name a linter: a clj-kondo built-in\n"
+ "in the pinned version, a linter configured under .clj-kondo/, or an external diagnostic\n"
+ "such as :clojure-lsp/unused-public-var. Unknown names are never removed automatically.")
+ (is (= #{}
+ (kondo-ratchet/unknown-linters (kondo-ratchet/read-ratchets) (kondo-ratchet/known-linters)))))))
+
;;;; ---------------------------------------------------------------------------
;;;; Scanner unit tests
;;;; ---------------------------------------------------------------------------
@@ -231,13 +279,18 @@
(deftest ^:parallel render-test
(testing "keys come out sorted, values aligned, and the text round-trips losslessly"
- (let [ratchets {:ignore-counts {:discouraged-var 3, :all 1, :metabase/modules 2}
- :config-counts {:unresolved-symbol 18, :inline-def 1}
+ (let [ratchets {:ignore-counts {:all 1
+ :discouraged-var 3
+ :metabase/modules 2
+ :unused-alias :unlimited}
+ :config-counts {:inline-def 1
+ :unresolved-symbol 18}
:comment-exempt #{:metabase/modules :discouraged-var}}
text (kondo-ratchet/render ratchets)]
(is (str/ends-with? text (str "{:ignore-counts {:all 1\n"
" :discouraged-var 3\n"
- " :metabase/modules 2}\n"
+ " :metabase/modules 2\n"
+ " :unused-alias :unlimited}\n"
" :config-counts {:inline-def 1\n"
" :unresolved-symbol 18}\n"
" :comment-exempt #{:discouraged-var\n"
@@ -248,13 +301,51 @@
(is (str/ends-with? (kondo-ratchet/render {:ignore-counts {}, :config-counts {}, :comment-exempt #{}})
"{:ignore-counts {}\n :config-counts {}\n :comment-exempt #{}}\n"))))
+(deftest read-ratchets-policy-values-test
+ (let [file (doto (java.io.File/createTempFile "kondo-ratchets" ".edn")
+ (spit (pr-str {:ignore-counts {:bounded 4, :free :unlimited}})))]
+ (binding [kondo-ratchet/*ratchets-file* (.getPath file)]
+ (is (= {:ignore-counts {:bounded 4, :free :unlimited}
+ :config-counts {}
+ :comment-exempt #{}}
+ (kondo-ratchet/read-ratchets))))))
+
+(deftest read-ratchets-validates-policies-test
+ (doseq [[value message] [[-1 #"non-negative integer or :unlimited"]
+ [:other #"non-negative integer or :unlimited"]
+ ["1" #"non-negative integer or :unlimited"]]]
+ (let [file (doto (java.io.File/createTempFile "kondo-ratchets" ".edn")
+ (spit (pr-str {:ignore-counts {:a value}})))]
+ (binding [kondo-ratchet/*ratchets-file* (.getPath file)]
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo message
+ (kondo-ratchet/read-ratchets)))))))
+
+(deftest read-ratchets-validates-config-counts-test
+ (doseq [value [-1 :unlimited "1"]]
+ (let [file (doto (java.io.File/createTempFile "kondo-ratchets" ".edn")
+ (spit (pr-str {:config-counts {:a value}})))]
+ (binding [kondo-ratchet/*ratchets-file* (.getPath file)]
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo #"expected a non-negative integer"
+ (kondo-ratchet/read-ratchets))
+ (str "a config budget is always a plain count, so " (pr-str value) " is rejected"))))))
+
(deftest ^:parallel lowered-counts-test
- (is (= {:lower 3, :over-budget 5}
- (kondo-ratchet/lowered-counts {:lower 5, :over-budget 5, :gone 5}
- {:lower 3, :over-budget 7, :new-linter 9}
+ (is (= {:empty-unbounded :unlimited
+ :lower 3
+ :over-budget 5
+ :unbounded :unlimited}
+ (kondo-ratchet/lowered-counts {:empty-unbounded :unlimited
+ :gone 5
+ :lower 5
+ :over-budget 5
+ :unbounded :unlimited}
+ {:lower 3
+ :new-linter 9
+ :over-budget 7
+ :unbounded 4}
[]))
"budgets only ever move down: :lower shrinks to actual, :over-budget stays (the test's business),
- :gone is dropped, :new-linter is not added")
+ :gone is dropped at zero but an unlimited policy is kept, :new-linter is not added")
(testing "seeding is the escape hatch: sets the budget to actual, adding or raising"
(is (= {:new-linter 9, :raised 7}
(kondo-ratchet/lowered-counts {:raised 5}
@@ -262,7 +353,20 @@
[:new-linter :raised])))
(is (= {}
(kondo-ratchet/lowered-counts {} {} [:nothing-to-seed]))
- "seeding a linter with no ignores adds nothing")))
+ "seeding a linter with no ignores adds nothing")
+ (is (= {}
+ (kondo-ratchet/lowered-counts {:empty :unlimited} {} [:empty]))
+ "seeding an unlimited linter with no ignores converts it to a count, which is zero, so it goes")))
+
+(deftest ^:parallel unexercised-unlimited-test
+ (let [ignore-counts {:z-empty :unlimited, :a-empty :unlimited, :used :unlimited, :bounded-empty 3}]
+ (is (= #{:a-empty :z-empty}
+ (kondo-ratchet/unexercised-unlimited ignore-counts {:used 2}))
+ "only unlimited policies at zero, in name order; a bounded zero is the fixer's business")
+ (is (= "WARNING: :unlimited policies with no ignores left: :a-empty, :z-empty -- delete an entry by hand once its linter no longer needs one"
+ (kondo-ratchet/unexercised-unlimited-warning ignore-counts {:used 2})))
+ (is (nil? (kondo-ratchet/unexercised-unlimited-warning ignore-counts {:used 2, :a-empty 1, :z-empty 1}))
+ "no line when every unlimited policy is in use")))
(deftest ^:parallel drift-test
(let [occurrences [{:file "f.clj", :line 1, :linters [:a]}
@@ -276,12 +380,26 @@
":a and :b are over budget (with examples); :c and :gone are stale (without)"))
(testing "a matching budget doesn't appear"
(is (= {} (kondo-ratchet/drift {:a 1} [{:file "f.clj", :line 1, :linters [:a]}]))))
+ (testing "unlimited policies never drift, including when their actual count reaches zero"
+ (is (= {}
+ (kondo-ratchet/drift {:free :unlimited, :empty :unlimited}
+ [{:file "f.clj", :line 1, :linters [:free]}]))))
(testing "examples are capped at 5"
(let [occurrences (for [line (range 1 10)]
{:file "f.clj", :line line, :linters [:a]})]
(is (= 5 (count (:examples (:a (kondo-ratchet/drift {} occurrences)))))))))
-(deftest ^:parallel fix-when-disabled-test
+(deftest ^:parallel budget-drift-test
+ (let [policies {:bounded 2, :free :unlimited, :empty :unlimited}
+ occurrences [{:file "f.clj", :line 1, :linters [:bounded :free]}]]
+ (is (= {:bounded {:recorded 2, :actual 1}}
+ (budget-drift nil policies occurrences))
+ "local checks require bounded counts to match exactly, but ignore unlimited linters")
+ (is (= {}
+ (budget-drift "true" policies occurrences))
+ "CI allows bounded improvements and unlimited policies, including stale ones")))
+
+(deftest ^:synchronized fix-when-disabled-test
(testing "fix! explains that the ratchets are disabled and leaves the file unchanged"
(let [dir (.toFile (java.nio.file.Files/createTempDirectory
"kondo-ratchet-test"
@@ -293,6 +411,89 @@
(with-out-str (kondo-ratchet/fix! {:seed "whatever"}))))
(is (= "{:disabled true}\n" (slurp budgets)))))))
+(deftest ^:synchronized seed-unlimited-linter-test
+ (let [dir (.toFile (java.nio.file.Files/createTempDirectory
+ "kondo-ratchet-test"
+ (make-array java.nio.file.attribute.FileAttribute 0)))
+ ratchets {:ignore-counts {:free :unlimited}, :config-counts {}, :comment-exempt #{}}
+ budgets (doto (io/file dir "ratchets.edn") (spit (kondo-ratchet/render ratchets)))
+ occurrences [{:file "f.clj", :line 1, :linters [:free]}
+ {:file "f.clj", :line 2, :linters [:free]}]]
+ (binding [kondo-ratchet/*ratchets-file* (.getPath budgets)]
+ (with-redefs [kondo-ratchet/known-linters (constantly #{:free})
+ kondo-ratchet/scan (constantly occurrences)
+ kondo-ratchet/config-suppressions (constantly {})]
+ (is (= ["seeded :free at 2"
+ (str "wrote " (.getPath budgets))]
+ (str/split-lines (with-out-str (kondo-ratchet/fix! {:seed "free"}))))))
+ (is (= {:ignore-counts {:free 2}
+ :config-counts {}
+ :comment-exempt #{}}
+ (kondo-ratchet/read-ratchets))))))
+
+(deftest ^:synchronized seed-unknown-linter-test
+ (let [dir (.toFile (java.nio.file.Files/createTempDirectory
+ "kondo-ratchet-test"
+ (make-array java.nio.file.attribute.FileAttribute 0)))
+ text (kondo-ratchet/render {:ignore-counts {:free :unlimited}, :config-counts {}, :comment-exempt #{}})
+ budgets (doto (io/file dir "ratchets.edn") (spit text))]
+ (binding [kondo-ratchet/*ratchets-file* (.getPath budgets)]
+ (with-redefs [kondo-ratchet/known-linters (constantly #{:free})
+ kondo-ratchet/scan (fn [] (throw (AssertionError. "scanned before validating the seed")))]
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo
+ #"^cannot seed :bogus: not a known linter -- policies must name"
+ (kondo-ratchet/fix! {:seed ":bogus"})))
+ (is (= text (slurp budgets))
+ "nothing is written")))))
+
+(deftest ^:synchronized fix-keeps-empty-unlimited-linter-test
+ (let [dir (.toFile (java.nio.file.Files/createTempDirectory
+ "kondo-ratchet-test"
+ (make-array java.nio.file.attribute.FileAttribute 0)))
+ ratchets {:ignore-counts {:free :unlimited, :empty :unlimited, :gone 2, :zero 0}
+ :config-counts {}
+ :comment-exempt #{}}
+ budgets (doto (io/file dir "ratchets.edn") (spit (kondo-ratchet/render ratchets)))
+ occurrences [{:file "f.clj", :line 1, :linters [:free]}]
+ run! #(str/split-lines (with-out-str (kondo-ratchet/fix!)))]
+ (binding [kondo-ratchet/*ratchets-file* (.getPath budgets)]
+ (with-redefs [kondo-ratchet/known-linters (constantly #{:free :empty :gone :zero})
+ kondo-ratchet/scan (constantly occurrences)
+ kondo-ratchet/config-suppressions (constantly {})]
+ (is (= ["dropped :gone (no ignores left)"
+ "dropped :zero (no ignores left)"
+ "WARNING: :unlimited policies with no ignores left: :empty -- delete an entry by hand once its linter no longer needs one"
+ (str "wrote " (.getPath budgets))]
+ (run!))
+ "the bounded zeros go, a hand-written 0 included; the unlimited zero stays and is reported")
+ (is (= {:ignore-counts {:free :unlimited, :empty :unlimited}
+ :config-counts {}
+ :comment-exempt #{}}
+ (kondo-ratchet/read-ratchets)))
+ (is (= ["WARNING: :unlimited policies with no ignores left: :empty -- delete an entry by hand once its linter no longer needs one"
+ "unchanged"]
+ (run!))
+ "a second run changes nothing and still reports")))))
+
+(deftest read-ratchets-requires-one-map-test
+ (doseq [[content message] [["" #"is empty; expected one map"]
+ [" \n;; only a comment\n" #"is empty; expected one map"]
+ ["[:not :a :map]" #"must hold a map of policies, not \[:not :a :map\]"]
+ ["{:ignore-counts {}} {}" #"holds more than one form"]]]
+ (let [file (doto (java.io.File/createTempFile "kondo-ratchets" ".edn")
+ (spit content))]
+ (binding [kondo-ratchet/*ratchets-file* (.getPath file)]
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo message
+ (kondo-ratchet/read-ratchets))
+ (str (pr-str content) " must not read as an empty policy set")))))
+ (doseq [content ["{:disabled true}"
+ ";; leading comment\n{:ignore-counts {:a 1}} ;; trailing comment\n"]]
+ (let [file (doto (java.io.File/createTempFile "kondo-ratchets" ".edn")
+ (spit content))]
+ (binding [kondo-ratchet/*ratchets-file* (.getPath file)]
+ (is (map? (kondo-ratchet/read-ratchets))
+ (str (pr-str content) " is one map, with or without comments around it"))))))
+
(deftest ^:parallel missing-ratchets-file-fails-test
(let [dir (.toFile (java.nio.file.Files/createTempDirectory
"kondo-ratchet-test"
@@ -306,6 +507,85 @@
#"ratchets.edn is missing"
(kondo-ratchet/disabled?))))))
+;;;; ---------------------------------------------------------------------------
+;;;; Known-linter unit tests
+;;;; ---------------------------------------------------------------------------
+
+(deftest ^:parallel builtin-linters-test
+ (let [builtin (kondo-ratchet/builtin-linters)]
+ (is (every? builtin [:unused-binding :redundant-ignore :unresolved-namespace])
+ "the pinned jar's default config names kondo's own linters")
+ (is (not-any? builtin [:metabase/modules :unresolved-require])
+ "repository linters and made-up names are not built-ins")))
+
+(defn- git-in
+ "Run git in `dir`, failing the test on a nonzero exit."
+ [dir & args]
+ (let [{:keys [exit err]} (apply sh/sh "git" (concat args [:dir dir]))]
+ (is (zero? exit) (str "git " (str/join " " args) ": " err))))
+
+(deftest ^:parallel repository-linters-test
+ (let [dir (.toFile (java.nio.file.Files/createTempDirectory
+ "kondo-ratchet-linters-test"
+ (make-array java.nio.file.attribute.FileAttribute 0)))]
+ (git-in dir "init" "-q")
+ (spit (io/file dir "config.edn")
+ (pr-str '{:linters {:custom/top {:level :warning}}
+ :config-in-ns {some.ns {:linters {:custom/scoped {:level :off}}}}
+ :hooks {:analyze-call {foo/bar hooks.foo/bar}}}))
+ (.mkdirs (io/file dir "some-lib" "some-lib"))
+ (spit (io/file dir "some-lib" "some-lib" "config.edn")
+ (pr-str '{:linters {:custom/lib {:level :error}}}))
+ (spit (io/file dir "not-config.txt") "{:linters {:custom/ignored {}}}")
+ (spit (io/file dir "deleted.edn") (pr-str '{:linters {:custom/deleted {:level :error}}}))
+ (git-in dir "add" "config.edn" "some-lib" "not-config.txt" "deleted.edn")
+ (.delete (io/file dir "deleted.edn"))
+ (.mkdirs (io/file dir "copied-lib" "copied-lib"))
+ (spit (io/file dir "copied-lib" "copied-lib" "config.edn")
+ (pr-str '{:linters {:custom/untracked {:level :error}}}))
+ (.mkdirs (io/file dir ".cache" "v1"))
+ (spit (io/file dir ".cache" "v1" "junk.edn") "<not edn>")
+ (is (= #{:custom/top :custom/scoped :custom/lib}
+ (kondo-ratchet/repository-linters (.getPath dir)))
+ "top-level and scoped :linters maps count, in nested directories too; untracked files (the configs
+ kondo copies in for dependencies, its cache), non-edn files, and tracked files deleted from the
+ worktree do not"))
+ (testing "the repository's own hook linters are found"
+ (is (contains? (kondo-ratchet/repository-linters) :metabase/modules))))
+
+(deftest ^:parallel known-linters-test
+ (let [known (kondo-ratchet/known-linters)]
+ (is (every? known [:unused-binding :metabase/modules :clojure-lsp/unused-public-var :all])
+ "built-ins, repository linters, external diagnostics, and the vector-less :all form")
+ (is (not (contains? known :unresolved-require))
+ "a linter that never existed stays unknown")))
+
+(deftest ^:parallel unknown-linters-test
+ (let [known #{:a :b :c}]
+ (is (= #{}
+ (kondo-ratchet/unknown-linters {:ignore-counts {:a 1, :b :unlimited}
+ :config-counts {:c 2}
+ :comment-exempt #{:a}}
+ known)))
+ (is (= #{:x/ignored :y-config :z-exempt}
+ (kondo-ratchet/unknown-linters {:ignore-counts {:a 1, :x/ignored 1}
+ :config-counts {:y-config 2}
+ :comment-exempt #{:z-exempt}}
+ known))
+ "each policy collection is checked")))
+
+(deftest validate-linters-test
+ (binding [kondo-ratchet/*ratchets-file* "r.edn"]
+ (is (nil? (kondo-ratchet/validate-linters! {:ignore-counts {:a 1}, :config-counts {}, :comment-exempt #{}}
+ #{:a})))
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo
+ #"^r.edn names 3 unknown linters: :b, :c/d, :e -- policies must name"
+ (kondo-ratchet/validate-linters! {:ignore-counts {:e 1, :a 2}
+ :config-counts {:c/d 1}
+ :comment-exempt #{:b}}
+ #{:a}))
+ "one error lists every unknown name in order")))
+
;;;; ---------------------------------------------------------------------------
;;;; Justification bookkeeping unit tests
;;;; ---------------------------------------------------------------------------
@@ -318,7 +598,11 @@
(is (= [2 4]
(map :line (kondo-ratchet/unjustified #{:a} occurrences)))
"line 1 is fully exempt, line 2 still owes :b a comment, line 3 is justified,
- line 4's :all is not exempt")))
+ line 4's :all is not exempt"))
+ (testing "the count policy plays no part: only :comment-exempt waives the comment"
+ (let [occurrence {:file "f.clj", :line 1, :linters [:free], :justified? false}]
+ (is (= [occurrence] (kondo-ratchet/unjustified #{} [occurrence])))
+ (is (= [] (kondo-ratchet/unjustified #{:free} [occurrence]))))))
(deftest ^:parallel stale-exemptions-test
(let [occurrences [{:file "f.clj", :line 1, :linters [:a], :justified? false}
@@ -362,7 +646,7 @@
{:same 5, :new 1, :up 3}))))
(deftest ^:parallel change-report-test
- (let [occurrences (concat (for [[linter n] {:lower 3, :over 7, :new 9, :same 4}
+ (let [occurrences (concat (for [[linter n] {:lower 3, :over 7, :new 9, :same 4, :free 2}
i (range n)]
{:file "f.clj", :line (inc i), :linters [linter], :justified? false})
[{:file "g.clj", :line 1, :linters [:polite], :justified? true}])]
@@ -371,14 +655,28 @@
"dropped :gone (no ignores left)"
"lowered :lower 5 -> 3"
"WARNING: :over is over budget (5 recorded, 7 actual) -- remove ignores, or accept them all with `--seed :over`"
+ "dropped :zero (no ignores left)"
+ "WARNING: :unlimited policies with no ignores left: :empty -- delete an entry by hand once its linter no longer needs one"
"dropped config :cfg-gone (no suppressions left)"
"lowered config :cfg-lower 4 -> 2"
"WARNING: config suppressions for :cfg-over are over budget (1 recorded, 3 actual) -- remove one from .clj-kondo/config.edn or raise the budget by hand"
"unexempted :polite (all its ignores are justified now)"]
- (kondo-ratchet/change-report {:ignore-counts {:lower 5, :over 5, :gone 5, :same 4, :polite 1}
- :config-counts {:cfg-lower 4, :cfg-over 1, :cfg-gone 2, :cfg-same 6}
+ (kondo-ratchet/change-report {:ignore-counts {:empty :unlimited
+ :free :unlimited
+ :gone 5
+ :lower 5
+ :over 5
+ :polite 1
+ :same 4
+ :zero 0}
+ :config-counts {:cfg-gone 2
+ :cfg-lower 4
+ :cfg-over 1
+ :cfg-same 6}
:comment-exempt #{:lower :polite}}
occurrences
{:cfg-lower 2, :cfg-over 3, :cfg-same 6}
[:new :void]))
- "untouched budgets (:same, :cfg-same) and a still-needed exemption (:lower) earn no line")))
+ "untouched budgets (:same, :cfg-same), a used unlimited policy (:free), and a still-needed
+ exemption (:lower) earn no line; a hand-written 0 (:zero) is dropped like any bounded budget with no
+ ignores left; the empty unlimited policy (:empty) is kept and warned about")))
diff --git a/enterprise/backend/src/metabase_enterprise/semantic_search/pgvector_api.clj b/enterprise/backend/src/metabase_enterprise/semantic_search/pgvector_api.clj
index 5100e811603..357b5b4b398 100644
--- a/enterprise/backend/src/metabase_enterprise/semantic_search/pgvector_api.clj
+++ b/enterprise/backend/src/metabase_enterprise/semantic_search/pgvector_api.clj
@@ -204,7 +204,7 @@
(semantic.index/delete-from-index! pgvector index model ids)))
;; REPL-only requires in the comment block; metabase.test also crosses the module boundary
-#_{:clj-kondo/ignore [:unresolved-require :metabase/modules]}
+#_{:clj-kondo/ignore [:metabase/modules]}
(comment
(init-semantic-search! pgvector index-metadata embedding-model)
(index-documents! pgvector index-metadata [{:model "card"
diff --git a/mage/src/mage/project_tests.clj b/mage/src/mage/project_tests.clj
index 7825e7077ed..9087bd4819a 100644
--- a/mage/src/mage/project_tests.clj
+++ b/mage/src/mage/project_tests.clj
@@ -12,7 +12,8 @@
metabase.core.modules-test])
(def ^:private ratchet-check-namespaces
- '[metabase.core.kondo-ratchet-test])
+ '[metabase.core.kondo-ratchet-test
+ metabase.core.kondo-ratchet-check-test])
(def ^:private backend-check-namespaces
(vec (concat module-check-namespaces ratchet-check-namespaces)))
diff --git a/mage/test/mage/project_tests_test.clj b/mage/test/mage/project_tests_test.clj
index baac7d6bde4..5c872656c3f 100644
--- a/mage/test/mage/project_tests_test.clj
+++ b/mage/test/mage/project_tests_test.clj
@@ -42,7 +42,7 @@
(testing "both suites run, in order"
(is (= [["clojure" "-M:test"]
["clojure" "-X:dev:dev/test:ee:ee-dev:drivers:drivers-dev:test:ci" ":only"
- "[dev.modules-config-test metabase.core.modules-test metabase.core.kondo-ratchet-test]"]]
+ "[dev.modules-config-test metabase.core.modules-test metabase.core.kondo-ratchet-test metabase.core.kondo-ratchet-check-test]"]]
calls)))
(testing "the failed suite is reported"
(is (= ["migrations"] failed))
@@ -82,5 +82,5 @@
"[dev.modules-config-test metabase.core.modules-test]"]]
(:calls (run-suites! [] ["modules"]))))
(is (= [["clojure" "-X:dev:dev/test:ee:ee-dev:drivers:drivers-dev:test:ci" ":only"
- "[metabase.core.kondo-ratchet-test]"]]
+ "[metabase.core.kondo-ratchet-test metabase.core.kondo-ratchet-check-test]"]]
(:calls (run-suites! [] ["ratchets"]))))))
diff --git a/src/metabase/lib/aggregation.cljc b/src/metabase/lib/aggregation.cljc
index a8bbe29f85e..09382da550a 100644
--- a/src/metabase/lib/aggregation.cljc
+++ b/src/metabase/lib/aggregation.cljc
@@ -1,4 +1,5 @@
(ns metabase.lib.aggregation
+ ;; clj-kondo treats the CLJ-only doseq/for exclusions and performance refers as unused in the CLJS pass
{:clj-kondo/ignore [:unused-excluded-var]}
(:refer-clojure :exclude [count distinct max min select-keys mapv empty? not-empty #?(:clj doseq) #?(:clj for)])
(:require
diff --git a/src/metabase/util/malli/schema.clj b/src/metabase/util/malli/schema.clj
index adb237bb5ae..c96b9c97ecb 100644
--- a/src/metabase/util/malli/schema.clj
+++ b/src/metabase/util/malli/schema.clj
@@ -4,7 +4,6 @@
For example the PositiveInt can be defined as (mr/def ::positive-int pos-int?)"
(:require
[clojure.string :as str]
- [malli.util :as mut]
[metabase.util :as u]
[metabase.util.date-2 :as u.date]
[metabase.util.i18n :as i18n :refer [deferred-tru]]
@@ -71,17 +70,6 @@
maps-schema]
(deferred-tru "value must be seq of maps in which {0}s are unique" (name k))))))
-#_{:clj-kondo/ignore [:unused-private-var]}
-(def ^{:arglists '([map-schema])
- :private true}
- ->kebab-keys-map
- "Transform all keys of a map schema to kebab keys."
- (memoize
- (fn [map-schema]
- (mut/transform-entries map-schema
- (partial map (fn [[k opts s]]
- [(u/->kebab-case-en k) opts s]))))))
-
(defn enum-keywords-and-strings
"Returns an enum schema that accept both keywords and strings.
(enum-keywords-and-strings :foo :bar)