Skip to content
Latchkey

Nx affected "requires a base and head" in CI

The nx affected command diffs two git refs to decide which projects changed. In CI, Nx cannot infer a sensible base and head on its own, so it errors and asks you to pass them (or set them via nrwl/nx-set-shas).

What this error means

A CI step running nx affected fails with a message that it requires a base and head, or that it cannot determine the SHAs to compare.

Nx
NX   Nx failed to determine the base and head SHAs for the affected command.
Pass --base and --head explicitly, or use nrwl/nx-set-shas.

Common causes

No base and head are supplied in CI

Outside a local git checkout with a tracked main branch, Nx has no default base to diff against and no head to compare, so it refuses to guess.

The base commit is not in the fetched history

A shallow clone omits the base commit, so even a supplied base ref cannot be resolved to a real SHA.

How to fix it

Set base and head with nrwl/nx-set-shas

  1. Add the nrwl/nx-set-shas action before the affected step.
  2. It exports NX_BASE and NX_HEAD from the last successful run.
  3. Run nx affected after it so the SHAs are available.
.github/workflows/ci.yml
- uses: nrwl/nx-set-shas@v4
- run: npx nx affected -t build test

Fetch full history so refs resolve

Use fetch-depth: 0 on checkout so the base commit exists locally and can be diffed.

.github/workflows/ci.yml
- uses: actions/checkout@v4
  with:
    fetch-depth: 0

How to prevent it

  • Always pair nx affected in CI with nrwl/nx-set-shas.
  • Set fetch-depth: 0 on checkout so the base is present.
  • Pass explicit --base / --head for non-standard branch flows.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →