axle boundaries
axle boundariesaxle boundaries --filter "./apps/*"axle boundaries --jsonA repository stays easy to change when its layers depend on each other one way: apps use libraries, libraries don’t use apps, and one team’s code stays out of another’s. axle boundaries checks this with rules on tags, and checks that each package says what it uses. Run it in CI, so a pull request that breaks a rule fails.
Tag each project in its axle.toml, such as tags = ["type:app"], then write rules in the root axle.toml:
[[boundaries.rules]]tag = "type:app"allow = ["type:lib", "type:util"]
[[boundaries.rules]]tag = "scope:billing"deny = ["scope:shop"]A rule is about the projects with its tag, or every project with *. Each dependency of those projects must have one of the tags in allow, and none of those in deny. A project that several rules are about must keep all of them, allow and deny take * too, and a project without tags only matches *, so it breaks every allow without it. The configuration has every key.
Axle checks every dependency between projects:
- those in
package.json, with the line that lists them; implicitDependenciesinaxle.toml;- those plugins find;
- those in the imports of JavaScript and TypeScript files, with the file and line, whether or not
scanImportsis on.implicitDependencies = ["!name"]doesn’t take an import out of these checks;ignoreImportsdoes.
A broken rule is boundary-broken:
error: apps/web/src/admin.ts:3 imports admin, but projects tagged type:app may only depend on type:lib or type:utilhint: Move what web needs from admin into a project tagged type:lib or type:util, or change the rule in axle.toml.Imports
Section titled “Imports”Axle also reads what each file of a package imports:
undeclared-import: a file imports a package, from the workspace or npm, that neither itspackage.jsonnor the root’s lists independencies,devDependencies,peerDependenciesoroptionalDependencies. It works today because another package brought it in, and breaks when the package manager lays things out differently.import-crosses-package: a relative import, such as../../ui/src/button, reaches into another package’s folder instead of importing the package by its name.
Each undeclared package is reported once for each package that imports it, at the first file that does.
Imports of Node.js’s built-in modules, of the package itself, subpath imports such as #utils, and specifiers with a scheme such as node:fs or virtual:icons are fine, and so is a package whose @types package is declared: Axle can’t tell an import type from an import that runs, so declare the package itself too when its code runs. Specifiers that start with ~, _, / or @/, as many path aliases do, are left alone too, and so are those with a character a package’s name can’t have, such as $lib/utils. Path aliases that look like a package’s name, such as src/utils or @app/utils, need ignoreImports:
[boundaries]ignoreImports = ["src/*", "@app/*"]An import ignoreImports matches is left out of everything: the import checks, the rules, and the dependencies scanImports finds.
undeclaredImports = false under [boundaries] turns these checks off and keeps the rules. Axle reads each file once and keeps what it imports in .axle/imports.json, so later checks only read the files that changed.
Rules from plugins
Section titled “Rules from plugins”A plugin that can check adds rules of its own, such as those of the conformance plugin: every package has a README, every package.json has a license. axle boundaries asks each such plugin about the projects it checks and prints what they report as plugin-rule-broken, naming the plugin and the rule:
error: packages/ui/README.md: has no README.md (conformance/readme)hint: Add a README.md that says what the project is for, or change the rule readme under [plugins.conformance] in axle.toml.A plugin’s rule is an error or a warning. Errors count as problems; warnings are printed with warning: and don’t fail the check. If a plugin fails, axle boundaries stops, since it can’t tell whether the repository keeps its rules.
Options
Section titled “Options”| Option | What it does |
|---|---|
--filter |
checks only the projects the filters choose |
--affected |
checks only the projects a change affects, with --base and --head |
--json |
prints the problems as JSON, and exits the same way |
axle boundaries prints every problem, then how many it found, and exits with 1. With none, it prints how many projects, dependencies and imports it checked, and exits with 0. Warnings from plugins are counted beside the problems, such as “No problems and 1 warning in 3 projects…”, and don’t change the exit code. With --json it prints:
{ "formatVersion": 1, "projects": 3, "dependencies": 4, "imports": 12, "problems": [ { "code": "boundary-broken", "severity": "error", "project": "web", "file": "apps/web/src/admin.ts", "line": 3, "message": "apps/web/src/admin.ts:3 imports admin, but projects tagged type:app may only depend on type:lib or type:util", "hint": "Move what web needs from admin into a project tagged type:lib or type:util, or change the rule in axle.toml." } ]}line is null when Axle can’t name a line, such as for a dependency a plugin adds, whose file is axle.toml. severity is error or warning. A problem from a plugin’s rule has the code plugin-rule-broken, with plugin and rule naming it, and project and file empty when the plugin named neither.