Skip to content
Latchkey

Nx affected: base SHA not found in CI (fetch-depth)

Nx affected diffs your branch against a base commit, but a shallow CI checkout did not fetch that commit, so the SHA is not in local history and the diff fails.

What this error means

nx affected fails resolving the base, with git complaining the base SHA is not a valid object or "fatal: bad object". The default actions/checkout fetch-depth of 1 is the usual cause.

nx
NX   Unable to find base SHA "a1b2c3d" in the repository.
fatal: bad object a1b2c3d

Common causes

Shallow clone hides the base commit

actions/checkout defaults to fetch-depth 1, so the base SHA Nx needs for the diff is not present.

Base ref not fetched

The main branch or merge-base was never fetched, so there is nothing to diff against.

Wrong --base value

A --base pointing at a SHA or ref that does not exist in the checkout.

How to fix it

Fetch full history in checkout

  1. Set fetch-depth to 0 so the base commit is available.
workflow.yml
- uses: actions/checkout@v4
  with:
    fetch-depth: 0

Use the Nx set-shas action

  1. Compute correct base and head SHAs for affected commands.
workflow.yml
- uses: nrwl/nx-set-shas@v4
- run: npx nx affected -t build --base=${NX_BASE} --head=${NX_HEAD}

How to prevent it

  • This is a common CI gotcha: nx affected needs git history, so always use fetch-depth 0 (or nx-set-shas) when computing affected projects.

Related guides

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