# axle boundaries

Check which projects depend on which, and that each file imports only what its package declares.

```sh
axle boundaries
axle boundaries --filter "./apps/*"
axle boundaries --json
```

A 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](https://axlers.dev/docs/reference/configuration.md#projects), and checks that each package says what it uses. Run it in CI, so a pull request that breaks a rule fails.

## Rules

Tag each project in its `axle.toml`, such as `tags = ["type:app"]`, then write rules in the root `axle.toml`:

```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](https://axlers.dev/docs/reference/configuration.md#boundaries) has every key.

Axle checks every dependency between projects:

- those in `package.json`, with the line that lists them;
- `implicitDependencies` in `axle.toml`;
- those plugins find;
- those in the imports of JavaScript and TypeScript files, with the file and line, whether or not `scanImports` is on. `implicitDependencies = ["!name"]` doesn't take an import out of these checks; `ignoreImports` does.

A broken rule is [`boundary-broken`](https://axlers.dev/docs/messages/boundary-broken.md):

```text
error: 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.
```

## Imports

Axle also reads what each file of a package imports:

- [`undeclared-import`](https://axlers.dev/docs/messages/undeclared-import.md): a file imports a package, from the workspace or npm, that neither its `package.json` nor the root's lists in `dependencies`, `devDependencies`, `peerDependencies` or `optionalDependencies`. It works today because another package brought it in, and breaks when the package manager lays things out differently.
- [`import-crosses-package`](https://axlers.dev/docs/messages/import-crosses-package.md): 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`:

```toml
[boundaries]
ignoreImports = ["src/*", "@app/*"]
```

An import `ignoreImports` matches is left out of everything: the import checks, the rules, and the dependencies [`scanImports`](https://axlers.dev/docs/reference/configuration.md#dependencies-from-imports) 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

A [plugin](https://axlers.dev/docs/reference/plugins.md#check) that can `check` adds rules of its own, such as those of the [conformance plugin](https://axlers.dev/docs/official-plugins/conformance.md): 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`](https://axlers.dev/docs/messages/plugin-rule-broken.md), naming the plugin and the rule:

```text
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

| Option       | What it does                                                                                       |
| ------------ | -------------------------------------------------------------------------------------------------- |
| `--filter`   | checks only the projects the [filters](https://axlers.dev/docs/reference/run.md#choosing-packages) choose              |
| `--affected` | checks only the projects a change [affects](https://axlers.dev/docs/reference/affected.md), 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:

```json
{
    "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.
