Skip to content

Commit bdaa2fd

Browse files
committed
fix(core): disable daemon when nx version mismatches workspace version
Add version check in DaemonClient.enabled() to ensure packages with mismatching nx versions don't use the daemon client.
1 parent 32c7e25 commit bdaa2fd

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

packages/nx/src/daemon/client/client.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { readNxJson } from '../../config/configuration';
2525
import { PromisedBasedQueue } from '../../utils/promised-based-queue';
2626
import { DaemonSocketMessenger, Message } from './daemon-socket-messenger';
2727
import { getDaemonProcessIdSync, readDaemonProcessJsonCache } from '../cache';
28+
import { isNxVersionMismatch } from '../is-nx-version-mismatch';
2829
import { Hash } from '../../hasher/task-hasher';
2930
import { Task, TaskGraph } from '../../config/task-graph';
3031
import { ConfigurationSourceMaps } from '../../project-graph/utils/project-configuration-utils';
@@ -177,7 +178,9 @@ export class DaemonClient {
177178
// docker=true,env=false => no daemon
178179
// docker=true,env=true => daemon
179180
// WASM => no daemon because file watching does not work
181+
// version mismatch => no daemon because the installed nx version differs from the running one
180182
if (
183+
isNxVersionMismatch() ||
181184
((isCI() || isDocker()) && env !== 'true') ||
182185
isDaemonDisabled() ||
183186
nxJsonIsNotPresent() ||
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { readJsonFile } from '../utils/fileutils';
2+
import type { PackageJson } from '../utils/package-json';
3+
import { nxVersion } from '../utils/versions';
4+
import { workspaceRoot } from '../utils/workspace-root';
5+
6+
export function getInstalledNxVersion(): string | null {
7+
try {
8+
const nxPackageJsonPath = require.resolve('nx/package.json', {
9+
paths: [workspaceRoot],
10+
});
11+
const { version } = readJsonFile<PackageJson>(nxPackageJsonPath);
12+
return version;
13+
} catch {
14+
// node modules are absent
15+
return null;
16+
}
17+
}
18+
19+
export function isNxVersionMismatch(): boolean {
20+
return getInstalledNxVersion() !== nxVersion;
21+
}

packages/nx/src/daemon/server/server.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ import {
88
consumeMessagesFromSocket,
99
isJsonMessage,
1010
} from '../../utils/consume-messages-from-socket';
11-
import { readJsonFile } from '../../utils/fileutils';
12-
import { PackageJson } from '../../utils/package-json';
1311
import { nxVersion } from '../../utils/versions';
1412
import { setupWorkspaceContext } from '../../utils/workspace-context';
1513
import { workspaceRoot } from '../../utils/workspace-root';
1614
import { getDaemonProcessIdSync, writeDaemonJsonProcessCache } from '../cache';
15+
import { isNxVersionMismatch } from '../is-nx-version-mismatch';
1716
import {
1817
getFullOsSocketPath,
1918
isWindows,
@@ -533,30 +532,14 @@ function registerProcessTerminationListeners() {
533532
let existingLockHash: string | undefined;
534533

535534
function daemonIsOutdated(): string | null {
536-
if (nxVersionChanged()) {
535+
if (isNxVersionMismatch()) {
537536
return 'NX_VERSION_CHANGED';
538537
} else if (lockFileHashChanged()) {
539538
return 'LOCK_FILES_CHANGED';
540539
}
541540
return null;
542541
}
543542

544-
function nxVersionChanged(): boolean {
545-
return nxVersion !== getInstalledNxVersion();
546-
}
547-
548-
const nxPackageJsonPath = require.resolve('nx/package.json');
549-
550-
function getInstalledNxVersion() {
551-
try {
552-
const { version } = readJsonFile<PackageJson>(nxPackageJsonPath);
553-
return version;
554-
} catch (e) {
555-
// node modules are absent, so we can return null, which would shut down the daemon
556-
return null;
557-
}
558-
}
559-
560543
function lockFileHashChanged(): boolean {
561544
const lockHashes = [
562545
join(workspaceRoot, 'package-lock.json'),

0 commit comments

Comments
 (0)