# Plugins

How plugins teach Axle other kinds of projects and tasks, and the protocol they speak.

A plugin is a program, in any language, that teaches Axle what it can't see on its own. It can find projects, such as Python packages, add tasks from files those projects already have, find dependencies between projects, report on each run in another format, run hooks before and after a run, run many projects' tasks in one process, update its own settings for `axle migrate`, publish released packages to registries Axle doesn't know, keep files such as a `CODEOWNERS` in step for `axle sync`, check the repository's own rules for `axle boundaries`, and move projects to a new version of its tool, with the code changes it needs, for `axle migrate --upgrade`. Axle starts it when it needs an answer and talks to it in JSON over standard input and output.

## Adding a plugin

`axle add` finds a plugin, downloads it if it has to, and writes it into the root `axle.toml`:

```sh
axle add python
axle add npm:axle-plugin-cobol@1.4.0
axle add https://example.com/axle-reports.tar.gz
axle add ./tools/axle-services
```

It takes the name of an [official or community plugin](#finding-plugins), an npm package, with or without a version, the `https://` address of a `.tar.gz`, or a folder in the repository. It uses the name the plugin gives itself in its `axle-plugin.toml`, or `--name`, and keeps the rest of `axle.toml` as it was. A name `axle.toml` already has is an [error](https://axlers.dev/docs/messages/plugin-already-added.md). For an npm package without a version it records the latest one, and for anything it downloads it records the hash:

```toml
[plugins.python]
source = "npm:@axle-rs/plugin-python@0.1.0"
hash = "sha256-9f2c…"
```

## Using a plugin

Each plugin the repository uses has a table under `[plugins]` in its root `axle.toml`. `axle add` writes it, or you write it yourself:

```toml
[plugins.services]
source = "./tools/axle-services"
timeout = 60
language = "go"
```

| Key       | Meaning                                                                                                                                    |
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
| `source`  | where the plugin is: a folder in the repository, relative to the root, `npm:<package>@<version>`, or the `https://` address of a `.tar.gz` |
| `hash`    | for an npm package or an address, the SHA-256 of what Axle downloads, as `sha256-<hex>`                                                    |
| `timeout` | how many seconds the plugin may take to answer each question before Axle stops it; 30 by default                                           |
| any other | the plugin's own settings, passed to it as they are                                                                                        |

A downloaded plugin is fetched the first time a computer needs it, checked against `hash` before anything of it is unpacked or run, and kept in `plugins` inside Axle's [cache folder](https://axlers.dev/docs/reference/cache.md), so each version is downloaded once per computer. A plugin with a different hash [isn't run](https://axlers.dev/docs/messages/plugin-hash-mismatch.md). npm packages come from the registry in `npm_config_registry` when it's set, or from npmjs.com.

Then everything works as before. The projects a plugin finds join the task graph, the cache, `--affected` and filters like any other; `axle list` shows them with the kind `plugin`, and `axle show` says which settings came from the plugin. Plugins are only listed in the repository's own `axle.toml`, never in a preset, since they run with your permissions.

A project's own `axle.toml` and the root one win over what a plugin says: a plugin sets a task's defaults, and the repository changes them as it would a script's. A project a plugin finds can't take the name or folder of a package or another project; that's an [error](https://axlers.dev/docs/messages/plugin-project-conflict.md).

### When plugins run

A plugin that finds projects, tasks or dependencies is asked while Axle reads the repository, for every command. A plugin that reports or has hooks is also started for each `axle run`, except `--dry`, and hears about the run as it happens. A plugin that can batch is started the first time one of its [batches](#batch-mode) is ready. A plugin that can `sync` is started for each `axle sync`, one that can `check` for each `axle boundaries`, and one that can `upgrade` for each `axle migrate --upgrade`.

A plugin's answers are kept in `.axle/plugins/<name>.json`. Axle asks again only when one of the files the plugin said it reads changes, or its settings, any file in its folder, built ones its `.gitignore` leaves out included, the projects Axle knows or the version of Axle do. So a plugin costs nothing until something it depends on changes.

The versions a plugin reports in a project's lockfile are hashed like a JavaScript lockfile: a new version of a dependency misses the cache of the tasks of every project that uses it.

When a plugin is missing, can't start, crashes, takes too long or answers something Axle can't use, Axle names the plugin and shows the end of what it printed to standard error. Each has its page under Messages, starting at [plugin-not-found](https://axlers.dev/docs/messages/plugin-not-found.md). What happens next depends on the command:

- Commands that only read the repository carry on without that plugin's projects, tasks and dependencies, with the [warning](https://axlers.dev/docs/messages/plugin-skipped.md) `Axle carried on without the plugin`: `axle list`, `axle graph`, `axle show`, `axle cache`, `axle remote`, the editors through `axle daemon` and `axle lsp`, and the MCP server's tools that read. The language server shows the warning on the plugin's table in the root `axle.toml`. `axle plugins` never starts a plugin.
- Commands whose answer would be wrong without the plugin stop: `axle run`, `axle watch`, `axle affected`, `--affected` with any command, `axle boundaries`, `axle sync`, `axle prune`, `axle release`, `axle generate` and `axle why miss`. A project that silently went missing could make CI pass when it shouldn't.

A plugin that fails while it runs a batch fails that batch's tasks with the same message instead, and the run carries on as it would after any failed task.

### Batch mode

Some tools are much faster run once for many projects than once for each: a test runner that is slow to start, or a compiler that keeps its cache warm. A plugin that can `batch` sets `batch = true` on the tasks it adds, and during `axle run` the tasks of that plugin that are ready at the same moment go to it together:

- Axle checks each task's cache first. Those from the cache are done, and the rest go to the plugin in one request.
- The plugin runs them however it likes and sends each line each task prints. Axle shows, caches and reports every task on its own, as if it had run alone.
- A batch counts as one task against [`--concurrency`](https://axlers.dev/docs/reference/run.md#options). Axle starts the plugin the first time it needs it and keeps it for the rest of the run, starting another when two batches run at once. A batch has no time limit, and stopping the run stops the plugin and everything it started.
- If the plugin fails, every task of the batch fails with its message.

A task is only batched while its command is still the plugin's. Give it another `command` in `axle.toml`, set `batch = false`, or make it `persistent`, `interactive` or `exclusive`, and it runs on its own like any task. The tasks of a batch run with the plugin's environment, apart from `setEnv`; the variables in `env` still decide their hashes.

## Finding plugins

`axle plugins` lists the official plugins, those the community publishes, with the generators each ships, and the ones the repository uses. `--json` prints the same for other tools, with `used` on each listed plugin. [Plugins](https://axlers.dev/docs/plugins.md) shows both lists on the website. A community plugin is listed by opening a pull request that adds it to [`plugins/community/plugins.json`](https://github.com/axle-rs/axle/blob/main/plugins/community/plugins.json).

## Dependencies from imports

Axle doesn't need a plugin to find dependencies in JavaScript and TypeScript code. With `scanImports` in the root `axle.toml`, it reads the `import`, `export … from` and `require` of every `.js`, `.jsx`, `.mjs`, `.cjs`, `.ts`, `.tsx`, `.mts` and `.cts` file, and adds a dependency on each workspace package a file imports by name, or by a relative path into its folder:

```toml
scanImports = true
```

Files Git ignores and `node_modules` are skipped. What each file imports is kept in `.axle/imports.json`, so a file is only read again after it changes. `implicitDependencies = ["!name"]` in a package's `axle.toml` removes a dependency the scan found, and `ignoreImports` under `[boundaries]` lists specifiers it leaves out.

## Writing a plugin

The [guide to writing a plugin](https://axlers.dev/docs/extending-axle/writing-a-plugin.md) starts one in TypeScript with `@axle-rs/plugin`, which speaks the protocol for you. In any other language, a plugin is a folder with an `axle-plugin.toml`:

```toml
name = "services"
protocol = 1
command = ["node", "index.mjs"]
```

| Key          | Meaning                                                                                                                                              |
| ------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
| `name`       | the plugin's name                                                                                                                                    |
| `protocol`   | the version of the protocol it speaks, `1`                                                                                                           |
| `command`    | the program and its arguments, run in the plugin's folder; a program with a `/` is a path inside the folder; optional for a plugin with `generators` |
| `generators` | a folder inside the plugin with one [generator](https://axlers.dev/docs/extending-axle/writing-a-plugin.md#generators) in each subfolder; optional                       |

A plugin needs a `command`, `generators`, or both. One with only `generators` runs no program: Axle reads its generators from the folder and never starts it.

## The protocol

Axle and a plugin speak [JSON-RPC 2.0](https://www.jsonrpc.org/specification), one message per line on standard input and output. Axle sends requests and the plugin answers each in turn. A plugin writes nothing else to standard output; its logs go to standard error, which Axle shows when it fails. An answer with an `error` stops Axle with the plugin's [message](https://axlers.dev/docs/messages/plugin-failed.md).

Fields are added to the protocol without a new version, so a plugin ignores fields it doesn't know, and so does Axle. Paths are relative to the repository root and use `/`.

### initialize

The first request.

```json
{ "protocol": 1, "axle": "0.1.0", "root": "/home/me/repo", "settings": { "language": "go" } }
```

The plugin answers with the protocol version it speaks, what it can do and the files its answers depend on, as globs from the root:

```json
{ "protocol": 1, "capabilities": ["discover", "tasks"], "watch": ["services/*/service.json"] }
```

A plugin that reads a file it doesn't list in `watch` gives answers that go stale until a file it lists changes.

### discover

Asked when the plugin can `discover`. `projects` are the packages and projects Axle already knows, as `{ name, folder }`, so the plugin's projects can depend on them. The plugin answers with the projects it found:

```json
{
    "projects": [
        {
            "name": "api",
            "folder": "services/api",
            "dependencies": ["web"],
            "lockfile": [{ "name": "fastapi", "version": "0.135.1" }]
        }
    ],
    "lockfiles": ["services/uv.lock"]
}
```

`dependencies` are names of projects Axle knows or this plugin found, and `lockfile` the versions of the project's dependencies, both optional. `lockfiles`, also optional, names the files those versions came from. Axle leaves them out of the files every task reads, since it already hashes each project's versions, and `--affected` counts a change to one against every project of the plugin.

### tasks

Asked when the plugin can `tasks`, with every project Axle knows, those from `discover` included. The plugin answers with tasks to add:

```json
{
    "tasks": [
        {
            "project": "api",
            "name": "build",
            "command": "uv build",
            "settings": { "outputs": ["dist/**"] }
        }
    ]
}
```

`settings` takes the keys of a task in `axle.toml`, such as `description`, `dependsOn`, `inputs`, `outputs`, `env`, `persistent` and `cache`, with the same names. A plugin that adds test tasks can set `arguments` with `{testFiles}`, `fallbackArguments` and `testFiles`, so they [run only the tests a change reaches](https://axlers.dev/docs/crafting-your-repository/running-only-affected-tests.md). A `description` says what the task does wherever Axle lists tasks; the repository's own `axle.toml` can replace it.

### dependencies

Asked when the plugin can `dependencies`, with every project Axle knows. The plugin answers with dependencies between them that their manifests don't show, such as those it found in the source code:

```json
{ "dependencies": [{ "from": "web", "to": "api" }] }
```

`from` depends on `to`, so `from`'s tasks wait for `to`'s. Both are names of projects Axle knows.

### migrate

Asked by [`axle migrate`](https://axlers.dev/docs/reference/migrate.md) when the plugin can `migrate`, with its settings from `[plugins.<name>]` in `axle.toml`, without `source`, `hash` and `timeout`:

```json
{ "settings": { "python": "3.13" } }
```

The plugin answers with its settings as this version of it reads them, and a line for each change it made:

```json
{ "settings": { "version": "3.13" }, "changes": ["python is now version"] }
```

With no changes, Axle leaves the settings alone. Otherwise it writes the new settings back into `[plugins.<name>]`, keeping `source`, `hash` and `timeout` and the order of the keys that stay, and prints each change. A setting can't be called `source`, `hash` or `timeout`, or be `null`, which TOML can't write. A plugin should answer the same settings with no changes once they're migrated, so running `axle migrate` again changes nothing.

### publish

Asked by [`axle release publish`](https://axlers.dev/docs/reference/release.md#publishing) when the plugin can `publish`, after Axle has published to the npm registry, with every package whose version is tagged at the commit:

```json
{
    "packages": [{ "name": "api", "version": "1.4.0", "folder": "services/api", "private": false }],
    "dryRun": false
}
```

The plugin publishes them wherever it knows how, such as crates.io or a container registry, skipping `private` packages and versions that are already there, so it can run again after a failure. With `dryRun`, it publishes nothing and says what it would do. It answers with a line for each thing it did, which Axle prints:

```json
{ "published": ["Pushed ghcr.io/acme/api:1.4.0"] }
```

### sync

Asked by [`axle sync`](https://axlers.dev/docs/reference/sync.md) when the plugin can `sync`, with every project Axle knows, its kind, tags and dependencies:

```json
{
    "projects": [
        {
            "name": "web",
            "folder": "apps/web",
            "kind": "package",
            "tags": ["type:app"],
            "dependencies": ["ui"]
        }
    ]
}
```

`kind` is `package`, `project` or `plugin`. The plugin answers with the files it wants, each with its path from the root and its whole contents:

```json
{ "files": [{ "path": ".github/CODEOWNERS", "contents": "# Written by Axle
/apps/web/ @acme/web
" }] }
```

`axle sync` writes the files whose contents differ, beside the `tsconfig.json` references of `typescriptReferences`, `--dry-run` shows the diff, and `--check` fails while one is out of date. A path is checked as strictly as a generator's: one that leaves the repository, goes into `.git` or has a name Windows can't use is a [mistake](https://axlers.dev/docs/messages/plugin-answer-has-a-mistake.md). Two that want the same file [stop](https://axlers.dev/docs/messages/sync-files-conflict.md) `axle sync` before it writes anything.

### check

Asked by [`axle boundaries`](https://axlers.dev/docs/reference/boundaries.md) when the plugin can `check`, with the projects it checks, chosen by `--filter` or `--affected`, as `sync` sends them. The plugin answers with the rules they break:

```json
{
    "violations": [
        {
            "rule": "readme",
            "message": "has no README.md",
            "project": "ui",
            "file": "packages/ui/README.md",
            "severity": "warning",
            "hint": "Add a README.md that says what the project is for."
        }
    ]
}
```

`rule` and `message` are needed; `project`, `file`, `line`, `severity` and `hint` are optional. `severity` is `error` by default, or `warning`. `project` must be a project Axle sent, and `file` a file inside the repository. Axle leaves out violations of projects it didn't choose, and prints the rest as [`plugin-rule-broken`](https://axlers.dev/docs/messages/plugin-rule-broken.md).

### upgrade

Asked by [`axle migrate --upgrade`](https://axlers.dev/docs/reference/migrate.md#upgrading-tools) when the plugin can `upgrade`, with every project Axle knows, as `sync` sends them. The plugin moves the projects that use its tool to the version it supports: it answers with each file it changes, with its whole new contents, and a line for each change, which Axle prints after `plugin <name>:`:

```json
{
    "files": [{ "path": "apps/web/package.json", "contents": "{
  \"name\": \"web\", …" }],
    "changes": ["web: next ^15.3.0 is now ^16.0.0"]
}
```

The files can be any in the repository: a `package.json` with new versions, and the code and settings the new version reads differently, as a codemod would change them. Axle writes the files whose contents differ, `--dry-run` shows the diff and `--check` fails while one would change. Paths are checked as `sync`'s are, and two plugins that change the same file [stop](https://axlers.dev/docs/messages/upgrade-files-conflict.md) Axle before it writes anything. A plugin should answer no files once the projects are upgraded, so running `axle migrate --upgrade` again changes nothing. Axle doesn't install anything: it tells the user to install the dependencies when a `package.json` changed.

### A run

A plugin that can `report` or has `hooks` is started again for each `axle run`, and initialized the same way. Then:

1. **`before`** `{ tasks }`, when it has `hooks`: the names of the tasks about to run, such as `web#build`. The plugin answers `null` when it's done; an error stops the run before any task starts.
2. **`event`**, when it can `report`: a notification for each [run event](https://axlers.dev/docs/reference/run.md#json-events), exactly as `axle run --json` prints it. It gets no answer.
3. **`finish`** `{}`, when it can `report`: the run is over. The plugin writes its report and answers with the files it wrote, `{ "files": ["reports/junit.xml"] }`.
4. **`after`** `{ tasks, status, results }`, when it has `hooks`: `status` is `passed`, `failed` or `stopped`, and `results` lists each task that finished as `{ task, status, durationMs }`. The plugin answers `null`.

An error in `finish` or `after` is a warning: the run has already happened.

[`axle watch`](https://axlers.dev/docs/reference/watch.md) keeps one such session for all its runs: `before` gets the tasks of the first run, `event` carries every run's events, each with its `run` number and with `watch-change` before each run after the first, and `finish` and `after` come once watching ends, with the latest result of each task.

### run

Asked during `axle run` when the plugin can `batch`, with the [tasks it runs together](#batch-mode). Axle starts the plugin for this on its own and keeps it until the run ends, so it may be asked many times, and starts another copy when two batches run at once:

```json
{
    "tasks": [
        {
            "task": "web#test",
            "project": "web",
            "folder": "apps/web",
            "command": "vitest run",
            "arguments": ["--coverage"],
            "env": { "NODE_ENV": "test" }
        }
    ]
}
```

`command` is the task's command and `arguments` those it is given, `env` its `setEnv`, and `folder` where it runs. While it works, the plugin sends a notification `output` for each line a task prints, `{ "task": "web#test", "stream": "stdout", "line": "✓ 12 tests" }`, with `stream` `stdout` or `stderr`. Then it answers with each task's exit code, `0` when it passed:

```json
{ "results": [{ "task": "web#test", "exitCode": 0 }] }
```

An answer with an `error`, or one that leaves a task out, fails every task of the batch.

### shutdown and exit

Axle sends the request `shutdown`, then the notification `exit`, and the plugin exits.
