From b7d610793d1f1952601ff1ad91d0b27075aab1a6 Mon Sep 17 00:00:00 2001 From: Miroslav Jonas Date: Fri, 25 Feb 2022 09:40:40 +0100 Subject: [PATCH] feat(core): give custom hasher factory access to projectGraph --- .../linter/src/executors/eslint/hasher.ts | 30 +++++++++---------- .../src/tasks-runner/task-orchestrator.ts | 3 +- .../src/tasks-runner/tasks-schedule.spec.ts | 10 +++++-- .../src/tasks-runner/tasks-schedule.ts | 7 +++-- 4 files changed, 29 insertions(+), 21 deletions(-) diff --git a/packages/linter/src/executors/eslint/hasher.ts b/packages/linter/src/executors/eslint/hasher.ts index 6fa4b015e327f..996fb21e9ece2 100644 --- a/packages/linter/src/executors/eslint/hasher.ts +++ b/packages/linter/src/executors/eslint/hasher.ts @@ -1,4 +1,4 @@ -import { Task, TaskGraph } from '@nrwl/devkit'; +import { ProjectGraph, Task, TaskGraph } from '@nrwl/devkit'; import { Hash, Hasher } from '@nrwl/workspace/src/core/hasher/hasher'; import { appRootPath } from '@nrwl/tao/src/utils/app-root'; import { Workspaces } from '@nrwl/tao/src/shared/workspace'; @@ -6,19 +6,18 @@ import { Workspaces } from '@nrwl/tao/src/shared/workspace'; export default async function run( task: Task, taskGraph: TaskGraph, - hasher: Hasher + hasher: Hasher, + projectGraph: ProjectGraph ): Promise { if (task.overrides['hasTypeAwareRules'] === true) { return hasher.hashTaskWithDepsAndContext(task); } const command = hasher.hashCommand(task); const sources = await hasher.hashSource(task); - // TODO: This will be used once we pass hasher's projectGraph - // const deps = allDeps(task.id, taskGraph); + const deps = allDeps(task.id, taskGraph, projectGraph); const workspace = new Workspaces(appRootPath).readWorkspaceConfiguration(); const tags = hasher.hashArray( - // deps.map((d) => (workspace.projects[d].tags || []).join('|')) - Object.values(workspace.projects).map((proj) => (proj.tags || []).join('|')) + deps.map((d) => (workspace.projects[d].tags || []).join('|')) ); const context = await hasher.hashContext(); return { @@ -38,13 +37,14 @@ export default async function run( }; } -function allDeps(taskId: string, taskGraph: TaskGraph): string[] { - return [ - ...taskGraph.dependencies[taskId].map( - (task) => taskGraph.tasks[task].target.project - ), - ...taskGraph.dependencies[taskId].flatMap((task) => - allDeps(task, taskGraph) - ), - ]; +function allDeps( + taskId: string, + taskGraph: TaskGraph, + projectGraph: ProjectGraph +): string[] { + const project = taskGraph.tasks[taskId].target.project; + const dependencies = projectGraph.dependencies[project] + .filter((d) => !!projectGraph.nodes[d.target]) + .map((d) => d.target); + return dependencies; } diff --git a/packages/workspace/src/tasks-runner/task-orchestrator.ts b/packages/workspace/src/tasks-runner/task-orchestrator.ts index 24ea9c2d7183b..fd0f33dea81f6 100644 --- a/packages/workspace/src/tasks-runner/task-orchestrator.ts +++ b/packages/workspace/src/tasks-runner/task-orchestrator.ts @@ -26,7 +26,8 @@ export class TaskOrchestrator { this.hasher, this.taskGraph, this.workspace, - this.options + this.options, + this.projectGraph ); // region internal state diff --git a/packages/workspace/src/tasks-runner/tasks-schedule.spec.ts b/packages/workspace/src/tasks-runner/tasks-schedule.spec.ts index dc56bd193b739..b3224e721b6e1 100644 --- a/packages/workspace/src/tasks-runner/tasks-schedule.spec.ts +++ b/packages/workspace/src/tasks-runner/tasks-schedule.spec.ts @@ -1,5 +1,5 @@ import { TasksSchedule } from './tasks-schedule'; -import { Task, TaskGraph } from '@nrwl/devkit'; +import { ProjectGraph, Task, TaskGraph } from '@nrwl/devkit'; import { Workspaces } from '@nrwl/tao/src/shared/workspace'; import { removeTasksFromTaskGraph } from '@nrwl/workspace/src/tasks-runner/utils'; @@ -82,6 +82,11 @@ describe('TasksSchedule', () => { }, }; + const projectGraph: ProjectGraph = { + nodes: {}, + dependencies: {}, + }; + const hasher = { hashTaskWithDepsAndContext: () => 'hash', } as any; @@ -96,7 +101,8 @@ describe('TasksSchedule', () => { endTask: jest.fn(), scheduleTask: jest.fn(), }, - } + }, + projectGraph ); }); diff --git a/packages/workspace/src/tasks-runner/tasks-schedule.ts b/packages/workspace/src/tasks-runner/tasks-schedule.ts index bd1ef2c96ded0..22e1f765522f4 100644 --- a/packages/workspace/src/tasks-runner/tasks-schedule.ts +++ b/packages/workspace/src/tasks-runner/tasks-schedule.ts @@ -1,4 +1,4 @@ -import { Task, TaskGraph } from '@nrwl/devkit'; +import { ProjectGraph, Task, TaskGraph } from '@nrwl/devkit'; import { Workspaces } from '@nrwl/tao/src/shared/workspace'; @@ -31,7 +31,8 @@ export class TasksSchedule { private readonly hasher: Hasher, private taskGraph: TaskGraph, private workspace: Workspaces, - private options: DefaultTasksRunnerOptions + private options: DefaultTasksRunnerOptions, + private readonly projectGraph: ProjectGraph ) {} public async scheduleNextTasks() { @@ -163,7 +164,7 @@ export class TasksSchedule { private async hashTask(task: Task) { const customHasher = getCustomHasher(task, this.workspace); const { value, details } = await (customHasher - ? customHasher(task, this.taskGraph, this.hasher) + ? customHasher(task, this.taskGraph, this.hasher, this.projectGraph) : this.hasher.hashTaskWithDepsAndContext(task)); task.hash = value; task.hashDetails = details;