Boundary configuration
Find the boundaries config, with zones, import rules, presets, public entry files, zone coverage, forbidden calls and accepted cycles.
Your team agreed on an architecture: the report code and the service code use the shared helpers, and they do not use each other. Nothing in the build stops a new import that breaks the agreement, so the layers slowly grow together. The boundaries key of the config file writes the agreement down, and fallow checks each import against it. Architecture explains the analysis and how to act on a finding.
In the demo project, this config gives the three parts of src a zone each:
// .fallowrc.json
{
"boundaries": {
"zones": [
{ "name": "services", "patterns": ["src/services/**"] },
{ "name": "reports", "patterns": ["src/reports/**"] },
{ "name": "shared", "patterns": ["src/lib/**", "src/utils/**"] }
],
"rules": [
{ "from": "services", "allow": ["shared"] },
{ "from": "reports", "allow": ["shared"] },
{ "from": "shared", "allow": [] }
]
}
}
fallow architecture reports each import that a rule does not allow as a boundary-violation finding:
● Boundary violations (2)
src/reports/invoice.ts:1 → src/services/orders.ts (reports → services)
src/reports/receipt.ts:1 → src/services/orders.ts (reports → services)
Both invoice and receipt code import from src/services/orders.ts, and no rule allows reports to import services. Type-only imports shows how to accept these two imports. The boundaries reference has the type of each key.
Zones
Each rule names zones, so the zones come first. A zone is a named set of files. Each entry of zones has these keys:
| Key | Content |
|---|---|
name | The name that rules use. |
patterns | Globs of the files in the zone, relative to the project root or to root. |
autoDiscover | Directories. Each child directory becomes its own zone. See Auto-discovered zones. |
root | A subdirectory. patterns and autoDiscover are then relative to it. |
publicFiles | Globs of the files that other zones may import. See Public entry files. |
- A file belongs to the first zone with a matching pattern. Put a specific zone before a broad zone.
- A file that matches no zone has no restrictions. It can import each zone, and each zone can import it.
- The patterns follow the glob rules.
- A zone that matches no file gives the warning
boundary zone '<zone>' matched 0 files.
One zone for each package
In a monorepo, give each package a zone with root. The patterns and the publicFiles of the zone are then relative to the package directory:
// .fallowrc.json
{
"boundaries": {
"zones": [
{ "name": "ui", "root": "packages/ui", "patterns": ["src/**"], "publicFiles": ["src/index.ts"] },
{ "name": "web", "root": "apps/web", "patterns": ["src/**"] }
],
"rules": [{ "from": "web", "allow": ["ui"] }]
}
}
An import from apps/web into packages/ui/src/internal/x.ts is then a finding with reason: "internal_file". Public entry files explains this reason.
packages/app,packages/app/and./packages/app/are the sameroot. Fallow also changes\to/. A file outside therootdirectory never matches the zone.- Do not repeat the root in the patterns.
root: "packages/app"withpatterns: ["packages/app/src/**"]stops the config load withFALLOW-BOUNDARY-ROOT-REDUNDANT-PREFIX. Writepatterns: ["src/**"].
Rules
With the zones in place, the rules say which imports between them are allowed. Each entry of rules restricts one zone:
| Key | Content |
|---|---|
from | The zone that imports. |
allow | The zones that from may import. |
allowTypeOnly | Zones that from may import types from. See Type-only imports. |
- A file can always import a file of its own zone.
- A zone without a rule has no restrictions.
- A rule with an empty
allowlist isolates the zone. It can import no other zone. - The rules apply to each analyzed file, also to a file that no entry point reaches.
- An import that does not run its target is not checked, for example
require.resolve("./x")or a webpack loader request such asraw-loader!./x.js. - Start with the rules of the lowest zones, the zones that may import nothing. Then add the rules of the zones above them.
Re-export chains
A named or default import through a barrel file counts against the zone of the module that declares the name.
to_pathandto_zonename the module that declares the name.via_pathnames the barrel that the file imports, and the human output shows it asvia <path>.- Fallow reports one finding for each importing file and declaring module. A direct import of the declaring module wins over an import through a barrel.
- When a re-export in the barrel breaks a rule of the barrel zone, only the barrel gets a finding.
- A namespace import (
import * as x) or a side-effect import counts against its direct target. - The chain stops at the first public entry file of a zone.
Type-only imports
The two findings at the top of this page come from the same first line in invoice.ts and receipt.ts: import type { Order } from "../services/orders". The report code needs the shape of an order, and it does not call the services. allowTypeOnly allows imports of types from a zone that is not in allow. TypeScript removes these imports, so they add no runtime dependency.
// .fallowrc.json, in "rules"
{ "from": "reports", "allow": ["shared"], "allowTypeOnly": ["services"] }
With this rule, fallow architecture reports no finding in the demo project. An import of a value from services, such as a function call, is still a finding:
Allowed by allowTypeOnly | Not allowed |
|---|---|
import type { Foo } from "..." | import { type Foo, Bar } from "...": Bar is a value |
import type * as ns from "..." | import { Foo } from "..." |
import { type Foo } from "...", when each name has type | import "...": a side-effect import |
export type { Foo } from "..." |
A type-only import is allowed when its target zone is in allow or in allowTypeOnly. No preset sets allowTypeOnly.
Public entry files
A rule allows a zone, but it does not say which files of that zone are the public interface. Without that limit, another zone can import an internal file, and a change to that file breaks code far away. publicFiles lists the files of a zone that other zones may import, for example an index.ts:
// .fallowrc.json
{
"boundaries": {
"zones": [
{ "name": "billing", "patterns": ["src/modules/billing/**"], "publicFiles": ["src/modules/billing/index.ts"] },
{ "name": "orders", "patterns": ["src/modules/orders/**"] }
],
"rules": [{ "from": "orders", "allow": ["billing"] }]
}
}
- An import from another zone into a file that is not public is a finding with
reason: "internal_file". Thepublic_filesfield names the files to import. - An import that a rule does not allow is a finding without
reason, also when the target is public. - A re-export chain stops at a public file. Fallow judges the import by the public file.
- The check applies to imports from each other zone, also from a zone without a rule. Files outside each zone are not checked.
- When no file of a zone matches its
publicFilesglobs, fallow writes a warning. - The globs use the same base as
patterns: the project root, orrootwhen the zone sets it. A zone can have more than one glob, for example["index.ts", "*.routes.ts"]. - On an
autoDiscoverzone, each glob is relative to each discovered directory.["index.ts"]gives each child zone its ownindex.ts. The files that only thepatternsof such a zone match get no public entry files.
Auto-discovered zones
A project with many features needs one zone for each feature, and a new feature must not need a config change. autoDiscover makes each child directory its own zone, named <zone>/<directory>:
// .fallowrc.json
{
"boundaries": {
"zones": [
{ "name": "features", "patterns": ["src/features/**"], "autoDiscover": ["src/features"] },
{ "name": "shared", "patterns": ["src/shared/**"] }
],
"rules": [{ "from": "features", "allow": ["shared"] }]
}
}
With src/features/auth and src/features/billing, the zones are features/auth and features/billing. An import from one feature into another is then a finding.
- A rule for the parent zone applies to each child zone. A rule for a child zone, such as
from: "features/auth", replaces it for that child. - The parent zone may import its child zones, so a barrel file in
src/featurescan export each feature. - When the zone also has
patterns, the files directly insrc/featuresbelong to the parent zone. Withoutpatterns, these files are in no zone.
Presets
When your project follows a known architecture, you do not need to write each zone. preset gives a set of zones and rules for a common architecture:
// .fallowrc.json
{ "boundaries": { "preset": "bulletproof" } }
| Preset | Zones | Rules |
|---|---|---|
layered | presentation, application, domain, infrastructure | presentation may import application. application may import domain. infrastructure may import domain and application. domain is isolated. |
hexagonal | adapters, ports, domain | adapters may import ports. ports may import domain. domain is isolated. |
feature-sliced | app, pages, widgets, features, entities, shared | Each layer may import each layer below it in this list. |
bulletproof | app, features (auto-discovered), shared, server | app may import features, shared and server. features may import shared and server. server may import shared. shared is isolated. |
deep-modules | modules (auto-discovered), app, tests | No rules. Other zones import a module only through its index file. See Keep a module deep. |
A preset zone has the pattern <root>/<zone>/**. The shared zone of bulletproof covers components, hooks, lib, utils, utilities, providers, shared, types, styles and i18n. <root> is the compilerOptions.rootDir of the tsconfig.json in the project root. When that value is missing, ., absolute or outside the project, <root> is src. Custom zones do not use rootDir.
The deep-modules preset discovers its modules in <root>/modules, or in <root>/features when <root>/modules does not exist or has no module folders. When neither folder exists, fallow stops with exit code 2. A zone of yours with the name modules replaces the discovery, and then no folder is necessary. Each module zone has its index file as its public file (index.{ts,tsx,mts,cts,js,jsx,mjs,cjs}). The app zone holds the other files under <root>, and the tests zone holds test/**, tests/** and e2e/**. These two zones come after your zones, so a zone of yours keeps its files.
Public files for a preset
The bulletproof and feature-sliced presets have no public files by default. Set presetPublicFiles to give each feature or slice its index file as its public file:
// .fallowrc.json
{ "boundaries": { "preset": "feature-sliced", "presetPublicFiles": true } }
bulletproof: each zone underfeaturesgetsindex.{ts,tsx,mts,cts,js,jsx,mjs,cjs}.feature-sliced: each slice ofpages,widgets,featuresandentitiesgets its index file. The@xfiles of an entity, such asentities/user/@x/order.ts, are public too.appandsharedget no public files.- With the
layeredorhexagonalpreset, or without a preset, the key stops the run with exit code 2. Thedeep-modulespreset always has public files.
A fallow version without this key ignores it and reports the preset without public files. To make such a version stop with a message, set minimumVersion.
Merge a preset with your zones
A preset rarely matches the folders of a real project in each detail. You can set preset together with zones and rules:
- A zone with the name of a preset zone replaces the preset zone. It takes the place of your zone in the list.
- A rule with the
fromof a preset rule replaces the preset rule. - Other zones and rules come after the preset zones and rules.
// .fallowrc.json
{
"boundaries": {
"preset": "hexagonal",
"zones": [{ "name": "domain", "patterns": ["src/core/**"] }]
}
}
This config keeps adapters, ports and the three rules, and puts the domain zone in src/core.
Zone coverage
A file in no zone has no restrictions (see Zones), so a new folder outside each zone escapes the check. coverage reports the files that are in no zone:
| Key | Content |
|---|---|
requireAllFiles | When true, each analyzed source file outside each zone is a boundary-coverage finding. Default false. |
allowUnmatched | Globs of the files that may stay outside each zone, for example generated code. |
// .fallowrc.json
{
"boundaries": {
"zones": [{ "name": "domain", "patterns": ["src/domain/**"] }],
"coverage": { "requireAllFiles": true, "allowUnmatched": ["src/generated/**"] }
}
}
Coverage checks each analyzed file, also a file that no entry point reaches. For generated code, use allowUnmatched and not a suppression comment in each file: the config keeps the exception in one place. Use coverage together with forbidden calls, so that no file escapes a call policy.
Forbidden calls
An import rule cannot stop a call to a global, such as process.exit, because the code imports nothing. calls.forbidden bans calls in the files of a zone:
// .fallowrc.json
{
"boundaries": {
"zones": [{ "name": "domain", "patterns": ["src/domain/**"] }],
"calls": {
"forbidden": [
{ "from": "domain", "callee": "child_process.*" },
{ "from": "domain", "callee": ["console.*", "process.exit"] }
]
}
}
}
Each matching call or new expression is a boundary-call-violation finding. The finding names the callee, the pattern and the zone. A new expression has the prefix new in callee, for example new Date for the pattern Date.
- A pattern compares whole name segments.
fetchmatchesfetchand notmyfetch. object.*matches each member of the object.*.membermatches the member on each object.- A pattern follows imports:
child_process.*also matchesexecSyncfromimport { execSync } from "node:child_process", the barechild_processspecifier, a namespace import and a default import. - Fallow matches direct callee paths, also with
?.. It does not follow a callee in a variable (const run = cp.exec; run()), a computed member or an injected object (this.client.exec()). - The policy applies only to files in a zone, also to a file that no entry point reaches. Use
coverage.requireAllFilesto put each file in a zone. - Each call site is one finding, so
Date()andnew Date()on two lines give two findings. Afallow-ignore-next-linecomment covers only the call site on its next line. - When two entries match a call, the first entry wins. A bare
*pattern stops the config load.
Severity and suppression
The import findings, the coverage findings and the call findings all use the boundary-violation rule. Its default is error. The boundary-violation token in a suppression comment hides each of these findings on the line or in the file. The tokens boundary-call-violation and boundary-call-violations do the same.
Because all boundary findings share one rule, "boundary-violation": "warn" also makes the import findings warnings. Use it while you triage the existing findings, then set the rule back to error.
Common patterns
A router that imports features
In a tRPC app, the server router imports the routers of the feature modules. With the bulletproof preset, each of these imports is a server → features finding. Suppress the one file that combines the routers:
// src/server/_app.ts
// fallow-ignore-file boundary-violation
import { authRouter } from "../features/auth/router";
A rule { "from": "server", "allow": ["shared", "features"] } also removes the findings, but then each server file may import the features.
A test zone next to a preset
Fallow puts the preset zones first and your zones after them, and a file belongs to the first zone that matches. So a test zone with src/**/*.test.ts matches only test files outside the preset directories, such as src/__tests__/**. A test file such as src/features/auth/auth.test.ts stays in the features zone.
To put the test zone first, list it first and then list each preset zone again with all its patterns. A zone with the name of a preset zone replaces it and keeps the place in your list, and the preset rules stay. Run fallow list --boundaries to check the order.
Check the config
A zone with a wrong pattern holds the wrong files, and its rules then check the wrong imports. Before you trust the findings, check the zones. fallow list --boundaries prints the zones after preset expansion and merge, with the number of files in each zone. For the demo config at the top of this page:
Boundaries: 3 zones, 3 rules
Zones:
services 3 files src/services/**
reports 3 files src/reports/**
shared 6 files src/lib/**, src/utils/**
Rules:
services → shared
reports → shared
shared (isolated, no imports allowed)
Add --format json for the same data as JSON. See JSON output.
Accepted cycles
The last two keys are not part of boundaries, but they also accept a structure that you keep on purpose. circularDependencies.allow lists circular dependencies that you accept. Each entry is the list of files of one cycle, in import order, relative to the project root:
// .fallowrc.json
{ "circularDependencies": { "allow": [["src/a.ts", "src/b.ts", "src/c.ts"]] } }
The entry accepts only the cycle with these files in this order. The cycle can start at any of its files. An entry that matches no cycle gives a circular-dependencies-allow-unmatched warning. circularDependencies.allow has the limits.
Shared-state allowlist
sharedStateAllowlist removes global properties from the shared-state report of fallow architecture --shared-state. Each entry is a pair: the global object and its property. Fallow compares each pair exactly, without globs.
// .fallowrc.json
{ "sharedStateAllowlist": [["window", "analyticsQueue"]] }
Related
- Next: Architecture. Read it to decide what to do with each boundary finding.
fallow architecture: the options and output of the command.- Boundaries reference: the type of each key.