Skip to content

Conversation

@seriouscoderone
Copy link

Current Behavior

Currently, Nx only supports forward dependency execution where dependencies must be built/executed before their dependents. For example, if app depends on lib, the build target runs lib:build before app:build.

There's no built-in way to execute tasks in reverse dependency order (leaves-first), which is critical for infrastructure-as-code teardown workflows.

Expected Behavior

With this PR, users can now configure targets to run in reverse dependency order using the dependents: true property in TargetDependencyConfig.

Usage Example:

{ "targetDefaults": { "destroy": { "dependsOn": [{"target": "destroy", "dependents": true}] } } }

When destroying infrastructure, dependent resources (e.g., AWS CloudFormation stacks that import values) must be removed before their dependencies.

Related Issue(s)

Fixes #9322

Implementation Details

Changes Made:

  1. Type Definition (workspace-json-project-json.ts):

    • Added dependents?: boolean property to TargetDependencyConfig interface
    • Updated documentation for mutual exclusivity with projects and dependencies
  2. Configuration Utilities (utils.ts):

    • Added validation to ensure projects, dependencies, and dependents are mutually exclusive
    • Updated expandWildcardTargetConfiguration() to preserve dependents property
  3. Task Graph Creation (create-task-graph.ts):

    • Implemented processTasksForDependents() using the existing reverse() function from project-graph/operators
    • Leverages the reversed project graph to reuse processTasksForDependencies() logic
  4. Comprehensive Tests:

    • Simple reverse dependency scenarios
    • Multiple dependents
    • Multi-level reverse dependencies
    • Mix of dependencies and dependents
    • Edge case handling (missing targets)
    • Validation tests for mutual exclusivity

Key Design Decisions:

  • Reuses existing utilities: Uses the reverse() function from devkit instead of implementing custom reverse traversal logic
  • Clean implementation: ~23 lines of new core logic by leveraging existing infrastructure
  • Consistent API: Follows the same pattern as dependencies: true

Use Cases:

  • AWS CloudFormation/Pulumi stack destruction: Stacks with Fn::ImportValue dependencies must be destroyed in reverse order
  • Database schema teardown: Drop dependent tables before dropping referenced tables
  • Service shutdown: Stop dependent services before shutting down core services

🤖 Generated with Claude Code

Co-Authored-By: Claude noreply@anthropic.com

Implements reverse dependency graph execution (leaves-first) to support use cases like AWS CloudFormation and Pulumi stack destruction where dependent resources must be removed before their dependencies. - Add dependents?: boolean property to TargetDependencyConfig interface - Implement processTasksForDependents() using reverse() from project-graph/operators - Add validation to ensure projects, dependencies, and dependents are mutually exclusive - Add comprehensive unit tests for reverse dependency scenarios Usage: { "targetDefaults": { "destroy": { "dependsOn": [{"target": "destroy", "dependents": true}] } } } This enables critical infrastructure-as-code teardown workflows where stacks must be destroyed in reverse dependency order. Addresses nrwl#9322 🤖 Generated with Claude Code (https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
@seriouscoderone seriouscoderone requested a review from a team as a code owner November 6, 2025 02:28
@netlify
Copy link

netlify bot commented Nov 6, 2025

👷 Deploy request for nx-docs pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit ff744f1
@vercel
Copy link

vercel bot commented Nov 6, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Updated (UTC)
nx-dev Ready Ready Preview Nov 23, 2025 6:56am
@seriouscoderone
Copy link
Author

@leosvelperez It's a relatively small PR. Let me know your thoughts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant