From 1a4959f9a7828724db8be1baacd3e1aa9b44285c Mon Sep 17 00:00:00 2001 From: Emily Xiong Date: Fri, 27 Sep 2024 14:29:53 -0400 Subject: [PATCH] fix(core): move getDetails to top (#28158) ## Current Behavior ## Expected Behavior ## Related Issue(s) Fixes https://github.com/nrwl/nx/issues/28035 --- packages/nx/src/hasher/hash-task.ts | 21 ++++++++----------- packages/nx/src/tasks-runner/cache.ts | 2 +- packages/nx/src/tasks-runner/run-command.ts | 11 ++++++++-- .../nx/src/tasks-runner/task-orchestrator.ts | 14 ++++++++----- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/packages/nx/src/hasher/hash-task.ts b/packages/nx/src/hasher/hash-task.ts index bcaf6ad767a8d..73e8369b33dd8 100644 --- a/packages/nx/src/hasher/hash-task.ts +++ b/packages/nx/src/hasher/hash-task.ts @@ -5,12 +5,12 @@ import { getInputs, TaskHasher } from './task-hasher'; import { ProjectGraph } from '../config/project-graph'; import { NxJsonConfiguration } from '../config/nx-json'; import { readNxJson } from '../config/nx-json'; -import { IS_WASM, TaskDetails } from '../native'; +import { HashedTask, IS_WASM, TaskDetails } from '../native'; import { getDbConnection } from '../utils/db-connection'; let taskDetails: TaskDetails; -function getTaskDetails() { +export function getTaskDetails(): TaskDetails | null { // TODO: Remove when wasm supports sqlite if (process.env.NX_DISABLE_DB === 'true' || IS_WASM) { return null; @@ -25,12 +25,11 @@ export async function hashTasksThatDoNotDependOnOutputsOfOtherTasks( hasher: TaskHasher, projectGraph: ProjectGraph, taskGraph: TaskGraph, - nxJson: NxJsonConfiguration + nxJson: NxJsonConfiguration, + tasksDetails: TaskDetails | null ) { performance.mark('hashMultipleTasks:start'); - const taskDetails = getTaskDetails(); - const tasks = Object.values(taskGraph.tasks); const tasksWithHashers = await Promise.all( tasks.map(async (task) => { @@ -58,9 +57,8 @@ export async function hashTasksThatDoNotDependOnOutputsOfOtherTasks( tasksToHash[i].hash = hashes[i].value; tasksToHash[i].hashDetails = hashes[i].details; } - // TODO: Remove if when sqlite is always on - if (taskDetails) { - taskDetails.recordTaskDetails( + if (tasksDetails?.recordTaskDetails) { + tasksDetails.recordTaskDetails( tasksToHash.map((task) => ({ hash: task.hash, project: task.target.project, @@ -83,11 +81,11 @@ export async function hashTask( projectGraph: ProjectGraph, taskGraph: TaskGraph, task: Task, - env: NodeJS.ProcessEnv + env: NodeJS.ProcessEnv, + taskDetails: TaskDetails | null ) { performance.mark('hashSingleTask:start'); - const taskDetails = getTaskDetails(); const customHasher = getCustomHasher(task, projectGraph); const projectsConfigurations = readProjectsConfigurationFromProjectGraph(projectGraph); @@ -106,8 +104,7 @@ export async function hashTask( task.hash = value; task.hashDetails = details; - // TODO: Remove if when wasm supports sqlite - if (taskDetails) { + if (taskDetails?.recordTaskDetails) { taskDetails.recordTaskDetails([ { hash: task.hash, diff --git a/packages/nx/src/tasks-runner/cache.ts b/packages/nx/src/tasks-runner/cache.ts index 10aa833d9bf86..3416e555b817b 100644 --- a/packages/nx/src/tasks-runner/cache.ts +++ b/packages/nx/src/tasks-runner/cache.ts @@ -32,7 +32,7 @@ export type TaskWithCachedResult = { task: Task; cachedResult: CachedResult }; export function getCache( nxJson: NxJsonConfiguration, options: DefaultTasksRunnerOptions -) { +): DbCache | Cache { return process.env.NX_DISABLE_DB !== 'true' && (nxJson.enableDbCache === true || process.env.NX_DB_CACHE === 'true') ? new DbCache({ diff --git a/packages/nx/src/tasks-runner/run-command.ts b/packages/nx/src/tasks-runner/run-command.ts index a09ceaeffbc71..a94c275afe850 100644 --- a/packages/nx/src/tasks-runner/run-command.ts +++ b/packages/nx/src/tasks-runner/run-command.ts @@ -11,7 +11,10 @@ import { Task, TaskGraph } from '../config/task-graph'; import { TargetDependencyConfig } from '../config/workspace-json-project-json'; import { daemonClient } from '../daemon/client/client'; import { createTaskHasher } from '../hasher/create-task-hasher'; -import { hashTasksThatDoNotDependOnOutputsOfOtherTasks } from '../hasher/hash-task'; +import { + getTaskDetails, + hashTasksThatDoNotDependOnOutputsOfOtherTasks, +} from '../hasher/hash-task'; import { IS_WASM } from '../native'; import { createProjectGraphAsync } from '../project-graph/project-graph'; import { NxArgs } from '../utils/command-line-utils'; @@ -589,6 +592,9 @@ export async function invokeTasksRunner({ }): Promise<{ [id: string]: TaskResult }> { setEnvVarsBasedOnArgs(nxArgs, loadDotEnvFiles); + // this needs to be done before we start to run the tasks + const taskDetails = getTaskDetails(); + const { tasksRunner, runnerOptions } = getRunner(nxArgs, nxJson); let hasher = createTaskHasher(projectGraph, nxJson, runnerOptions); @@ -601,7 +607,8 @@ export async function invokeTasksRunner({ hasher, projectGraph, taskGraph, - nxJson + nxJson, + taskDetails ); const taskResultsLifecycle = new TaskResultsLifeCycle(); const compositedLifeCycle: LifeCycle = new CompositeLifeCycle([ diff --git a/packages/nx/src/tasks-runner/task-orchestrator.ts b/packages/nx/src/tasks-runner/task-orchestrator.ts index 10bdaa63bf6f2..9910009cc086a 100644 --- a/packages/nx/src/tasks-runner/task-orchestrator.ts +++ b/packages/nx/src/tasks-runner/task-orchestrator.ts @@ -5,7 +5,7 @@ import { writeFileSync } from 'fs'; import { TaskHasher } from '../hasher/task-hasher'; import runCommandsImpl from '../executors/run-commands/run-commands.impl'; import { ForkedProcessTaskRunner } from './forked-process-task-runner'; -import { getCache } from './cache'; +import { Cache, DbCache, getCache } from './cache'; import { DefaultTasksRunnerOptions } from './default-tasks-runner'; import { TaskStatus } from './tasks-runner'; import { @@ -22,7 +22,7 @@ import { TaskMetadata } from './life-cycle'; import { ProjectGraph } from '../config/project-graph'; import { Task, TaskGraph } from '../config/task-graph'; import { DaemonClient } from '../daemon/client/client'; -import { hashTask } from '../hasher/hash-task'; +import { getTaskDetails, hashTask } from '../hasher/hash-task'; import { getEnvVariablesForBatchProcess, getEnvVariablesForTask, @@ -32,9 +32,11 @@ import { workspaceRoot } from '../utils/workspace-root'; import { output } from '../utils/output'; import { combineOptionsForExecutor } from '../utils/params'; import { NxJsonConfiguration } from '../config/nx-json'; +import type { TaskDetails } from '../native'; export class TaskOrchestrator { - private cache = getCache(this.nxJson, this.options); + private taskDetails: TaskDetails | null = getTaskDetails(); + private cache: DbCache | Cache = getCache(this.nxJson, this.options); private forkedProcessTaskRunner = new ForkedProcessTaskRunner(this.options); private tasksSchedule = new TasksSchedule( @@ -163,7 +165,8 @@ export class TaskOrchestrator { this.projectGraph, this.taskGraph, task, - taskSpecificEnv + taskSpecificEnv, + this.taskDetails ); } @@ -181,7 +184,8 @@ export class TaskOrchestrator { this.projectGraph, this.taskGraph, task, - this.batchEnv + this.batchEnv, + this.taskDetails ); } await this.options.lifeCycle.scheduleTask(task);