# Caching

How Axle remembers the results of tasks, what changes a task's hash, and the commands that show and clean the cache.

```sh
axle cache status
axle cache clean [--older-than <duration>] [--remote [--yes]]
axle why miss <task>
```

Axle remembers what each task produced. When a task's inputs haven't changed since a run that passed, `axle run` puts back the files it made and prints its output again instead of running it:

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

@acme/core#build  cached, replaying output (3f9a1c27)
@acme/core#build  > tsc -p .
@acme/ui#build    cached, replaying output (b81e04d2)
@acme/ui#build    > vite build
@acme/web#build   cached, replaying output (6c0d5e93)
@acme/web#build   > next build

✓ 3 tasks passed in 0.1s, all 3 from the cache
```

After an edit in `@acme/ui`, only `@acme/ui#build` and `@acme/web#build`, which depends on it, run again.

Nothing has to be set up. The options that change how [`axle run`](https://axlers.dev/docs/reference/run.md#caching) uses the cache are `--force`, `--cache`, `--env-mode` and `--output-logs`, and [`axle.toml`](https://axlers.dev/docs/reference/configuration.md) covers what Axle can't find out by itself.

## What can be cached

- A task is stored when it passes. A failed or stopped task is never stored, so it runs again next time.
- Long-running scripts (`dev`, `start`, `serve`, `preview` and `watch`) and tasks with `cache = false` in `axle.toml` are never cached.
- Root tasks such as `//#lint` are cached, and every file in the repository is one of their inputs.
- A task that changes its own inputs, such as a formatter that rewrites source files, isn't stored, and Axle [warns](https://axlers.dev/docs/messages/task-changed-its-inputs.md) about it.

## What changes a task's hash

A task's hash covers everything that could change what it does. When any of these change, the task runs again:

| Input                  | What exactly                                                                                                                                                                                            |
| ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| The task               | its package and script, and the arguments after `--` when it was named                                                                                                                                  |
| The package's files    | every file in the package folder that Git tracks or would track, so new and edited files count before a commit; with [`inputs`](https://axlers.dev/docs/reference/configuration.md#inputs), only those it names             |
| Files outside packages | every such file, such as a root `tsconfig.json`, because Axle can't know which packages read it; with `globalInputs`, only those it names                                                               |
| `.env` files           | `.env` and `.env.*` in the package, when it uses a [framework](#frameworks) that reads them                                                                                                             |
| The lockfile           | the versions of the packages the package uses, directly and through others                                                                                                                              |
| Dependency tasks       | the hashes of the tasks it waits for, so a change reaches every task that depends on it; with `^outputs` in `inputs`, the files they wrote instead                                                      |
| Transit packages       | the files of workspace packages it uses that don't have the script, or of every package it uses when it waits for `^<task>` that no package has a script for, since their source is often read directly |
| Environment variables  | the names and values of those listed in the task's `env` or in `globalEnv`, and those of the frameworks the package uses                                                                                |
| Commands               | the output of each command in the task's [`inputs`](https://axlers.dev/docs/reference/configuration.md#command-inputs), such as `node --version`                                                                            |
| Where it runs          | the operating system, the processor type, the package manager, the version of `node` and the version of Axle                                                                                            |

A file counts by two things. One is its content as Git stores it, after the line-ending conversion of `core.autocrlf` and `.gitattributes`, so a file with Windows line endings hashes the same before and after it is committed. The other is whether it is executable: making a script executable with `chmod +x`, or with `git update-index --chmod=+x` on Windows, where the file system has no such bit, changes the hash.

Nothing in a hash depends on the machine or the folder, so the same commit on two machines with the same system and tools gets the same hashes. Axle asks `node` for its version once and remembers it in the cache folder until the program changes. The script's command is part of `package.json`, which is one of the package's files. Files Git ignores, such as `node_modules` and build output, never count, apart from the `.env` files above. When the lockfile can't be read, the whole file counts for every task, and Axle [warns](https://axlers.dev/docs/messages/lockfile-unreadable.md#when-a-run-hashes-tasks). Outside a Git repository, Axle reads every file, following `.gitignore`.

## Outputs

On a hit, Axle writes back the task's outputs exactly as the task made them, with executable bits and symbolic links, and removes the files it removed. If they are already on disk because the last run made or restored them, Axle leaves them alone.

- With `outputs` in [`axle.toml`](https://axlers.dev/docs/reference/configuration.md#tasks), the files matching those patterns are the outputs.
- Without them, Axle finds the outputs itself: the files Git ignores in the folders the task wrote to, leaving out `node_modules`, `.env` files and the cache folders of the frameworks it finds, such as `.next/cache`. So that this is exact, two such tasks of the same package don't run at the same time. Tasks of different packages still run side by side, and declaring `outputs` removes the limit.
- Files a task writes outside its package folder aren't stored, unless `outputs` names them with `{root}/`, such as `{root}/coverage/web/**`.

## Environment variables

By default, tasks run in **strict** mode: a task gets only the variables it needs to run, those listed in `axle.toml`, and those of the frameworks its package uses. Anything else is left out, so a variable can't change a build without changing its hash. If a task fails, the hint says how many variables were left out.

The variables every task gets are `PATH`, `HOME`, the user and temporary-folder variables, the system variables Windows needs (such as `SystemRoot`, `ComSpec` and `APPDATA`), `LANG` and `LC_*`, `TERM`, `TZ`, `CI`, the colour variables, the proxy variables and `SSH_AUTH_SOCK`. None of them changes the hash.

| To let a task see a variable                 | List it in `axle.toml` under                    | Changes the hash |
| -------------------------------------------- | ----------------------------------------------- | ---------------- |
| that changes what it builds, such as a URL   | the task's `env`, or `globalEnv` for every task | yes              |
| that it needs but that doesn't change output | `passThroughEnv`, or `globalPassThroughEnv`     | no               |

A name ending in `*` stands for every variable that starts with it, such as `NEXT_PUBLIC_*`. On Windows, names are matched without regard to case.

`--env-mode loose`, or `AXLE_ENV_MODE=loose`, gives tasks every variable, and still hashes only the listed ones.

Values are hashed but never stored or printed, so secrets don't end up in the cache, in reports or in `axle why miss`.

### Frameworks

Axle finds the frameworks a package uses from its dependencies, and hashes and passes on the variables they build into the code. Set `frameworkEnv = false` in `axle.toml` to turn this off.

| Framework        | Dependency          | Variables         |
| ---------------- | ------------------- | ----------------- |
| Next.js          | `next`              | `NEXT_PUBLIC_*`   |
| Vite             | `vite`              | `VITE_*`          |
| Astro            | `astro`             | `PUBLIC_*`        |
| SvelteKit        | `@sveltejs/kit`     | `PUBLIC_*`        |
| Nuxt             | `nuxt`              | `NUXT_PUBLIC_*`   |
| Gatsby           | `gatsby`            | `GATSBY_*`        |
| Create React App | `react-scripts`     | `REACT_APP_*`     |
| Vue CLI          | `@vue/cli-service`  | `VUE_APP_*`       |
| Expo             | `expo`              | `EXPO_PUBLIC_*`   |
| RedwoodJS        | `@redwoodjs/core`   | `REDWOOD_ENV_*`   |
| Sanity           | `sanity`            | `SANITY_STUDIO_*` |
| SolidStart       | `@solidjs/start`    | `VITE_*`          |
| Qwik             | `@builder.io/qwik`  | `PUBLIC_*`        |
| Hydrogen         | `@shopify/hydrogen` | `PUBLIC_*`        |
| Storybook        | `storybook`         | `STORYBOOK_*`     |

## Why a task ran

`axle why miss` says what changed since a task's last cached run in this checkout:

```text
@acme/web#build will run because its inputs changed since its last cached run, 2 hours ago:

  packages/ui/src/button.tsx changed, through @acme/ui#build
  NEXT_PUBLIC_API_URL changed
```

It names files that were added, changed or removed, variables that were set, changed or unset, commands whose output changed, such as "the output of `node --version` changed", lockfile versions such as `is-odd 3.0.0 → 3.0.1`, arguments, and changes in where the task runs. A change in a dependency task is followed down to what changed there. If the task would come from the cache, or can't, it says that instead. Name the package and the script, such as `web#build`, or `//#lint` for a root script.

## Where the cache lives

| System  | Folder                                     |
| ------- | ------------------------------------------ |
| Linux   | `$XDG_CACHE_HOME/axle`, or `~/.cache/axle` |
| macOS   | `~/Library/Caches/axle`                    |
| Windows | `%LOCALAPPDATA%\axle\cache`                |

`dir` under [`[cache]` in axle.toml](https://axlers.dev/docs/reference/configuration.md#cache-settings), or `AXLE_CACHE_DIR`, chooses another folder. Every checkout and Git worktree on the machine shares the cache, so a second clone of a repository starts with its results.

After a run, at most once a day, Axle removes entries that haven't been used for 14 days, then the least recently used ones until the cache is under 10 GB. `maxAge` and `maxSize` under `[cache]`, or `AXLE_CACHE_MAX_AGE` and `AXLE_CACHE_MAX_SIZE`, change these limits. An entry that would unpack to more than `maxSize` is [left out](https://axlers.dev/docs/messages/cache-entry-too-large.md) and its task runs.

## axle cache status

Shows where the cache is, how many entries it holds and how much space they take. Next to each setting is where it came from:

```text
Local cache
  Folder:       /home/sam/.cache/axle (default)
  Entries:      214, 86.3 MB
  Oldest use:   9 days ago
  Limits:       entries unused for 7 days are removed (axle.toml:3), then the least recently used above 10.0 GB (default)
```

## axle cache clean

Removes every entry, or with `--older-than`, only the entries not used for that long, given in days or hours such as `7d` or `12h`:

```text
Removed 37 entries from /home/sam/.cache/axle, 12.4 MB.
```

### The remote cache

`--remote` cleans the [remote cache](https://axlers.dev/docs/reference/remote-cache.md) instead. It removes the entries of the repository's team from wherever `[cache.remote]` or `AXLE_REMOTE_CACHE_URL` points: `axle-server`, an S3 bucket, an Azure container or a shared folder. Other machines lose those entries too, so it asks first:

```text
✔ Remove the entries older than 30 days of the team default from the remote cache at https://cache.example.com? Yes
Removed 1204 entries from the remote cache at https://cache.example.com, 3.1 GB.
```

`--yes` removes them without asking. Where there's no terminal to ask in, such as in CI, it's needed, or Axle [stops](https://axlers.dev/docs/messages/confirmation-needed.md) without removing anything. With `--older-than`, an entry's age is the time since it was last used on `axle-server`, and since it was stored in a bucket, a container or a shared folder.

Signed and encrypted entries are removed without their keys. With `axle-server`, the token must be one that may write, or the server [refuses](https://axlers.dev/docs/messages/remote-cache-clean-read-only.md). With a bucket or a container, the credentials need permission to list and delete.
