Skip to content

What is NX Affected?

Nx Affected is a feature in the Nx build system that analyses the dependency graph to identify which projects (applications and libraries) are affected by changes in our codebase. It helps determine the minimal set of projects that need to be rebuilt and retested.

NX Affected example workflow

  1. Detect Changes: Nx identifies modifications in projectA and projectB.
  2. Identify Affected Projects: Nx determines that projectC depends on projectA and projectD depends on projectB.
  3. Traverse Dependencies: Nx continues to check all projects affected by changes in projectA and projectB.
  4. List Affected Projects: Nx lists Projects A, B, C, and D as affected.

What is "Traversing the Dependency Graph" in Nx?

When Nx "traverses" the dependency graph, it systematically explores projects and their dependencies to find affected projects:

  • Start with Changed Projects: Nx identifies directly changed projects.
  • Follow Dependencies: Nx follows project dependencies to find all dependent projects.
  • Mark as Affected: Each dependent project is marked as affected.
  • Repeat if Necessary: This process continues recursively until all affected projects are identified.

Example of traversing a dependency graph

Consider a dependency graph with these projects:

  • projectA
  • projectB depends on projectA
  • projectC depends on projectB
  • projectD depends on projectC

If projectA changes:

  • Nx starts at projectA.
  • Nx follows the edge to projectB and marks it as affected.
  • Nx follows the edge to projectC and marks it as affected.
  • Nx follows the edge to projectD and marks it as affected.

In this example, Projects A, B, C, and D are all affected by the change in projectA.

How can I list the affected projects?

You can use commands such as nx affected:apps, nx affected:libs, or nx affected:projects to list affected applications, libraries, or all projects respectively.

How do I run tests only for affected projects?

Use nx affected:test to run tests for only the projects affected by recent changes.

Why are all projects marked as affected?

All projects in an Nx workspace might be marked as affected in the following scenarios:

  1. Global Configuration Changes: Changes to global configuration files (e.g., nx.json, tsconfig.json, root package.json).
  2. Base Libraries/Utilities: Changes to core libraries or utility modules used across multiple projects.
  3. Cascading Dependencies: Changes in foundational projects can cascade and mark many dependent projects as affected.
  4. Build or CI Configuration: Changes to global build scripts or CI/CD pipelines might require retesting or rebuilding all projects.
  5. Major Framework Upgrades: Upgrading a major framework version (e.g., Angular, React) used across projects.
  6. Nx or Tooling Updates: Updating Nx or other critical development tools.
  7. Global Linting Rules: Changes to global ESLint or Prettier configurations.
  8. Global Environment Variables: Changes to environment variables or configuration elements affecting all projects.
  9. Platform-Specific Changes: Updates to infrastructure impacting project deployment or execution.

Example Scenario

In an Nx workspace with these projects:

  • projectA (depends on sharedLib)
  • projectB (depends on sharedLib)
  • projectC (depends on projectB)
  • sharedLib (used by projectA and projectB)

If sharedLib is modified:

  • Directly Affected: projectA and projectB (depend on sharedLib).
  • Indirectly Affected: projectC (depends on projectB).

Thus, projectA, projectB, and projectC are marked as affected. Changes to widely-used libraries or configurations can lead to all projects being marked as affected.

Example Commands

1
2
3
4
5
6
7
# List affected projects
nx affected:apps
nx affected:libs
nx affected:projects

# Run tests for affected projects
nx affected:test