Sub topic 11
Enforcing boundaries with lint rules
Earlier chapters gave each feature a folder, a route file, scoped providers and a barrel file that lists its public API. All of that is convention, and conventions erode. Someone in a hurry imports ../../orders/data-access/orders.store from the customers feature, and review misses it. After a few of those, the feature edges exist only in the folder names.
This chapter turns the conventions into rules that fail the build. It assumes you know the layer types (feature, ui, data-access, util), barrels and path aliases from the previous chapters. You also need basic ESLint configuration.
What the rules must catch
Boundary violations come in a few kinds. Each kind needs a different rule.
| Violation | Example | Rule that catches it |
|---|---|---|
| Deep import past a barrel | `import { OrdersStore } from '@app/orders/data-access/orders.store'` | Restrict imports to the barrel |
| Wrong layer direction | A `ui` library imports a `data-access` library | Layer constraints by tag |
| Sideways feature coupling | Customers feature imports Orders internals | Scope constraints by tag |
| Circular dependency | `orders` imports `customers`, which imports `orders` | Cycle detection |
A violation is a dependency you have not decided to allow, so the rules follow an allow-list: each kind of code gets a short list of what it may depend on, and everything else is an error.
Dependencies only point downward in this diagram. Because nothing in util imports anything above it, you can change a feature without breaking util, and the diagram has no cycles.
Tagging code in an Nx workspace
Nx's @nx/enforce-module-boundaries rule works on tags. Each project (an app or library) carries tags in its project.json. A tag is a plain string, and the type: and scope: prefixes are a naming convention.
{
"name": "orders-feature",
"tags": ["type:feature", "scope:orders"]
}The rule is configured in your root ESLint configuration. Here is a complete .eslintrc.json that applies it to every TypeScript file:
{
"root": true,
"overrides": [
{
"files": ["*.ts"],
"rules": {
"@nx/enforce-module-boundaries": [
"error",
{
"enforceBuildableLibDependency": true,
"allow": [],
"depConstraints": [
{
"sourceTag": "type:feature",
"onlyDependOnLibsWithTags": ["type:ui", "type:data-access", "type:util"]
},
{
"sourceTag": "type:ui",
"onlyDependOnLibsWithTags": ["type:ui", "type:util"]
},
{
"sourceTag": "type:data-access",
"onlyDependOnLibsWithTags": ["type:data-access", "type:util"]
},
{
"sourceTag": "type:util",
"onlyDependOnLibsWithTags": ["type:util"]
}
]
}
]
}
}
]
}Nx workspaces created with recent versions use the flat config format (eslint.config.js) instead. The rule name and the depConstraints option are the same in both.
A project may carry several tags, and a constraint applies to every tag it has. A ui library tagged type:ui that imports a type:data-access library breaks the constraint, and lint reports an error such as "A project tagged with type:ui can only depend on libs tagged with type:ui, type:util".
Adding scope constraints
Layer constraints stop the wrong direction. They do not stop the Customers feature from reaching into Orders. For that, add scope: tags and constraints that require matching scopes:
{
"sourceTag": "scope:orders",
"onlyDependOnLibsWithTags": ["scope:orders", "scope:shared"]
},
{
"sourceTag": "scope:customers",
"onlyDependOnLibsWithTags": ["scope:customers", "scope:shared"]
}Now a library tagged scope:orders cannot import one tagged scope:customers. Code that both features need goes into a library tagged scope:shared.
A project must satisfy every constraint that matches one of its tags. A library tagged type:feature and scope:orders has to pass both the layer rule and the scope rule. That is the intended behavior, but it can confuse you when a violation message mentions a tag you were not thinking about.
What the rule also checks for free
@nx/enforce-module-boundaries does more than the constraints you write:
- Circular dependencies. It reports cycles between projects, even with an empty
depConstraintslist. - Barrel-only imports. Code may import a library only through its alias (
@app/orders), not through a relative path into another library's files. - Lazy-loading mistakes. If a library is loaded with
loadChildrenin one place and imported statically in another, the rule reports it. A static import pulls the library into the main bundle, so the lazy route no longer saves anything.
Enforcing boundaries without Nx
The Nx rule works at the level of projects. In a single Angular CLI app, your feature folders are all one project, so the rule cannot see the edges between them. You have two options.
The first is the built-in ESLint rule no-restricted-imports. Use it to forbid deep imports into another feature. This .eslintrc.json blocks the Customers feature from importing Orders internals and allows only the Orders barrel:
{
"root": true,
"overrides": [
{
"files": ["src/app/customers/**/*.ts"],
"rules": {
"no-restricted-imports": [
"error",
{
"patterns": [
{
"group": [
"**/orders/data-access/**",
"**/orders/feature/**",
"**/orders/ui/**"
],
"message": "Import from '@app/orders' instead of reaching into the feature."
}
]
}
]
}
}
]
}You write one override block per feature. It works, but it grows by hand and only covers the patterns you list.
The second option is a plugin such as eslint-plugin-boundaries. You declare element types by folder pattern (feature, ui, data-access) and then write allow rules between those types. It expresses the same layer model as the Nx tags, and the folders themselves act as the tags. The trade-off is another dependency and a pattern syntax to learn.
Choose by size. A small app with three or four features is fine with no-restricted-imports. Once you maintain many override blocks, or you are about to split into libraries, move to tags.
Making the rules stick
A rule that developers can bypass will be bypassed. Four habits keep the boundaries real:
- Run lint in CI and fail the build on errors. Lint only in an editor and the rule is advice. In Nx,
nx affected -t lintlimits the run to projects touched by the change. - Use
error, notwarn. Warnings pile up until nobody reads them. - Treat
eslint-disablefor this rule as a review item. A disable comment on a boundary rule is a design decision, so it needs a reason in the pull request. - Adopt the rules on a clean tree, or record the existing violations. Turning the rule on in a messy codebase gives hundreds of errors on day one. See the migration chapter for the incremental approach.
Common errors
- Tags are missing. A project with no tags matches no constraint, so it is not restricted at all. Add a check that every project has both a
type:and ascope:tag. - The alias points at the wrong file. If
@app/ordersmaps to a file other than the barrel, imports pass the rule but expose too much. Check thepathsentries from the previous chapter. - The barrel exports too much. The rule allows anything the barrel exports. A barrel that re-exports every file makes the boundary meaningless, so keep it to the routes and the types others need.
- Overly broad
allowlists. Theallowoption exempts imports from the rule. Use it for a deliberate, temporary exception, and keep it short. - Constraints that match nothing. A typo such as
type:data_accessis not an error. The constraint just never applies, so test each constraint by adding a deliberately illegal import once and confirming that lint fails.
Exercise
Your workspace has a library tagged type:ui and scope:orders. A developer adds an import in it from a library tagged type:data-access and scope:orders. Using the constraints in this chapter, does lint fail, and which constraint reports it?
Show answer
Lint fails. The scope constraint passes, because both libraries are in scope:orders. The layer constraint for type:ui fails, because it allows dependencies only on type:ui and type:util libraries and the imported library is tagged type:data-access.
Exercise
You have a single Angular CLI app, not an Nx workspace, with src/app/orders and src/app/customers folders. Which ESLint rule can stop code in customers from importing orders/data-access/orders.store, and what must you do to allow the Orders barrel at the same time?
Show answer
Use no-restricted-imports with a patterns entry in an override for src/app/customers/**/*.ts. List the internal folders in group (for example **/orders/data-access/**). Do not list the barrel path @app/orders, so imports through it stay allowed.