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
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."
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.
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 |
Encode rules as code next to the source, for example an ArchUnit test:
@ArchTest
static final ArchRule domainIsIndependent =
noClasses().that().resideInAPackage("..domain..")
.should().dependOnClassesThat()
.resideInAnyPackage("..adapters..", "org.springframework..");
Run the checks in CI and in the editor. Fail the pull request on new violations. Lint-based rules give instant IDE feedback.
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.
Visualize the dependency graph periodically (dependency-cruiser, nx graph, go mod graph, IDE diagrams) to spot emerging hotspots and cycles.
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.
1---2name: apply-module-boundary-enforcement3description: Use when a codebase has intended structure (features, layers, packages, monorepo libs) but imports keep crossing it — circular dependencies, features reaching i4---5---6name: apply-module-boundary-enforcement7description: 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.8source: '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'9tags: [project-structure, architecture-tests, dependency-rules, linting, developer, architecture-erosion]10related: [apply-package-by-feature, apply-hexagonal-architecture, design-monorepo-structure, apply-low-coupling, write-adr]11---1213# Apply Module Boundary Enforcement1415Turn 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.1617## Why This Is Best Practice1819**Adopted by:**20- Shopify (Packwerk, built to modularize their Rails monolith).21- Nx monorepos (`@nx/enforce-module-boundaries`).22- Java teams using ArchUnit, Spring Modulith, and the JPMS.23- The Go toolchain (`internal/` directories are compiler-enforced).24- Python projects using import-linter, and JavaScript/TypeScript projects using dependency-cruiser and eslint-plugin-boundaries.2526**Impact:**27- Shopify reported that making dependency violations visible and checkable was central to untangling their monolith into components (2020).28- *Accelerate* (2018) identifies loosely coupled architecture as a key predictor of delivery performance. Enforcement keeps coupling from creeping back.2930**Why best:**31- Documented architecture erodes. A diagram doesn't fail CI, and one convenient import is all it takes.32- 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.3334Sources: 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*.3536## Steps37381. **Write down the intended rules** in plain language, ideally in an ADR (see `write-adr`):39 - "Features may import `shared/` but not each other's internals."40 - "`domain/` imports nothing from `adapters/` or frameworks."41 - "`apps/*` may depend on `libs/*`. `libs/*` never depend on `apps/*`."42 - "No circular dependencies between modules."43442. **Use language-level encapsulation first. It's free:**45 - Go: put private code under `internal/`.46 - Java/Kotlin: package-private visibility, JPMS `module-info.java`, Gradle modules with `implementation` (not `api`) dependencies.47 - TypeScript: a public `index.ts` per module plus package.json `exports` in workspaces.48 - Rust: `pub(crate)` and workspace crates. .NET: `internal` and separate projects.49503. **Add a rule-checking tool for what the language can't express:**5152 | Ecosystem | Tool |53 |-----------|------|54 | JS/TS | eslint-plugin-boundaries, dependency-cruiser, Nx `enforce-module-boundaries`, Steiger (FSD) |55 | Java/Kotlin | ArchUnit, Spring Modulith, Konsist (Kotlin) |56 | Python | import-linter |57 | Ruby | Packwerk |58 | .NET | NetArchTest, ArchUnitNET |59 | Polyglot/Bazel | `visibility` attributes |60614. **Encode rules as code** next to the source, for example an ArchUnit test:62 ```java63 @ArchTest64 static final ArchRule domainIsIndependent =65 noClasses().that().resideInAPackage("..domain..")66 .should().dependOnClassesThat()67 .resideInAnyPackage("..adapters..", "org.springframework..");68 ```69705. **Run the checks in CI and in the editor.** Fail the pull request on new violations. Lint-based rules give instant IDE feedback.71726. **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.73747. **Visualize the dependency graph** periodically (dependency-cruiser, `nx graph`, `go mod graph`, IDE diagrams) to spot emerging hotspots and cycles.75768. **Review rules when the architecture changes.** Update the rule and the ADR together. Never silence rules with blanket ignores.7778## Rules7980- Every intended boundary has an automated check, or it doesn't really exist.81- Prefer compiler or visibility enforcement over linters, and linters over conventions.82- New violations fail CI. Old ones are baselined and reduced.83- Rules live in the repo and change through review.8485## Common Mistakes8687- **Enforcing on day one with hundreds of errors.** Teams disable the tool. Baseline first.88- **Rules nobody understands.** Each rule needs a message explaining *why* and how to fix the violation.89- **Escape-hatch abuse.** Per-line disables accumulate. Track and review them.90- **Checking only layers, not cycles.** Circular dependencies between features are often the biggest problem.9192## Examples9394**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.9596**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.9798## When NOT to Use99100- **Single-module projects** with no meaningful internal boundaries yet.101- **Exploratory prototypes** where structure is still being discovered. Add enforcement once boundaries stabilize.