From 84fb879c006fa3db57a0e3be337ea95bdfacad2a Mon Sep 17 00:00:00 2001 From: Victor Savkin Date: Fri, 23 Sep 2022 14:44:44 -0400 Subject: [PATCH] fix(core): excluding task dependencies breaks task sorting --- .../tasks-runner/create-task-graph.spec.ts | 69 +++++++++++++++++-- .../nx/src/tasks-runner/create-task-graph.ts | 24 +++++-- 2 files changed, 85 insertions(+), 8 deletions(-) diff --git a/packages/nx/src/tasks-runner/create-task-graph.spec.ts b/packages/nx/src/tasks-runner/create-task-graph.spec.ts index 16b851cacc..06dc465f8c 100644 --- a/packages/nx/src/tasks-runner/create-task-graph.spec.ts +++ b/packages/nx/src/tasks-runner/create-task-graph.spec.ts @@ -1,4 +1,4 @@ -import { ProjectGraph } from '../config/project-graph'; +import { DependencyType, ProjectGraph } from '../config/project-graph'; import { createTaskGraph } from 'nx/src/tasks-runner/create-task-graph'; describe('createTaskGraph', () => { @@ -741,14 +741,31 @@ describe('createTaskGraph', () => { files: [], targets: { build: { - dependsOn: [{ target: 'test', projects: 'self' }], + dependsOn: [ + { target: 'prebuild', projects: 'self' }, + { target: 'build', projects: 'dependencies' }, + ], }, - test: {}, + prebuild: {}, + }, + }, + }, + app2: { + name: 'app2', + type: 'app', + data: { + root: 'app2-root', + files: [], + targets: { + build: {}, }, }, }, }, - dependencies: {}, + dependencies: { + app1: [{ source: 'app1', target: 'app2', type: DependencyType.static }], + app2: [], + }, }; const taskGraph = createTaskGraph( @@ -782,5 +799,49 @@ describe('createTaskGraph', () => { 'app1:build': [], }, }); + + const taskGraph2 = createTaskGraph( + projectGraph, + {}, + ['app1', 'app2'], + ['build'], + 'development', + { + __overrides_unparsed__: [], + }, + true + ); + // prebuild should also be in here + expect(taskGraph2).toEqual({ + roots: ['app2:build'], + tasks: { + 'app1:build': { + id: 'app1:build', + target: { + project: 'app1', + target: 'build', + }, + overrides: { + __overrides_unparsed__: [], + }, + projectRoot: 'app1-root', + }, + 'app2:build': { + id: 'app2:build', + target: { + project: 'app2', + target: 'build', + }, + overrides: { + __overrides_unparsed__: [], + }, + projectRoot: 'app2-root', + }, + }, + dependencies: { + 'app1:build': ['app2:build'], + 'app2:build': [] + }, + }); }); }); diff --git a/packages/nx/src/tasks-runner/create-task-graph.ts b/packages/nx/src/tasks-runner/create-task-graph.ts index a85f8edf75..e299950691 100644 --- a/packages/nx/src/tasks-runner/create-task-graph.ts +++ b/packages/nx/src/tasks-runner/create-task-graph.ts @@ -44,12 +44,28 @@ export class ProcessTasks { } } - if (!excludeTaskDependencies) { - for (const taskId of Object.keys(this.tasks)) { - const task = this.tasks[taskId]; - this.processTask(task, task.target.project, configuration, overrides); + // used when excluding tasks + const initialTasks = { ...this.tasks }; + + for (const taskId of Object.keys(this.tasks)) { + const task = this.tasks[taskId]; + this.processTask(task, task.target.project, configuration, overrides); + } + + if (excludeTaskDependencies) { + for (let t of Object.keys(this.tasks)) { + if (!initialTasks[t]) { + delete this.tasks[t]; + delete this.dependencies[t]; + } + } + for (let d of Object.keys(this.dependencies)) { + this.dependencies[d] = this.dependencies[d].filter( + (dd) => !!initialTasks[dd] + ); } } + return Object.keys(this.dependencies).filter( (d) => this.dependencies[d].length === 0 );