Skip to content
Back to skills

Adding A Rule

ASecurity

Use when adding a new ArchUnit rule to Corral's central catalog (corral-rules) — creating a rule class, choosing its id, registering it in a group, or a rule test that seems to pass without checking anything

  • 2 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added October 11, 2026
developmentgojavabashnodetestinggit

Works with

  • cli

Security analysis

A100/100

Scanned November 10, 2026

npx -y skills add MilczekT1/Corral --skill adding-a-rule --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Adding A Rule?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Adding A Rule
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/milczekt1-adding-a-rule/badge)](https://www.skillsdirectory.com/skills/milczekt1-adding-a-rule)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: adding-a-rule
description: Use when adding a new ArchUnit rule to Corral's central catalog (corral-rules) — creating a rule class, choosing its id, registering it in a group, or a rule test that seems to pass without checking anything
---

# Adding a Rule

## Overview

Adding a rule to Corral's catalog touches 7+ files and has two failure modes that produce a
**green build with zero enforcement** — no test catches them structurally; you have to build in the
right order. This skill is that order, the two traps named explicitly, and a checklist for
proving the rule checks something before you call it done.

Read [docs/creating-a-rule.md](../../../docs/creating-a-rule.md), which is the contract every rule
follows, and [CONTRIBUTING.md § Rule ids](../../../CONTRIBUTING.md#rule-ids) first — this skill
sequences and cross-references them for *this* catalog, it does not restate them.

## The steps, in order

1. **Choose the id.** Grammar and caps are in
   [CONTRIBUTING.md § Rule ids](../../../CONTRIBUTING.md#rule-ids). Segment 1 is always the fixed
   `corral.` vendor prefix — never a judgment call. The segments after it are free-form; reuse a word
   the catalog already uses when one fits. The one judgment call: the segment-2 concern (or, for a
   library-specific rule, the library segment after it) is the non-JDK **library whose
   correct use the rule asserts**, not a library the predicate merely uses to *detect* something. A
   rule that flags JUnit/Mockito types leaking into production code names them only to find them — it
   polices layering, not either library's correct use — so it stays `corral.layering.*`, not
   `corral.test.*` or `corral.test.mockito.*`. Ask "what is this rule actually asserting about?", not
   "what classes does the predicate mention?".

2. **Create the rule class** at
   `corral-rules/src/main/java/io/github/milczekt1/corral/rules/<topic>/<rule>/<Name>Rule.java` — a
   `final class implements DocumentedRule` with a private constructor and a name ending in `Rule`
   (see `TestClassNamingConventionRule`). **Field order is `DOC`, then `DEFINITION`, then the
   `@ArchTest` field**: `guard()` runs at class init and dereferences both, so the field declared
   above either throws `ExceptionInInitializerError` in every test that touches the rule.

   **If the rule targets another library's type, name it as a fully-qualified string, never a class
   literal**, and add no dependency for it. A class literal is resolved when the field initialises,
   and `guard()` runs at class init — so on a consumer without the library the rule class fails to
   load and takes the whole group node with it. Match as weakly as the defect allows: a package
   prefix through `dependOnClassesThat()` needs no resolution at all, while assignability and
   meta-annotation checks silently return false when the type is absent. See
   [Targeting a library you don't depend on](../../../docs/creating-a-rule.md#targeting-a-library-you-dont-depend-on).

3. **Write the rule test** at `.../rules/<topic>/<rule>/<Name>RuleTest.java`, covering **both
   directions** against the raw `DEFINITION` field — see Trap 1 below for why the published field
   cannot carry these assertions. The test shares the rule's package deliberately: `DEFINITION` is
   package-private, so a test anywhere else cannot reach it without widening the published surface.

4. **Put the examples where nothing will run them and nothing will lint them**, and reach them with
   `importClasses(...)`. Only the compiler has to see them, which is what puts them in test output
   and therefore in `TestScope.TEST_CLASSES`.

   **Every example is a top-level class in a `fixtures/` package beside the test**, as
   `NoJUnit4Rule` and `NoDisabledWithoutReasonRule` do — plain code included. Never a `static` nested
   class in the test: it clutters the test, and Sonar reads a nested class in a test file as part of
   that test and flags the deliberate violation under test. `**/fixtures/**` is excluded from
   Surefire, Failsafe and Sonar; nothing excludes a nested class from analysis. Getting this wrong
   costs a dozen findings on the PR, not a build failure, so it surfaces late. Give example methods
   real bodies.

   **Open every fixture's type Javadoc with its verdict and why:** `MUST FLAG`, `MUST IGNORE`, or
   `SUPPORT` for a type that is not an example (a collaborator, a composed annotation). A class with
   any flagged member opens with `MUST FLAG`. Add a test calling
   `FixtureVerdicts.assertMatch(TheRuleTest.class, TheRule.DEFINITION, EXAMPLES)`: it fails a
   `MUST FLAG` fixture that is not reported, a `MUST IGNORE` one that is, and either one that
   `EXAMPLES` never imports. `FixtureVerdictsTest` fails a fixture with no verdict, and
   `CatalogArchitectureTest` fails a rule test that skips the call.

   **A production-scoped rule needs its examples in production output**, but every fixture compiles
   into test output. Put those examples in `fixtures/production/`, keep the ones that must stay in
   test output in `fixtures/`, and import both with
   `ProductionLayout.importFixtures(TheRuleTest.class, outputRoot)`.

   **Both directions is two things, not necessarily two classes.** Where the verdict is about a
   *call*, one example carries both — `NoThreadSleepRule`'s `ThreadSleeper` sleeps *and* calls
   `Thread.currentThread`, and the store recording one line rather than two is the assertion. Add a
   second, deliberately-ignored class only when one cannot hold both: a rule whose verdict is about
   the *class* needs one, because the first example is already a violation. What is not optional is
   that an over-broad predicate fails — a lone example with a lone matching call cannot do that,
   since every too-wide predicate still finds exactly that one call.

   Never name a top-level example `*IT` outside a `fixtures` package. Failsafe includes
   `**/*IT.java` and excludes only `**/fixtures/**`, so such a class runs as a real integration
   test — an example that sleeps then really sleeps.

   Give them names where none is a substring of another: the assertions match on the report text.

5. **Freeze the examples into the committed store**, in the same test. Steps 3 and 4 prove the
   predicate; this proves the finding reaches
   `corral-rules/src/test/resources/archunit/frozen/<id>` — the file every consumer's recorded debt
   is the shape of. Committing it makes the store a reviewed artefact: widen or reword a predicate
   and the extra findings fail the build instead of quietly joining the accepted set.

   ```java
   @Test
   void freezesWhatItFindsIntoTheCommittedStore() throws IOException {
       String debt = CommittedStore.freeze(TheRule.rule, ID, EXAMPLES);

       assertTrue(debt.contains("FlaggedExample"), debt);
       assertFalse(debt.contains("IgnoredExample"), debt);
   }
   ```

   `CommittedStore` (`rules/testsupport`) checks the published field against the store and asserts
   the `stored.rules` index files it under the id. It hands the store over with `persistIn`; do not
   name it in `freeze.store` instead. `FreezingArchRule` captures its store when the rule object is
   *constructed*, and the published field is constructed during class initialisation — at whichever
   test touches the rule first. A `freeze.store` set in a test body therefore races class loading:
   it passes when the test runs alone and silently gets ArchUnit's stock store in a full module run.

   Seed the store once, then **commit it**:

   ```bash
   ./mvnw test -pl corral-rules -Darchunit.freeze.store.default.allowStoreCreation=true \
       -Darchunit.freeze.store.default.allowStoreUpdate=true
   ```

   `CommittedStore` forbids store updates otherwise, so a missing entry, or a recorded line the
   predicate stopped matching, fails loudly rather than rewriting the store green.

   Before seeding, make each line survive edits to the violating file — see
   [Keep the frozen line stable](#keep-the-frozen-line-stable). Once a consumer has recorded it,
   rewording it is a breaking change.

6. **Register it in a group** as an `@ArchTest ArchTests` field (see `TestingRulesGroup`). A rule
   class nobody points at is imported and compiled but never evaluated by any consumer.

   A group is adopted whole, so everything in one runs for whoever wires it. A rule a team can
   reasonably decline stands alone instead, in **no** group: list it on `EveryStandaloneRule` (test
   sources) so the grammar, doc and id-uniqueness tests still walk it, give it a row in the
   **Standalone rules** table of `docs/rules.md` rather than the main one, and leave
   `PublishedCatalogTest` alone — step 7 does not apply. Consumers wire it with
   `ArchTests.in(TheRule.class)`.

7. **Extend the expected id set** in
   `PublishedCatalogTest.ruleDiscoveryDescendsThroughNestedGroups` (`corral-rules/src/test/java/io/github/milczekt1/corral/groups/PublishedCatalogTest.java`).

8. **Add a row to the [rules catalog](../../../docs/rules.md).** `RulesCatalogDocTest` fails the
   build if you skip it: it checks the id set against the published groups *and* the group named in
   column 2. Only those two columns — a dangling rule id in the prose column passes.

## Keep the frozen line stable

Each violation line is a key in a consumer's committed store. Edit the violating file without
touching the violation — add a method, move code down, drop a redundant modifier — and the line must
still match; otherwise the recorded debt resurfaces as a new failure on code nobody broke. ArchUnit's
default `FuzzyViolationLineMatcher` compares lines verbatim except for two runs of digits: those
between `:` and `)`, which is how `getSourceCodeLocation()` prints `(File.java:N)`, and those
directly after a `$` (`Outer$1`, `lambda$run$0`). Every other character is the key. So when the rule
writes its own text (`SimpleConditionEvent`, a custom `describe(...)`):

- **Print a location only as `getSourceCodeLocation()`.** `at line N`, `File.java:N` without the
  parentheses, or a line range is compared verbatim, so moving the code rewords it.
- **One fixed shape per line.** A clause is always there or never there — keep `in (File.java:0)`
  for abstract methods and `-g:none` builds rather than dropping it. Optional text appearing or
  vanishing changes the whole line.
- **One event per violation, never one per class listing them.** A line naming three offending
  members is a new line once one is fixed or a fourth added, and the still-unfixed two resurface
  with it.
- **Only what identifies the violation**: owner, member, offending target, reason. No counts,
  sibling members, literal values, annotation attributes or modifiers that are not the reason.
- **Several applicable reasons → one, by fixed precedence, pinned by a test**
  (`NoAsyncOnUnproxyableMethodRuleTest.reportsAFinalMethodInAFinalClassAsFinal`). Never join a
  hash-ordered collection into the text.

## Two silent failure modes

Each of these leaves the build green while enforcing nothing. None is caught by a structural check;
catching them is why the steps above are ordered the way they are.

| Trap | Why it's silent |
|---|---|
| **Predicate tested against the published field, not `DEFINITION`** | The published field is wrapped in `FreezingArchRule`. Against a store with no entry for the rule, it seeds every violation as accepted debt and reports zero — the test passes on the very run that should have shown a failure, and every run after. Assert the predicate against the raw `DEFINITION`, which is unfrozen. Step 5 does check the published field, and is *not* this trap: its store is committed, so the run compares against recorded lines instead of seeding. |
| **Consumer sets `ImportOption.DoNotIncludeTests`** | Any rule scoped to test classes (see `TestScope`) then has no test classes to evaluate, so it passes vacuously for that consumer. This one binds *consumers*, not catalog authors — call it out in a rule's Javadoc whenever the rule inspects test-scope code, the way `TestClassNamingConventionRule` does. |

## Three loud failures — and what they mean

Skip a step above and one of these tests fails the build, on purpose:

- **`PublishedCatalogTest`** — fails if the published id set no longer matches its expected set
  (step 6 skipped, or an id changed). If it fails on a rule you did **not** mean to touch, an id got
  renamed somewhere in your diff. **Ids are never renamed** — an id is the freeze-store key, and
  renaming orphans every consumer's recorded violations (rule re-seeds clean, build stays green,
  nothing is enforced). Retire the old id with `DeprecatedRule.supersededBy(retiredId,
  replacementId, why)` in `corral-sdk` instead, and keep the retired id in the published set.
- **`RuleIdGrammarTest`** — fails if the id lacks the `corral.` prefix or a polarity marker (step 1).
  Fix the id, not the test.
- **`RulesCatalogDocTest`** — fails if `docs/rules.md` and the published groups disagree, on either
  the id set or the group column (step 8 skipped, or a rule moved group).

## Before you call it done

The steps above wire the rule up; these prove it checks something. Rationale in
[docs/creating-a-rule.md § 6](../../../docs/creating-a-rule.md). Every one of these caught a real
defect in the last rule added here — none was caught by the build.

- [ ] **Each predicate clause mutation-tested.** Delete a clause, run the rule's test, confirm a
      *named* test fails; restore it. `NoThreadSleepRule` reached review with its scope clause and
      its name clause both unpinned behind a green suite. Mutate a *list* entry by entry, not the
      list as a whole — one clause covers for another, so deleting a whole clause can fail a test
      while a single entry of it stays unpinned.
      **Back up `src/test/resources/archunit/frozen/` first and restore it after each run.** The
      published field is frozen, so a mutation that finds fewer violations makes
      `FreezingArchRule` prune the committed store — and `EmptyOmittingViolationStore` deletes the
      file outright when nothing is left. Reseed and re-run before you call the rule done.
- [ ] **The flagged example holds a call the rule must NOT match.** With one matching call and
      nothing else, an over-broad predicate — up to `alwaysTrue()` — finds exactly the recorded
      violation and passes.
- [ ] **No example's name is a substring of another's.** Assertions match on report text, so a
      contained name makes one case pass on another's violations, and a mutation that should have
      killed it survives.
- [ ] **Run whole: `./mvnw test -pl corral-rules`, not `-Dtest=<OneTest>`.** `ArchConfiguration` is
      process-wide and Surefire reuses the JVM; the freeze-store wiring passed alone and failed in a
      full run, twice, for two different reasons.
- [ ] **The committed store survives an edit to an example.** The only numbers in the `<id>` file an
      edit can move are inside `(File.java:N)` or after a `$`. Then, with the store backed up as
      above, add a line above a flagged member and an unrelated method to its class, and re-run the
      step-5 store test: it must pass without reseeding; revert the example after. A failure names text that would resurface in a consumer's build —
      see [Keep the frozen line stable](#keep-the-frozen-line-stable).
- [ ] **Test names re-read against their assertions.** Rename any that claim more.
- [ ] **`RuleDoc` re-read against the predicate.** It renders into the failure output, so a dodge it
      warns about but the predicate misses is a claim someone will act on.
- [ ] **`./mvnw clean verify` green**, with the committed store either untouched or reseeded and
      committed on purpose.
- [ ] **`rule-reviewer` agent run on the branch, with mutation mode**, and every finding fixed or
      answered. It can do the clause-by-clause mutation step above for you.

## Quick reference

| Artifact | Path |
|---|---|
| Rule class | `corral-rules/src/main/java/io/github/milczekt1/corral/rules/<topic>/<rule>/<Name>Rule.java` |
| Rule test | `corral-rules/src/test/java/io/github/milczekt1/corral/rules/<topic>/<rule>/<Name>RuleTest.java` |
| Examples | `.../<topic>/<rule>/fixtures/`, always — never nested in the test; `fixtures/production/` for those a production-scoped rule imports from production output |
| Committed freeze store | `corral-rules/src/test/resources/archunit/frozen/<id>` (plus its `stored.rules` line) |
| Group wiring | `corral-rules/src/main/java/io/github/milczekt1/corral/groups/<Topic>RulesGroup.java` |
| Discovery test | `corral-rules/src/test/java/io/github/milczekt1/corral/groups/PublishedCatalogTest.java` |
| Rules table | `docs/rules.md` |

`NoJUnit4Rule` is the worked example; `NoTransactionalInControllerRule` is the production-scoped one.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.