diff --git a/packages/nx/src/tasks-runner/life-cycles/task-history-life-cycle.ts b/packages/nx/src/tasks-runner/life-cycles/task-history-life-cycle.ts index 9db2f7a05ea443..b9ed61b04495e8 100644 --- a/packages/nx/src/tasks-runner/life-cycles/task-history-life-cycle.ts +++ b/packages/nx/src/tasks-runner/life-cycles/task-history-life-cycle.ts @@ -3,7 +3,7 @@ import { Task } from '../../config/task-graph'; import { output } from '../../utils/output'; import { LifeCycle, TaskResult } from '../life-cycle'; import type { TaskRun as NativeTaskRun } from '../../native'; -import { getTaskHistory } from '../../utils/task-history'; +import { getTaskHistory, TaskHistory } from '../../utils/task-history'; interface TaskRun extends NativeTaskRun { target: Task['target']; @@ -12,7 +12,7 @@ interface TaskRun extends NativeTaskRun { export class TaskHistoryLifeCycle implements LifeCycle { private startTimings: Record = {}; private taskRuns = new Map(); - private taskHistory = getTaskHistory(); + private taskHistory: TaskHistory | undefined = getTaskHistory(); startTasks(tasks: Task[]): void { for (let task of tasks) { @@ -38,6 +38,9 @@ export class TaskHistoryLifeCycle implements LifeCycle { async endCommand() { const entries = Array.from(this.taskRuns); + if (!this.taskHistory) { + return; + } await this.taskHistory.recordTaskRuns(entries.map(([_, v]) => v)); const flakyTasks = await this.taskHistory.getFlakyTasks( entries.map(([hash]) => hash) diff --git a/packages/nx/src/tasks-runner/tasks-schedule.ts b/packages/nx/src/tasks-runner/tasks-schedule.ts index 2ae26d2e437938..84fd51e64c3153 100644 --- a/packages/nx/src/tasks-runner/tasks-schedule.ts +++ b/packages/nx/src/tasks-runner/tasks-schedule.ts @@ -20,8 +20,7 @@ export class TasksSchedule { private notScheduledTaskGraph = this.taskGraph; private reverseTaskDeps = calculateReverseDeps(this.taskGraph); private reverseProjectGraph = reverse(this.projectGraph); - private taskHistory: TaskHistory = - process.env.NX_DISABLE_DB !== 'true' ? getTaskHistory() : null; + private taskHistory: TaskHistory | undefined = getTaskHistory(); private scheduledBatches: Batch[] = []; private scheduledTasks: string[] = []; diff --git a/packages/nx/src/utils/task-history.ts b/packages/nx/src/utils/task-history.ts index d1ed2bbcf36337..716c845d899ccd 100644 --- a/packages/nx/src/utils/task-history.ts +++ b/packages/nx/src/utils/task-history.ts @@ -1,6 +1,6 @@ import { daemonClient } from '../daemon/client/client'; import { isOnDaemon } from '../daemon/is-on-daemon'; -import { NxTaskHistory, TaskRun, TaskTarget } from '../native'; +import { IS_WASM, NxTaskHistory, TaskRun, TaskTarget } from '../native'; import { getDbConnection } from './db-connection'; export class TaskHistory { @@ -39,9 +39,13 @@ let taskHistory: TaskHistory; /** * This function returns the singleton instance of TaskHistory - * @returns singleton instance of TaskHistory + * @returns singleton instance of TaskHistory, undefined if disabled */ -export function getTaskHistory(): TaskHistory { +export function getTaskHistory(): TaskHistory | undefined { + if (process.env.NX_DISABLE_DB === 'true' || !IS_WASM) { + return; + } + if (!taskHistory) { taskHistory = new TaskHistory(); }