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¶
- Detect Changes: Nx identifies modifications in
projectAandprojectB. - Identify Affected Projects: Nx determines that
projectCdepends onprojectAandprojectDdepends onprojectB. - Traverse Dependencies: Nx continues to check all projects affected by changes in
projectAandprojectB. - 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:
projectAprojectBdepends onprojectAprojectCdepends onprojectBprojectDdepends onprojectC
If projectA changes:
- Nx starts at
projectA. - Nx follows the edge to
projectBand marks it as affected. - Nx follows the edge to
projectCand marks it as affected. - Nx follows the edge to
projectDand 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:
- Global Configuration Changes: Changes to global configuration files (e.g.,
nx.json,tsconfig.json, rootpackage.json). - Base Libraries/Utilities: Changes to core libraries or utility modules used across multiple projects.
- Cascading Dependencies: Changes in foundational projects can cascade and mark many dependent projects as affected.
- Build or CI Configuration: Changes to global build scripts or CI/CD pipelines might require retesting or rebuilding all projects.
- Major Framework Upgrades: Upgrading a major framework version (e.g., Angular, React) used across projects.
- Nx or Tooling Updates: Updating Nx or other critical development tools.
- Global Linting Rules: Changes to global ESLint or Prettier configurations.
- Global Environment Variables: Changes to environment variables or configuration elements affecting all projects.
- Platform-Specific Changes: Updates to infrastructure impacting project deployment or execution.
Example Scenario¶
In an Nx workspace with these projects:
projectA(depends onsharedLib)projectB(depends onsharedLib)projectC(depends onprojectB)sharedLib(used byprojectAandprojectB)
If sharedLib is modified:
- Directly Affected:
projectAandprojectB(depend onsharedLib). - Indirectly Affected:
projectC(depends onprojectB).
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.