# axle affected

See which packages a change affects, and why.

```sh
axle affected [--json] [--exit-code [<package>...]] [--base <ref>] [--head <ref>]
axle affected --files [--task <task>] [--json] [--base <ref>] [--head <ref>]
axle why affected <package, task or file> [--base <ref>] [--head <ref>]
```

`axle affected` lists the packages a change affects: the packages whose files changed, and every package that depends on them. It's what [`axle run --affected`](https://axlers.dev/docs/reference/run.md#affected-packages) runs in, so a pull request only builds and tests what it touches.

```text
Comparing with main: 3 files changed in 1 package, which 2 others depend on.

@acme/ui    changed
@acme/web   depends on @acme/ui
@acme/docs  depends on @acme/ui
```

`axle why affected` explains one package:

```text
@acme/web is affected because @acme/ui changed, and @acme/web depends on it:

  @acme/web → @acme/ui

Changed in @acme/ui:
  packages/ui/src/button.tsx
  packages/ui/package.json
```

For a package that isn't affected, it says so and what was compared.

Given one package's task, such as `@acme/web#build`, it explains the task instead, naming the input that matched:

```text
@acme/web#build is affected because packages/web/src/page.tsx changed, and its input "production" matches it.
```

Given a file's path from the repository root, it follows the file's imports to the change:

```text
apps/web/src/button.test.tsx is affected because packages/ui/src/button.ts changed, and it imports it:

  apps/web/src/button.test.tsx → @acme/ui → packages/ui/src/button.ts
```

## Options

| Option                       | What it does                                                                                           |
| ---------------------------- | ------------------------------------------------------------------------------------------------------ |
| `--base <ref>`               | compares with this branch, tag or commit instead of the default                                        |
| `--head <ref>`               | compares with this commit instead of the files in your working tree                                    |
| `--json`                     | prints the list as JSON, for other tools to read (`axle affected`)                                     |
| `--exit-code [<package>...]` | exits with 1 when any package, or any package named, is affected, and 0 when none is (`axle affected`) |
| `--files`                    | lists the test files the change reaches in each affected package instead (`axle affected`)             |
| `--task <task>`              | the task whose test files `--files` lists; `test` by default                                           |

## Test files

`--files` lists, for each affected package's `test` task, the test files the change reaches through their imports, which is what a task with [`{testFiles}`](https://axlers.dev/docs/reference/configuration.md#test-files) in its arguments gets from `axle run --affected`. `--task` names another task.

```text
Comparing with main: 2 files changed in 1 package, which 1 other depends on.

@acme/ui#test  every test, since packages/ui/README.md changed, and none of its test files import it, directly or through other files

@acme/web#test  1 test file the change reaches
  apps/web/src/button.test.tsx
```

A task runs every test when Axle can't account for the whole change, as [running only the tests a change reaches](https://axlers.dev/docs/crafting-your-repository/running-only-affected-tests.md#when-every-test-runs) explains, and the line says why. With `--json`:

```json
{
    "formatVersion": 1,
    "base": "main",
    "head": null,
    "task": "test",
    "tasks": [
        {
            "task": "@acme/ui#test",
            "package": "@acme/ui",
            "folder": "packages/ui",
            "runs": "all",
            "reason": "untraced",
            "message": "packages/ui/README.md changed, and none of its test files import it, directly or through other files",
            "file": "packages/ui/README.md"
        },
        {
            "task": "@acme/web#test",
            "package": "@acme/web",
            "folder": "apps/web",
            "runs": "some",
            "tests": [
                {
                    "file": "apps/web/src/button.test.tsx",
                    "reaches": ["packages/ui/README.md", "packages/ui/src/button.ts"]
                }
            ],
            "changed": ["packages/ui/README.md", "packages/ui/src/button.ts"]
        }
    ],
    "warnings": []
}
```

`runs` is `some` or `all`. With `some`, `tests` lists the test files with the changed files each reaches, and `changed` the changed files the task reads. With `all`, `reason` is one of `not-compared`, `root`, `everything`, `lockfile`, `nothing-read`, `untraced`, `deleted` and `too-long`, `message` says it in words, and `file` names the file for `untraced` and `deleted`. Paths are from the repository root.

## Deciding whether to deploy

`--exit-code` exits with 1 when any package is affected and with 0 when none is. Given package names, it exits with 1 only when one of them is affected:

```sh
axle affected --exit-code @acme/web
```

That's the convention hosting services use to skip a build: Vercel's Ignored Build Step and Netlify's `ignore` command both build on 1 and skip on 0. An error, such as an unknown package or a base that isn't there, also exits with 1, and a clone too shallow to compare counts every package as affected, so the build goes ahead whenever Axle can't tell.

