# axle watch

Run tasks, then run them again whenever the files they read change.

```sh
axle watch <task>... [options] [-- <arguments>]
```

Runs the tasks once, the way [`axle run`](https://axlers.dev/docs/reference/run.md) does, then waits. When you save a file, Axle works out which tasks the change affects and runs only those, and the tasks that depend on them:

```text
Running build in 3 packages with pnpm, up to 16 tasks at once.

@acme/core#build  built
@acme/ui#build    built
@acme/web#build   built

3 tasks passed in 4.2s

Waiting for changes. Press Ctrl+C to stop.

Changed packages/ui/src/button.tsx
@acme/ui#build    built
@acme/web#build   built

2 tasks passed in 2.1s
```

Ctrl+C stops every task and ends watching, and Axle exits with 0.

## What runs again

After each change, Axle reads the repository and hashes every task again, exactly as `axle run` does, and runs a task when:

- its hash differs from the one it had when it last passed, because its own files, a dependency, an environment variable or the lockfile changed;
- it has [`^outputs`](https://axlers.dev/docs/reference/configuration.md#inputs) among its inputs and a task it waits for runs again;
- it failed, or was stopped, the last time it ran.

Everything else is up to date and isn't shown. Since a task's hash covers the tasks it waits for, a change reaches every task downstream. Files a task writes aren't among its inputs, so a build writing `dist` never starts another run. Tasks still come from the [cache](https://axlers.dev/docs/reference/cache.md) when they can: undo an edit, and the next run replays the earlier result.

Changes are collected until the files have been quiet for a tenth of a second, so a formatter saving twenty files, or switching branches, starts one run. A change that comes while a run is going stops that run and starts the next with everything that changed since the last one that finished.

Changing an `axle.toml`, a `package.json` or a lockfile is picked up by the next run, so a new package or task needs no restart. A mistake in one is shown, and watching goes on until it's fixed.

## Which files count

Files Git ignores never start a run, nor do `.git`, `.axle` and `node_modules`. [`[watch]`](https://axlers.dev/docs/reference/configuration.md#watch-settings) in the root `axle.toml` ignores more:

```toml
[watch]
ignore = ["**/*.log", "docs/**"]
```

A change to a file no task reads runs nothing, and Axle says so. Changes to `[watch]` itself take effect the next time you start `axle watch`.

## Test files

A task whose `arguments` hold [`{testFiles}` or `{changedFiles}`](https://axlers.dev/docs/reference/configuration.md#test-files) runs every test the first time, then gets the files of each change: save `packages/ui/src/button.ts`, and `@acme/web#test` runs the tests that import it. The files it's given are part of its hash, so a task given the same files as last time, with nothing else changed, doesn't run again. A change to a lockfile runs every test.

## Persistent tasks

A [persistent task](https://axlers.dev/docs/reference/configuration.md#persistent-tasks), such as a dev server, starts in the first run and keeps running; changes wait until the first run's other tasks have finished. Most dev servers reload on their own. One that doesn't can say `restart = true` in `axle.toml`, and `axle watch` restarts it, and says so, after a run when a file in its package changed or a task it waits for ran again:

```toml
[tasks.start]
persistent = true
restart = true
```

## Options

`axle watch` takes these options of `axle run`, which mean the same: `--filter`, `--only`, `--variant`, `--concurrency`, `--continue`, `--log-order`, `--output-logs`, `--json`, `--log-file`, `--force`, `--cache`, `--env-mode` and `-- <arguments>`. It always prints labelled lines, as `--ui stream` does, and has no `--affected`, `--dry`, `--profile`, `--summarize` or `--check-inputs`. `[run]` settings and `AXLE_*` variables apply too, and `[hooks]` commands run once before watching starts and once after it ends.

A [log file](https://axlers.dev/docs/reference/run.md#log-file) is created once when watching starts, and gets the output and the summary of every run until it ends. To run any command again on changes, rather than scripts, use [`axle exec --watch`](https://axlers.dev/docs/reference/exec.md#running-again-on-changes).

## JSON events

`--json` prints the [events of `axle run`](https://axlers.dev/docs/reference/run.md#json-events). Each run has its own `run` number, starting at 1, and every run after the first begins with the files that started it:

```json
{ "run": 2, "event": "watch-change", "files": ["packages/ui/src/button.tsx"] }
{ "run": 2, "event": "run-started", "protocol": 1, "tasks": 2, "concurrency": 16 }
```

`files` is empty when the system lost track of what changed, such as after a very large checkout; every task is checked then. Persistent tasks belong to run 1, so their events keep that number while later runs come and go, including the `task-restarted` of one with `restart = true`.
