From 445cd59c7061dee80c36d36634b7076ae8ee9558 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Jona=C5=A1?= Date: Tue, 8 Mar 2022 11:25:16 +0100 Subject: [PATCH] fix(linter): use projectGraph in hasher for calculating dependencies (#9118) --- .../linter/src/executors/eslint/hasher.ts | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/packages/linter/src/executors/eslint/hasher.ts b/packages/linter/src/executors/eslint/hasher.ts index 6fa4b015e327f..685d613462772 100644 --- a/packages/linter/src/executors/eslint/hasher.ts +++ b/packages/linter/src/executors/eslint/hasher.ts @@ -1,7 +1,8 @@ -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'; +import { readCachedProjectGraph } from '@nrwl/workspace/src/core/project-graph'; export default async function run( task: Task, @@ -11,14 +12,22 @@ export default async function run( if (task.overrides['hasTypeAwareRules'] === true) { return hasher.hashTaskWithDepsAndContext(task); } + if (!(global as any).projectGraph) { + try { + (global as any).projectGraph = readCachedProjectGraph(); + } catch { + // do nothing, if project graph is unavailable we fallback to using all projects + } + } + const projectGraph = (global as any).projectGraph; 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 workspace = new Workspaces(appRootPath).readWorkspaceConfiguration(); + const deps = projectGraph + ? allDeps(task.id, taskGraph, projectGraph) + : Object.keys(workspace.projects); 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 +47,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; }