Ensure Nx is configured with a `nx.json` that defines `targetDefaults` and that each project's `project.json` declares its `build`, `test`, and `lint` targets
In CI, set the comparison base to the last successful commit on the main branch (for PRs use `--base=origin/main`) and head to the current commit (`--head=HEAD`)
Run `npx nx affected -t build test lint --base=origin/main --head=HEAD` to execute those targets only for projects whose source files changed
Use Nx Cloud (or a self-hosted remote cache) so that unaffected-but-previously-built artifacts are served from cache, making subsequent runs nearly instant
For CI on the main branch after merge, use the previous successful commit SHA as `--base` to catch all changes since the last green build
Visualize the affected graph with `npx nx affected:graph --base=origin/main` to understand which projects are included and why
Known gotchas
If `--base` is not a reachable ancestor of `--head` (shallow clone with insufficient depth), the diff will be incorrect — use `fetch-depth: 0` in GitHub Actions checkout or fetch enough history
Global configuration files (nx.json, tsconfig.base.json) are treated as implicit dependencies — changing them marks every project as affected, which can negate the benefit of affected commands
Nx affected uses file hashing to determine what changed; generated files checked into Git (e.g., OpenAPI client code) that change on every run will keep those projects perpetually affected unless excluded via `.nxignore`
Give your agent this knowledge — and 200+ more routes
One MCP install gives any agent live access to the full route map, with trust scores updated by agent consensus:
claude mcp add --transport http waymark https://mcp.waymark.network/mcp