axle affected
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 runs in, so a pull request only builds and tests what it touches.
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/uiaxle why affected explains one package:
@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.jsonFor 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:
@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:
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.tsOptions
Section titled “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
Section titled “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} in its arguments gets from axle run --affected. --task names another task.
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.tsxA task runs every test when Axle can’t account for the whole change, as running only the tests a change reaches explains, and the line says why. With --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
Section titled “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:
axle affected --exit-code @acme/webThat’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:
# 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# 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/webWithout 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
Section titled “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:
--base- the
AXLE_SCM_BASEenvironment variable - in a pull request on GitHub Actions, GitLab CI, Bitbucket Pipelines, Azure Pipelines or Buildkite, the branch it targets, as
origin/<branch> basein the rootaxle.toml, such asbase = "develop"- the default branch of
origin, when the clone knows it - the first of
main,origin/main,masterandorigin/masterthat 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
Section titled “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.globalInputsnarrows 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.lockandbun.lock. - When Axle can’t tell, every package counts as affected, and a warning says why: the clone’s history is too short, or a lockfile can’t be read.
axle affected lists packages, but axle run --affected decides task by task. A task with 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, such as the output of node --version, play no part, since they aren’t files that change between commits. affected = "projects" goes back to package-level affected for every task.
On GitHub Actions, fetch the whole history so Axle can find where the branch started:
- uses: actions/checkout@v7 with: fetch-depth: 0Then, after installing dependencies, run the tests of the affected packages:
pnpm exec axle run test --affectednpx axle run test --affectedyarn axle run test --affectedbunx axle run test --affectednubx axle run test --affectedaube exec axle run test --affectedIn 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
Section titled “JSON output”{ "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.