# Apply Module Boundary Enforcement

> Use when a codebase has intended structure (features, layers, packages, monorepo libs) but imports keep crossing it — circular dependencies, features reaching i

- Skill: `jeffreytse/apply-module-boundary-enforcement` (Agent Skill)
- Install (CLI): `npx get-skill add jeffreytse/apply-module-boundary-enforcement`
- Raw SKILL.md: https://api.skillmd.com/api/skills/jeffreytse/apply-module-boundary-enforcement/raw
- Safety review: pending
- Works with: any agent that reads SKILL.md (Claude Code, Claude.ai, Cursor, Codex, Windsurf, 60+ more)
- Category: Coding & Dev Tools
- Author: jeffreytse (https://skillmd.com/u/jeffreytse)
- Updated: 2026-12-03
- Page: https://skillmd.com/skills/jeffreytse/apply-module-boundary-enforcement

---

---
name: apply-module-boundary-enforcement
description: Use when a codebase has intended structure (features, layers, packages, monorepo libs) but imports keep crossing it — circular dependencies, features reaching into each other's internals, domain code importing infrastructure — and code review alone is not stopping the erosion.
source: 'TNG Technology Consulting, ArchUnit User Guide; Nx, "Enforce Module Boundaries" documentation; Shopify Engineering, "Enforcing Modularity in Rails Apps with Packwerk", 2020; Go, "Internal packages" design (Go 1.4), 2014; Oracle, "Java Platform Module System" (JEP 261), 2017; Forsgren, Humble & Kim, "Accelerate", 2018'
tags: [project-structure, architecture-tests, dependency-rules, linting, developer, architecture-erosion]
related: [apply-package-by-feature, apply-hexagonal-architecture, design-monorepo-structure, apply-low-coupling, write-adr]
---

# Apply Module Boundary Enforcement

Turn your intended architecture into automated checks that fail the build when code imports across a forbidden boundary, so the structure survives deadlines and new team members.

## Why This Is Best Practice

**Adopted by:**
- Shopify (Packwerk, built to modularize their Rails monolith).
- Nx monorepos (`@nx/enforce-module-boundaries`).
- Java teams using ArchUnit, Spring Modulith, and the JPMS.
- The Go toolchain (`internal/` directories are compiler-enforced).
- Python projects using import-linter, and JavaScript/TypeScript projects using dependency-cruiser and eslint-plugin-boundaries.

**Impact:**
- Shopify reported that making dependency violations visible and checkable was central to untangling their monolith into components (2020).
- *Accelerate* (2018) identifies loosely coupled architecture as a key predictor of delivery performance. Enforcement keeps coupling from creeping back.

**Why best:**
- Documented architecture erodes. A diagram doesn't fail CI, and one convenient import is all it takes.
- Automated rules are cheap, objective, and catch violations at the moment they're introduced, when they're easiest to fix. Reviewers stop having to police imports by hand.

Sources: ArchUnit User Guide (archunit.org). Nx documentation, "Enforce Module Boundaries". Shopify Engineering (2020), "Enforcing Modularity in Rails Apps with Packwerk". Go 1.4 release notes (internal packages). JEP 261. Forsgren, Humble & Kim (2018), *Accelerate*.

## Steps

1. **Write down the intended rules** in plain language, ideally in an ADR (see `write-adr`):
   - "Features may import `shared/` but not each other's internals."
   - "`domain/` imports nothing from `adapters/` or frameworks."
   - "`apps/*` may depend on `libs/*`. `libs/*` never depend on `apps/*`."
   - "No circular dependencies between modules."

2. **Use language-level encapsulation first. It's free:**
   - Go: put private code under `internal/`.
   - Java/Kotlin: package-private visibility, JPMS `module-info.java`, Gradle modules with `implementation` (not `api`) dependencies.
   - TypeScript: a public `index.ts` per module plus package.json `exports` in workspaces.
   - Rust: `pub(crate)` and workspace crates. .NET: `internal` and separate projects.

3. **Add a rule-checking tool for what the language can't express:**

   | Ecosystem | Tool |
   |-----------|------|
   | JS/TS | eslint-plugin-boundaries, dependency-cruiser, Nx `enforce-module-boundaries`, Steiger (FSD) |
   | Java/Kotlin | ArchUnit, Spring Modulith, Konsist (Kotlin) |
   | Python | import-linter |
   | Ruby | Packwerk |
   | .NET | NetArchTest, ArchUnitNET |
   | Polyglot/Bazel | `visibility` attributes |

4. **Encode rules as code** next to the source, for example an ArchUnit test:
   ```java
   @ArchTest
   static final ArchRule domainIsIndependent =
       noClasses().that().resideInAPackage("..domain..")
           .should().dependOnClassesThat()
           .resideInAnyPackage("..adapters..", "org.springframework..");
   ```

5. **Run the checks in CI and in the editor.** Fail the pull request on new violations. Lint-based rules give instant IDE feedback.

6. **Baseline legacy violations, then ratchet.** In an existing codebase, record current violations (ArchUnit `FreezingArchRule`, the Packwerk todo file, dependency-cruiser `--ignore-known`). Block *new* violations and burn down the baseline over time.

7. **Visualize the dependency graph** periodically (dependency-cruiser, `nx graph`, `go mod graph`, IDE diagrams) to spot emerging hotspots and cycles.

8. **Review rules when the architecture changes.** Update the rule and the ADR together. Never silence rules with blanket ignores.

## Rules

- Every intended boundary has an automated check, or it doesn't really exist.
- Prefer compiler or visibility enforcement over linters, and linters over conventions.
- New violations fail CI. Old ones are baselined and reduced.
- Rules live in the repo and change through review.

## Common Mistakes

- **Enforcing on day one with hundreds of errors.** Teams disable the tool. Baseline first.
- **Rules nobody understands.** Each rule needs a message explaining *why* and how to fix the violation.
- **Escape-hatch abuse.** Per-line disables accumulate. Track and review them.
- **Checking only layers, not cycles.** Circular dependencies between features are often the biggest problem.

## Examples

**Nx monorepo:** libraries are tagged `type:feature`, `type:ui`, `type:data-access`, `type:util`, and `scope:checkout`. The rule "feature may depend on ui, data-access, util; ui may depend only on util; scope:checkout may not import scope:admin" is enforced by ESLint in CI.

**Rails monolith:** Packwerk packages per domain with a public folder. The CI check fails on new privacy violations, and the todo file is reduced by 10% per quarter.

## When NOT to Use

- **Single-module projects** with no meaningful internal boundaries yet.
- **Exploratory prototypes** where structure is still being discovered. Add enforcement once boundaries stabilize.

