diff --git a/packages/nx/src/hasher/hash-task.ts b/packages/nx/src/hasher/hash-task.ts index 4864a2b3c3873..5a627b641c61f 100644 --- a/packages/nx/src/hasher/hash-task.ts +++ b/packages/nx/src/hasher/hash-task.ts @@ -6,13 +6,13 @@ import { ProjectGraph } from '../config/project-graph'; import { NxJsonConfiguration } from '../config/nx-json'; import { readNxJson } from '../config/nx-json'; import { HashedTask, IS_WASM, TaskDetails } from '../native'; -import { getDbConnection } from '../utils/db-connection'; +import { databaseSupportEnabled, getDbConnection } from '../utils/database'; let taskDetails: TaskDetails; export function getTaskDetails(): TaskDetails | null { // TODO: Remove when wasm supports sqlite - if (process.env.NX_DISABLE_DB === 'true' || IS_WASM) { + if (!databaseSupportEnabled() || IS_WASM) { return null; } if (!taskDetails) { diff --git a/packages/nx/src/native/tests/cache.spec.ts b/packages/nx/src/native/tests/cache.spec.ts index d577cea4eae9f..af7436ea82556 100644 --- a/packages/nx/src/native/tests/cache.spec.ts +++ b/packages/nx/src/native/tests/cache.spec.ts @@ -2,7 +2,7 @@ import { TaskDetails, NxCache } from '../index'; import { join } from 'path'; import { TempFs } from '../../internal-testing-utils/temp-fs'; import { rmSync } from 'fs'; -import { getDbConnection } from '../../utils/db-connection'; +import { getDbConnection } from '../../utils/database'; import { randomBytes } from 'crypto'; describe('Cache', () => { diff --git a/packages/nx/src/native/tests/task_history.spec.ts b/packages/nx/src/native/tests/task_history.spec.ts index 991663459f784..9593c6a2eb145 100644 --- a/packages/nx/src/native/tests/task_history.spec.ts +++ b/packages/nx/src/native/tests/task_history.spec.ts @@ -2,7 +2,7 @@ import { TaskDetails, NxTaskHistory } from '../index'; import { join } from 'path'; import { TempFs } from '../../internal-testing-utils/temp-fs'; import { rmSync } from 'fs'; -import { getDbConnection } from '../../utils/db-connection'; +import { getDbConnection } from '../../utils/database'; import { randomBytes } from 'crypto'; const dbOutputFolder = 'temp-db-task'; diff --git a/packages/nx/src/tasks-runner/cache.ts b/packages/nx/src/tasks-runner/cache.ts index e35914de71451..03adb621e694c 100644 --- a/packages/nx/src/tasks-runner/cache.ts +++ b/packages/nx/src/tasks-runner/cache.ts @@ -13,7 +13,7 @@ import { cacheDir } from '../utils/cache-directory'; import { Task } from '../config/task-graph'; import { machineId } from 'node-machine-id'; import { NxCache, CachedResult as NativeCacheResult, IS_WASM } from '../native'; -import { getDbConnection } from '../utils/db-connection'; +import { databaseSupportEnabled, getDbConnection } from '../utils/database'; import { isNxCloudUsed } from '../utils/nx-cloud-utils'; import { NxJsonConfiguration, readNxJson } from '../config/nx-json'; import { verifyOrUpdateNxCloudClient } from '../nx-cloud/update-manager'; @@ -32,7 +32,7 @@ export type TaskWithCachedResult = { task: Task; cachedResult: CachedResult }; export function dbCacheEnabled(nxJson: NxJsonConfiguration = readNxJson()) { return ( !IS_WASM && - process.env.NX_DISABLE_DB !== 'true' && + databaseSupportEnabled() && nxJson.useLegacyCache !== true && process.env.NX_DB_CACHE !== 'false' ); diff --git a/packages/nx/src/tasks-runner/run-command.ts b/packages/nx/src/tasks-runner/run-command.ts index 97bb25f3b814b..1a73287c3d9d9 100644 --- a/packages/nx/src/tasks-runner/run-command.ts +++ b/packages/nx/src/tasks-runner/run-command.ts @@ -55,6 +55,7 @@ import { shouldStreamOutput } from './utils'; import chalk = require('chalk'); import type { Observable } from 'rxjs'; import { printPowerpackLicense } from '../utils/powerpack'; +import { databaseSupportEnabled } from '../utils/database'; async function getTerminalOutputLifeCycle( initiatingProject: string, @@ -711,7 +712,7 @@ function constructLifeCycles(lifeCycle: LifeCycle): LifeCycle[] { } if (!isNxCloudUsed(readNxJson())) { lifeCycles.push( - process.env.NX_DISABLE_DB !== 'true' && !IS_WASM + databaseSupportEnabled() && !IS_WASM ? new TaskHistoryLifeCycle() : new LegacyTaskHistoryLifeCycle() ); diff --git a/packages/nx/src/utils/database.spec.ts b/packages/nx/src/utils/database.spec.ts new file mode 100644 index 0000000000000..99e3665212f91 --- /dev/null +++ b/packages/nx/src/utils/database.spec.ts @@ -0,0 +1,16 @@ +import { databaseSupportEnabled } from './database'; + +describe('test if database is enabled', () => { + it('should be disabled by default', () => { + process.env.NX_DISABLE_DB = undefined; + expect(databaseSupportEnabled()).toBe(false); + }); + it('should be disabled if NX_DISABLE_DB is explicitly set to `true`', () => { + process.env.NX_DISABLE_DB = 'true'; + expect(databaseSupportEnabled()).toBe(false); + }); + it('should be enabled if NX_DISABLE_DB is explicitly set to `false`', () => { + process.env.NX_DISABLE_DB = 'false'; + expect(databaseSupportEnabled()).toBe(true); + }); +}); diff --git a/packages/nx/src/utils/db-connection.ts b/packages/nx/src/utils/database.ts similarity index 86% rename from packages/nx/src/utils/db-connection.ts rename to packages/nx/src/utils/database.ts index 66034177d6bf5..ce92f8cff9440 100644 --- a/packages/nx/src/utils/db-connection.ts +++ b/packages/nx/src/utils/database.ts @@ -31,3 +31,8 @@ function getEntryOrSet( map.set(key, val); return val; } + +// database is disabled by default +export function databaseSupportEnabled() { + return process.env.NX_DISABLE_DB === 'false'; +} diff --git a/packages/nx/src/utils/task-history.ts b/packages/nx/src/utils/task-history.ts index afbbaf50eef06..86e6c9009880b 100644 --- a/packages/nx/src/utils/task-history.ts +++ b/packages/nx/src/utils/task-history.ts @@ -1,7 +1,7 @@ import { daemonClient } from '../daemon/client/client'; import { isOnDaemon } from '../daemon/is-on-daemon'; import { IS_WASM, NxTaskHistory, TaskRun, TaskTarget } from '../native'; -import { getDbConnection } from './db-connection'; +import { databaseSupportEnabled, getDbConnection } from './database'; export class TaskHistory { taskHistory = new NxTaskHistory(getDbConnection()); @@ -42,7 +42,7 @@ let taskHistory: TaskHistory; * @returns singleton instance of TaskHistory, null if database is disabled or WASM is enabled */ export function getTaskHistory(): TaskHistory | null { - if (process.env.NX_DISABLE_DB === 'true' || IS_WASM) { + if (!databaseSupportEnabled() || IS_WASM) { return null; }