These commands run before dependencies are installed, so run Axle by its package name, with the version in your `package.json`, and compare with the commit of the last deploy. When there isn't one, such as on a branch's first deploy, the first test fails, which exits with 1 and builds:

```sh
# Vercel
[ -n "$VERCEL_GIT_PREVIOUS_SHA" ] && AXLE_SCM_BASE=$VERCEL_GIT_PREVIOUS_SHA npx @axle-rs/axle@0.1.0 affected --exit-code @acme/web
```

```sh
# Netlify
[ -n "$CACHED_COMMIT_REF" ] && [ "$CACHED_COMMIT_REF" != "$COMMIT_REF" ] && AXLE_SCM_BASE=$CACHED_COMMIT_REF npx @axle-rs/axle@0.1.0 affected --exit-code @acme/web
```

Without `AXLE_SCM_BASE`, Axle compares with the default branch, such as `main`, so a deploy of that branch finds nothing to build.

## What is compared

Axle compares the point where your branch left the base branch, their merge base, with your working tree. Committed, staged, unstaged and new files all count, so the answer is the same before and after you commit. Files Git ignores never count.

The base is the first of these that is set:

1. `--base`
2. the `AXLE_SCM_BASE` environment variable
3. in a pull request on GitHub Actions, GitLab CI, Bitbucket Pipelines, Azure Pipelines or Buildkite, the branch it targets, as `origin/<branch>`
4. [`base`](https://axlers.dev/docs/reference/configuration.md#affected) in the root `axle.toml`, such as `base = "develop"`
5. the default branch of `origin`, when the clone knows it
6. the first of `main`, `origin/main`, `master` and `origin/master` that exists

`--head` or `AXLE_SCM_HEAD` compares with a commit instead of the working tree, which CI uses to compare two commits exactly.

## What counts as affected

- A changed file belongs to the package whose folder holds it. With packages inside packages, the deepest one wins.
- A changed file outside every package, such as a root `tsconfig.json`, affects every package, because Axle can't know which packages read it. [`globalInputs`](https://axlers.dev/docs/reference/configuration.md#inputs) narrows that to the files it lists.
- A package that depends on an affected package, directly or through others, is affected too.
- When the lockfile changes, Axle reads it before and after, and only the packages whose resolved dependencies changed are affected, along with the packages that depend on them. This works for `pnpm-lock.yaml`, `package-lock.json`, `yarn.lock` and `bun.lock`.
- When Axle can't tell, every package counts as affected, and a warning says why: the clone's [history is too short](https://axlers.dev/docs/messages/history-too-short.md), or a [lockfile can't be read](https://axlers.dev/docs/messages/lockfile-unreadable.md).

## Tasks

`axle affected` lists packages, but `axle run --affected` decides task by task. A task with [`inputs`](https://axlers.dev/docs/reference/configuration.md#inputs) runs only when a changed file is one of its inputs, when its dependencies in the lockfile changed, or when a task it waits for runs, so a README change no longer reruns tests whose inputs leave it out. A task without `inputs` runs when its package is affected. [Command inputs](https://axlers.dev/docs/reference/configuration.md#command-inputs), such as the output of `node --version`, play no part, since they aren't files that change between commits. [`affected = "projects"`](https://axlers.dev/docs/reference/configuration.md#affected) goes back to package-level affected for every task.

## In CI

On GitHub Actions, fetch the whole history so Axle can find where the branch started:

```yaml
- uses: actions/checkout@v7
  with:
      fetch-depth: 0
```

Then, after installing dependencies, run the tests of the affected packages:

```sh
pnpm exec axle run test --affected              # pnpm
npx axle run test --affected                    # npm
yarn axle run test --affected                   # Yarn
bunx axle run test --affected                   # Bun
nubx axle run test --affected                   # nub
aube exec axle run test --affected              # aube
```

In a pull request, Axle compares with the branch it targets without further setup. On other CI services, set `AXLE_SCM_BASE` to the branch to compare with.

## JSON output

```json
{
    "formatVersion": 1,
    "base": "main",
    "head": null,
    "packages": [
        { "name": "@acme/ui", "path": "packages/ui", "reason": "changed" },
        { "name": "@acme/web", "path": "apps/web", "reason": "dependent", "through": "@acme/ui" }
    ],
    "warnings": []
}
```

`formatVersion` is 1, and goes up only when a change would break a tool that reads the output. `reason` is `changed`, `lockfile`, `dependent` or `everything`. `through` names the affected package a dependent was reached from. `warnings` says why when every package counts as affected.
