Lesson 8 of 9 · 24m
Enforcing Architecture Boundaries Before They Erode
Use lint rules, dependency constraints, module boundaries, and architecture tests to keep the dependency graph aligned with the intended design.
Architecture diagrams do not erode because engineers are careless. They erode because the easiest import is often the wrong one, deadlines are real, and nothing complains at the moment the boundary is crossed. In a monorepo, every invalid edge is one keystroke away. If the architecture matters, enforce it where the change happens.
Make forbidden edges fail locally
ESLint can reject obvious boundary violations before CI:
module.exports = {
rules: {
"no-restricted-imports": ["error", {
patterns: [
{
group: ["@acme/*/src/*"],
message: "Import package public APIs, not deep source files."
},
{
group: ["apps/*"],
message: "Packages must not import deployable applications."
}
]
}]
}
}This is crude but effective for common mistakes. Nx module-boundary rules can express richer constraints based on project tags:
{
"enforceBuildableLibDependency": true,
"depConstraints": [
{
"sourceTag": "type:app",
"onlyDependOnLibsWithTags": ["type:feature", "type:util"]
},
{
"sourceTag": "type:util",
"onlyDependOnLibsWithTags": ["type:util"]
}
]
}The exact tags matter less than the fact that dependency direction is executable policy.
Architecture tests catch what lint misses
Some constraints are easier to test across the repository than through import parsing. dependency-cruiser can validate rules against the real graph:
depcruiser src apps packages --config .dependency-cruiser.js --output-type errExample policy:
module.exports = {
forbidden: [
{
name: "no-app-to-app",
from: { path: "^apps/[^/]+" },
to: { path: "^apps/(?!shared)[^/]+" }
},
{
name: "no-server-deps-in-ui",
from: { path: "^packages/ui" },
to: { path: "node_modules/(pg|mysql|redis)" }
}
]
}Run it in CI and locally. A rule that only runs in CI is still useful, but feedback after push is late enough that people rationalize exceptions.
Exceptions need expiry
Real systems have legitimate exceptions. The mistake is making exceptions invisible or immortal. Encode them with reviewable context:
{
name: "temporary-legacy-invoice-import",
from: { path: "^apps/web/src/legacy-invoice" },
to: { path: "^apps/api/src/internal" },
comment: "Remove after invoice API migration, tracked in PLAT-1042"
}Even better, count exceptions and fail if the count increases. Architecture policy should behave like a ratchet: temporary relief is allowed, erosion is not.
Test behavior, not just imports
Dependency rules do not catch semantic coupling. If a shared package reads environment variables intended only for one app, or a UI package assumes a global router, the import graph can look clean while behavior remains coupled. Add contract tests for package initialization, environment requirements, and side effects.
For example, a package test can assert that importing a UI library does not touch server-only modules:
test("ui package has no server-only side effects", async () => {
await expect(import("@acme/ui")).resolves.toBeTruthy()
expect(process.listenerCount("warning")).toBeLessThan(3)
})That specific test is illustrative, not universal. The durable lesson is to test the boundary behavior you actually care about.
Graph health is an operational metric
Track cycle count, forbidden-edge count, average dependency depth, and orphan packages over time. Do not gamify the numbers; use them as smoke detectors. A sudden jump after a reorg or feature push is a prompt for architectural conversation.
Field notes
- The first boundary violation is usually small and reasonable. The tenth is architecture.
- Generated barrel files can hide deep coupling by making everything look like a public API.
- Policies without ownership become lint noise. Assign boundary rules to the teams who control the architecture.
- Do not enforce purity you cannot explain. Every rule should map to a real failure mode: cache pessimism, deploy coupling, security isolation, or review ambiguity.
Was this lesson helpful?