feat(core): add dependents property for reverse dependency execution #33389
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Current Behavior
Currently, Nx only supports forward dependency execution where dependencies must be built/executed before their dependents. For example, if
appdepends onlib, thebuildtarget runslib:buildbeforeapp: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: trueproperty inTargetDependencyConfig.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:
Type Definition (
workspace-json-project-json.ts):dependents?: booleanproperty toTargetDependencyConfiginterfaceprojectsanddependenciesConfiguration Utilities (
utils.ts):projects,dependencies, anddependentsare mutually exclusiveexpandWildcardTargetConfiguration()to preservedependentspropertyTask Graph Creation (
create-task-graph.ts):processTasksForDependents()using the existingreverse()function fromproject-graph/operatorsprocessTasksForDependencies()logicComprehensive Tests:
Key Design Decisions:
reverse()function from devkit instead of implementing custom reverse traversal logicdependencies: trueUse Cases:
Fn::ImportValuedependencies must be destroyed in reverse order🤖 Generated with Claude Code
Co-Authored-By: Claude noreply@anthropic.com