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 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
- Add the
nrwl/nx-set-shasaction before the affected step. - It exports
NX_BASEandNX_HEADfrom the last successful run. - Run
nx affectedafter it so the SHAs are available.
- uses: nrwl/nx-set-shas@v4
- run: npx nx affected -t build testFetch full history so refs resolve
Use fetch-depth: 0 on checkout so the base commit exists locally and can be diffed.
- uses: actions/checkout@v4
with:
fetch-depth: 0How to prevent it
- Always pair
nx affectedin CI with nrwl/nx-set-shas. - Set
fetch-depth: 0on checkout so the base is present. - Pass explicit
--base/--headfor non-standard branch flows.