From 9286e6229b271e606f54efa56a0aeb25303bd62f Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Mon, 6 Mar 2023 10:11:26 -0500 Subject: [PATCH 01/21] fix(core): stop the daemon before doing migrations --- packages/nx/src/command-line/migrate.ts | 3 +++ packages/nx/src/hasher/file-hasher.ts | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/packages/nx/src/command-line/migrate.ts b/packages/nx/src/command-line/migrate.ts index e843029e541b8..d4eb4a2af9ade 100644 --- a/packages/nx/src/command-line/migrate.ts +++ b/packages/nx/src/command-line/migrate.ts @@ -1524,6 +1524,9 @@ export async function migrate( process.env.NX_VERBOSE_LOGGING = 'true'; } + const { daemonClient } = await import('../daemon/client/client'); + daemonClient.stop(); + return handleErrors(process.env.NX_VERBOSE_LOGGING === 'true', async () => { const opts = parseMigrationsOptions(args); if (opts.type === 'generateMigrations') { diff --git a/packages/nx/src/hasher/file-hasher.ts b/packages/nx/src/hasher/file-hasher.ts index e78c1698a518d..be1b510d3b994 100644 --- a/packages/nx/src/hasher/file-hasher.ts +++ b/packages/nx/src/hasher/file-hasher.ts @@ -9,6 +9,11 @@ import { NativeFileHasher } from './native-file-hasher'; function createFileHasher(): FileHasherBase { try { + const dotEnv: typeof import('dotenv') = require('dotenv'); + try { + dotEnv.config(); + } catch {} + if ( !process.env.NX_NON_NATIVE_HASHER || process.env.NX_NON_NATIVE_HASHER != 'true' From df7b63d17433327e098826b313a9c811b4a70b8b Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Mon, 6 Mar 2023 11:26:06 -0500 Subject: [PATCH 02/21] fix(core): use execSync to stop the daemon --- packages/nx/src/command-line/migrate.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/nx/src/command-line/migrate.ts b/packages/nx/src/command-line/migrate.ts index d4eb4a2af9ade..5d04cf7cccd8b 100644 --- a/packages/nx/src/command-line/migrate.ts +++ b/packages/nx/src/command-line/migrate.ts @@ -1524,8 +1524,11 @@ export async function migrate( process.env.NX_VERBOSE_LOGGING = 'true'; } - const { daemonClient } = await import('../daemon/client/client'); - daemonClient.stop(); + try { + execSync('nx daemon --stop', { + cwd: workspaceRoot, + }); + } catch {} return handleErrors(process.env.NX_VERBOSE_LOGGING === 'true', async () => { const opts = parseMigrationsOptions(args); From 929dcc93e6da6916c77124af62c9799aec6fc477 Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Mon, 6 Mar 2023 11:36:35 -0500 Subject: [PATCH 03/21] fix(core): use runNxSync instead of execSync --- packages/nx/src/command-line/migrate.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/nx/src/command-line/migrate.ts b/packages/nx/src/command-line/migrate.ts index 5d04cf7cccd8b..74d04b17ebe7e 100644 --- a/packages/nx/src/command-line/migrate.ts +++ b/packages/nx/src/command-line/migrate.ts @@ -1525,9 +1525,9 @@ export async function migrate( } try { - execSync('nx daemon --stop', { - cwd: workspaceRoot, - }); + runNxSync("daemon --stop", { + cwd: workspaceRoot + }) } catch {} return handleErrors(process.env.NX_VERBOSE_LOGGING === 'true', async () => { From 89274afd17472efb758663054e58fcff3306f82b Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Mon, 6 Mar 2023 11:49:56 -0500 Subject: [PATCH 04/21] chore(core): fix formatting --- packages/nx/src/command-line/migrate.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/nx/src/command-line/migrate.ts b/packages/nx/src/command-line/migrate.ts index 74d04b17ebe7e..6f93b59926ab8 100644 --- a/packages/nx/src/command-line/migrate.ts +++ b/packages/nx/src/command-line/migrate.ts @@ -1525,9 +1525,9 @@ export async function migrate( } try { - runNxSync("daemon --stop", { - cwd: workspaceRoot - }) + runNxSync('daemon --stop', { + cwd: workspaceRoot, + }); } catch {} return handleErrors(process.env.NX_VERBOSE_LOGGING === 'true', async () => { From 094033dfd2089968a1204ed09ef043d45cc127d7 Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Mon, 6 Mar 2023 17:09:48 -0500 Subject: [PATCH 05/21] fix(core): close client connection after sending message to daemon --- packages/nx/src/daemon/client/client.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/nx/src/daemon/client/client.ts b/packages/nx/src/daemon/client/client.ts index 72b9fac6c59b0..50451851c31ab 100644 --- a/packages/nx/src/daemon/client/client.ts +++ b/packages/nx/src/daemon/client/client.ts @@ -188,9 +188,18 @@ export class DaemonClient { } private async sendToDaemonViaQueue(messageToDaemon: Message): Promise { - return this.queue.sendToQueue(() => - this.sendMessageToDaemon(messageToDaemon) - ); + return this.queue + .sendToQueue(() => this.sendMessageToDaemon(messageToDaemon)) + .then(() => { + if (this.queue.isEmpty()) { + this.closeConnection(); + } + }); + } + + private closeConnection() { + this.socketMessenger.close(); + this._connected = false; } private setUpConnection() { From 9bf0bd9870b6e1774ac17deb6832649b826b7da5 Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Mon, 6 Mar 2023 18:06:36 -0500 Subject: [PATCH 06/21] fix(core): review changes --- packages/nx/src/daemon/client/client.ts | 1 - packages/nx/src/hasher/file-hasher.ts | 5 ----- 2 files changed, 6 deletions(-) diff --git a/packages/nx/src/daemon/client/client.ts b/packages/nx/src/daemon/client/client.ts index 50451851c31ab..3821f536dd896 100644 --- a/packages/nx/src/daemon/client/client.ts +++ b/packages/nx/src/daemon/client/client.ts @@ -199,7 +199,6 @@ export class DaemonClient { private closeConnection() { this.socketMessenger.close(); - this._connected = false; } private setUpConnection() { diff --git a/packages/nx/src/hasher/file-hasher.ts b/packages/nx/src/hasher/file-hasher.ts index be1b510d3b994..e78c1698a518d 100644 --- a/packages/nx/src/hasher/file-hasher.ts +++ b/packages/nx/src/hasher/file-hasher.ts @@ -9,11 +9,6 @@ import { NativeFileHasher } from './native-file-hasher'; function createFileHasher(): FileHasherBase { try { - const dotEnv: typeof import('dotenv') = require('dotenv'); - try { - dotEnv.config(); - } catch {} - if ( !process.env.NX_NON_NATIVE_HASHER || process.env.NX_NON_NATIVE_HASHER != 'true' From f4836b3e3046f750e263e9a4d4bee89cc9b101a1 Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Mon, 6 Mar 2023 18:15:44 -0500 Subject: [PATCH 07/21] fix(core): return value --- packages/nx/src/daemon/client/client.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/nx/src/daemon/client/client.ts b/packages/nx/src/daemon/client/client.ts index 3821f536dd896..098a4ff9bc22e 100644 --- a/packages/nx/src/daemon/client/client.ts +++ b/packages/nx/src/daemon/client/client.ts @@ -190,10 +190,11 @@ export class DaemonClient { private async sendToDaemonViaQueue(messageToDaemon: Message): Promise { return this.queue .sendToQueue(() => this.sendMessageToDaemon(messageToDaemon)) - .then(() => { + .then((value) => { if (this.queue.isEmpty()) { this.closeConnection(); } + return value; }); } From d1156739cffa839a66341c5b60606550ad069c0f Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Thu, 9 Mar 2023 14:13:00 -0500 Subject: [PATCH 08/21] fix(core): timeout the disconnect --- packages/nx/src/daemon/client/client.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/nx/src/daemon/client/client.ts b/packages/nx/src/daemon/client/client.ts index 098a4ff9bc22e..9df0b2a4bdf49 100644 --- a/packages/nx/src/daemon/client/client.ts +++ b/packages/nx/src/daemon/client/client.ts @@ -191,9 +191,11 @@ export class DaemonClient { return this.queue .sendToQueue(() => this.sendMessageToDaemon(messageToDaemon)) .then((value) => { - if (this.queue.isEmpty()) { - this.closeConnection(); - } + setTimeout(() => { + if (this.queue.isEmpty()) { + this.closeConnection(); + } + }, 0); return value; }); } From b05b5bdcb786ce8f14a9fab20004cd0d65bd5752 Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Fri, 10 Mar 2023 12:39:23 -0500 Subject: [PATCH 09/21] fix(core): increase timeout --- packages/nx/src/daemon/client/client.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nx/src/daemon/client/client.ts b/packages/nx/src/daemon/client/client.ts index 9df0b2a4bdf49..0413e662df796 100644 --- a/packages/nx/src/daemon/client/client.ts +++ b/packages/nx/src/daemon/client/client.ts @@ -195,7 +195,7 @@ export class DaemonClient { if (this.queue.isEmpty()) { this.closeConnection(); } - }, 0); + }, 100); return value; }); } From ac8ed1f2d66019338844c03364418d040d7b13c0 Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Fri, 10 Mar 2023 13:21:09 -0500 Subject: [PATCH 10/21] fix(core): add a onQueueEmpty callback --- packages/nx/src/daemon/client/client.ts | 20 ++++++------------ packages/nx/src/utils/promised-based-queue.ts | 21 +++++++++++++++---- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/packages/nx/src/daemon/client/client.ts b/packages/nx/src/daemon/client/client.ts index 0413e662df796..5970b64b4934e 100644 --- a/packages/nx/src/daemon/client/client.ts +++ b/packages/nx/src/daemon/client/client.ts @@ -88,6 +88,9 @@ export class DaemonClient { reset() { this.socketMessenger = null; this.queue = new PromisedBasedQueue(); + this.queue.onQueueEmpty(() => { + this.socketMessenger.close(); + }); this.currentMessage = null; this.currentResolve = null; this.currentReject = null; @@ -188,20 +191,9 @@ export class DaemonClient { } private async sendToDaemonViaQueue(messageToDaemon: Message): Promise { - return this.queue - .sendToQueue(() => this.sendMessageToDaemon(messageToDaemon)) - .then((value) => { - setTimeout(() => { - if (this.queue.isEmpty()) { - this.closeConnection(); - } - }, 100); - return value; - }); - } - - private closeConnection() { - this.socketMessenger.close(); + return this.queue.sendToQueue(() => + this.sendMessageToDaemon(messageToDaemon) + ); } private setUpConnection() { diff --git a/packages/nx/src/utils/promised-based-queue.ts b/packages/nx/src/utils/promised-based-queue.ts index 63a6be74bc037..bee40757afb6c 100644 --- a/packages/nx/src/utils/promised-based-queue.ts +++ b/packages/nx/src/utils/promised-based-queue.ts @@ -2,6 +2,8 @@ export class PromisedBasedQueue { private counter = 0; private promise = Promise.resolve(null); + private emptyCallback: () => void = null; + sendToQueue(fn: () => Promise): Promise { this.counter++; let res, rej; @@ -14,19 +16,19 @@ export class PromisedBasedQueue { .then(async () => { try { res(await fn()); - this.counter--; + this._promiseResolved(); } catch (e) { rej(e); - this.counter--; + this._promiseResolved(); } }) .catch(async () => { try { res(await fn()); - this.counter--; + this._promiseResolved(); } catch (e) { rej(e); - this.counter--; + this._promiseResolved(); } }); return r; @@ -35,4 +37,15 @@ export class PromisedBasedQueue { isEmpty() { return this.counter === 0; } + + onQueueEmpty(callback: () => void): void { + this.emptyCallback = callback; + } + + private _promiseResolved() { + this.counter--; + if (this.isEmpty()) { + this.emptyCallback(); + } + } } From 73e2cc1d54c4265ffbbd905497d46a7e613dfaa0 Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Fri, 10 Mar 2023 13:37:00 -0500 Subject: [PATCH 11/21] fix(core): handle null callback --- packages/nx/src/utils/promised-based-queue.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nx/src/utils/promised-based-queue.ts b/packages/nx/src/utils/promised-based-queue.ts index bee40757afb6c..cc5bd177af9c8 100644 --- a/packages/nx/src/utils/promised-based-queue.ts +++ b/packages/nx/src/utils/promised-based-queue.ts @@ -45,7 +45,7 @@ export class PromisedBasedQueue { private _promiseResolved() { this.counter--; if (this.isEmpty()) { - this.emptyCallback(); + this.emptyCallback?.(); } } } From e8c0abbf45592d5b0faaa97b6c3266f9d4f643ca Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Fri, 10 Mar 2023 14:59:10 -0500 Subject: [PATCH 12/21] Revert "fix(core): handle null callback" This reverts commit 4add0bd7514f07e914d100eeb9c615fd34f21291. --- packages/nx/src/utils/promised-based-queue.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nx/src/utils/promised-based-queue.ts b/packages/nx/src/utils/promised-based-queue.ts index cc5bd177af9c8..bee40757afb6c 100644 --- a/packages/nx/src/utils/promised-based-queue.ts +++ b/packages/nx/src/utils/promised-based-queue.ts @@ -45,7 +45,7 @@ export class PromisedBasedQueue { private _promiseResolved() { this.counter--; if (this.isEmpty()) { - this.emptyCallback?.(); + this.emptyCallback(); } } } From 732a919adbf31cf6b0affd22e9b8ab914092ce47 Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Fri, 10 Mar 2023 14:59:17 -0500 Subject: [PATCH 13/21] Revert "fix(core): add a onQueueEmpty callback" This reverts commit ef9233c57d38cbdcdf5016586f9a597b3731936f. --- packages/nx/src/daemon/client/client.ts | 20 ++++++++++++------ packages/nx/src/utils/promised-based-queue.ts | 21 ++++--------------- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/packages/nx/src/daemon/client/client.ts b/packages/nx/src/daemon/client/client.ts index 5970b64b4934e..0413e662df796 100644 --- a/packages/nx/src/daemon/client/client.ts +++ b/packages/nx/src/daemon/client/client.ts @@ -88,9 +88,6 @@ export class DaemonClient { reset() { this.socketMessenger = null; this.queue = new PromisedBasedQueue(); - this.queue.onQueueEmpty(() => { - this.socketMessenger.close(); - }); this.currentMessage = null; this.currentResolve = null; this.currentReject = null; @@ -191,9 +188,20 @@ export class DaemonClient { } private async sendToDaemonViaQueue(messageToDaemon: Message): Promise { - return this.queue.sendToQueue(() => - this.sendMessageToDaemon(messageToDaemon) - ); + return this.queue + .sendToQueue(() => this.sendMessageToDaemon(messageToDaemon)) + .then((value) => { + setTimeout(() => { + if (this.queue.isEmpty()) { + this.closeConnection(); + } + }, 100); + return value; + }); + } + + private closeConnection() { + this.socketMessenger.close(); } private setUpConnection() { diff --git a/packages/nx/src/utils/promised-based-queue.ts b/packages/nx/src/utils/promised-based-queue.ts index bee40757afb6c..63a6be74bc037 100644 --- a/packages/nx/src/utils/promised-based-queue.ts +++ b/packages/nx/src/utils/promised-based-queue.ts @@ -2,8 +2,6 @@ export class PromisedBasedQueue { private counter = 0; private promise = Promise.resolve(null); - private emptyCallback: () => void = null; - sendToQueue(fn: () => Promise): Promise { this.counter++; let res, rej; @@ -16,19 +14,19 @@ export class PromisedBasedQueue { .then(async () => { try { res(await fn()); - this._promiseResolved(); + this.counter--; } catch (e) { rej(e); - this._promiseResolved(); + this.counter--; } }) .catch(async () => { try { res(await fn()); - this._promiseResolved(); + this.counter--; } catch (e) { rej(e); - this._promiseResolved(); + this.counter--; } }); return r; @@ -37,15 +35,4 @@ export class PromisedBasedQueue { isEmpty() { return this.counter === 0; } - - onQueueEmpty(callback: () => void): void { - this.emptyCallback = callback; - } - - private _promiseResolved() { - this.counter--; - if (this.isEmpty()) { - this.emptyCallback(); - } - } } From d0e3acfe146832c032110ab4258fe364d7abe46c Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Fri, 10 Mar 2023 15:06:19 -0500 Subject: [PATCH 14/21] fix(core): change order of reset daemon client --- packages/nx/src/daemon/client/client.ts | 17 +++-------------- packages/nx/src/project-graph/project-graph.ts | 3 ++- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/packages/nx/src/daemon/client/client.ts b/packages/nx/src/daemon/client/client.ts index 0413e662df796..72b9fac6c59b0 100644 --- a/packages/nx/src/daemon/client/client.ts +++ b/packages/nx/src/daemon/client/client.ts @@ -188,20 +188,9 @@ export class DaemonClient { } private async sendToDaemonViaQueue(messageToDaemon: Message): Promise { - return this.queue - .sendToQueue(() => this.sendMessageToDaemon(messageToDaemon)) - .then((value) => { - setTimeout(() => { - if (this.queue.isEmpty()) { - this.closeConnection(); - } - }, 100); - return value; - }); - } - - private closeConnection() { - this.socketMessenger.close(); + return this.queue.sendToQueue(() => + this.sendMessageToDaemon(messageToDaemon) + ); } private setUpConnection() { diff --git a/packages/nx/src/project-graph/project-graph.ts b/packages/nx/src/project-graph/project-graph.ts index 07f07f0a9aacc..b690de2c46532 100644 --- a/packages/nx/src/project-graph/project-graph.ts +++ b/packages/nx/src/project-graph/project-graph.ts @@ -130,10 +130,11 @@ export async function createProjectGraphAsync( } } else { try { + const projectGraph = await daemonClient.getProjectGraph(); if (opts.resetDaemonClient) { daemonClient.reset(); } - return await daemonClient.getProjectGraph(); + return projectGraph; } catch (e) { if (e.message.indexOf('inotify_add_watch') > -1) { // common errors with the daemon due to OS settings (cannot watch all the files available) From a4236f8e5ccb4f2a921ece0e47c1de6dde6afb6f Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Fri, 10 Mar 2023 15:24:34 -0500 Subject: [PATCH 15/21] fix(core): close the socket connection before nullifying the messenger --- packages/nx/src/daemon/client/client.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/nx/src/daemon/client/client.ts b/packages/nx/src/daemon/client/client.ts index 72b9fac6c59b0..3e65dc8f52810 100644 --- a/packages/nx/src/daemon/client/client.ts +++ b/packages/nx/src/daemon/client/client.ts @@ -86,6 +86,7 @@ export class DaemonClient { } reset() { + this.socketMessenger?.close(); this.socketMessenger = null; this.queue = new PromisedBasedQueue(); this.currentMessage = null; From 5319c43508dd7e31e557479b1f9880c6efaae665 Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Wed, 15 Mar 2023 10:51:23 -0400 Subject: [PATCH 16/21] fix(core): always use host node for spawning the daemon --- package.json | 5 +++-- packages/nx/package.json | 1 + packages/nx/src/daemon/client/client.ts | 6 +++++- packages/nx/src/daemon/server/shutdown-utils.ts | 2 +- packages/nx/src/daemon/tmp-dir.ts | 2 +- yarn.lock | 7 ++++++- 6 files changed, 17 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index ce5765979fc45..e36ca466b34b1 100644 --- a/package.json +++ b/package.json @@ -117,6 +117,7 @@ "@types/semver": "^7.3.8", "@types/tar-stream": "^2.2.2", "@types/tmp": "^0.2.0", + "@types/which": "^2.0.2", "@types/yargs": "^17.0.10", "@types/yarnpkg__lockfile": "^1.1.5", "@typescript-eslint/eslint-plugin": "5.38.1", @@ -314,7 +315,8 @@ "tailwindcss": "3.2.4", "tslib": "^2.3.0", "vitest": "^0.25.8", - "weak-napi": "^2.0.2" + "weak-napi": "^2.0.2", + "which": "^3.0.0" }, "resolutions": { "**/xmlhttprequest-ssl": "~1.6.2", @@ -334,4 +336,3 @@ ] } } - diff --git a/packages/nx/package.json b/packages/nx/package.json index 1a4bc29f0ce84..4b37c4fd84718 100644 --- a/packages/nx/package.json +++ b/packages/nx/package.json @@ -63,6 +63,7 @@ "tsconfig-paths": "^4.1.2", "tslib": "^2.3.0", "v8-compile-cache": "2.3.0", + "which": "^3.0.0", "yargs": "^17.6.2", "yargs-parser": "21.1.1" }, diff --git a/packages/nx/src/daemon/client/client.ts b/packages/nx/src/daemon/client/client.ts index 3e65dc8f52810..1dc89355ffb78 100644 --- a/packages/nx/src/daemon/client/client.ts +++ b/packages/nx/src/daemon/client/client.ts @@ -5,6 +5,7 @@ import { ensureDirSync, ensureFileSync } from 'fs-extra'; import { connect } from 'net'; import { join } from 'path'; import { performance } from 'perf_hooks'; +import * as which from 'which'; import { output } from '../../utils/output'; import { FULL_OS_SOCKET_PATH, killSocketOrPath } from '../socket-utils'; import { @@ -315,8 +316,11 @@ export class DaemonClient { const out = openSync(DAEMON_OUTPUT_LOG_FILE, 'a'); const err = openSync(DAEMON_OUTPUT_LOG_FILE, 'a'); + // Find the node path based on the path. + // This allows us to use the node version based on the host os, rather than other host processes + const nodeExecutable = await which("node") const backgroundProcess = spawn( - process.execPath, + nodeExecutable, [join(__dirname, '../server/start.js')], { cwd: workspaceRoot, diff --git a/packages/nx/src/daemon/server/shutdown-utils.ts b/packages/nx/src/daemon/server/shutdown-utils.ts index 2ffb3a01061e1..34abfb2c4b293 100644 --- a/packages/nx/src/daemon/server/shutdown-utils.ts +++ b/packages/nx/src/daemon/server/shutdown-utils.ts @@ -43,7 +43,6 @@ export async function handleServerProcessTermination({ }: HandleServerProcessTerminationParams) { try { server.close(); - deleteDaemonJsonProcessCache(); if (sourceWatcherSubscription) { await sourceWatcherSubscription.unsubscribe(); serverLogger.watcherLog( @@ -63,6 +62,7 @@ export async function handleServerProcessTermination({ ); } serverLogger.log(`Server stopped because: "${reason}"`); + deleteDaemonJsonProcessCache(); } finally { process.exit(0); } diff --git a/packages/nx/src/daemon/tmp-dir.ts b/packages/nx/src/daemon/tmp-dir.ts index d335198422552..459b2fa4ff258 100644 --- a/packages/nx/src/daemon/tmp-dir.ts +++ b/packages/nx/src/daemon/tmp-dir.ts @@ -50,7 +50,7 @@ export function isDaemonDisabled() { function socketDirName() { const hasher = createHash('sha256'); - hasher.update(workspaceRoot); + hasher.update(workspaceRoot.toLowerCase()); const unique = hasher.digest('hex').substring(0, 20); return join(tmpdir, unique); } diff --git a/yarn.lock b/yarn.lock index 0d69d0e018c9e..1daee185fcef3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8047,6 +8047,11 @@ anymatch "^3.0.0" source-map "^0.6.0" +"@types/which@^2.0.2": + version "2.0.2" + resolved "http://localhost:4873/@types%2fwhich/-/which-2.0.2.tgz#54541d02d6b1daee5ec01ac0d1b37cecf37db1ae" + integrity sha512-113D3mDkZDjo+EeUEHCFy0qniNc1ZpecGiAU7WSo7YDoSzolZIQKpYFHrPpjkB2nuyahcKfrmLXeQlh7gqJYdw== + "@types/ws@^8.5.1": version "8.5.3" resolved "https://registry.yarnpkg.com/@types/ws/-/ws-8.5.3.tgz#7d25a1ffbecd3c4f2d35068d0b283c037003274d" @@ -26020,7 +26025,7 @@ which@^2.0.1, which@^2.0.2: which@^3.0.0: version "3.0.0" - resolved "https://registry.yarnpkg.com/which/-/which-3.0.0.tgz#a9efd016db59728758a390d23f1687b6e8f59f8e" + resolved "http://localhost:4873/which/-/which-3.0.0.tgz#a9efd016db59728758a390d23f1687b6e8f59f8e" integrity sha512-nla//68K9NU6yRiwDY/Q8aU6siKlSs64aEC7+IV56QoAuyQT2ovsJcgGYGyqMOmI/CGN1BOR6mM5EN0FBO+zyQ== dependencies: isexe "^2.0.0" From 459049c1120d3e43d8897bc0d685ac407b6e4166 Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Wed, 15 Mar 2023 12:15:30 -0400 Subject: [PATCH 17/21] fix(core): close daemon.log handle on reset --- packages/nx/src/daemon/client/client.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/nx/src/daemon/client/client.ts b/packages/nx/src/daemon/client/client.ts index 1dc89355ffb78..470bdc8f1bba9 100644 --- a/packages/nx/src/daemon/client/client.ts +++ b/packages/nx/src/daemon/client/client.ts @@ -1,6 +1,7 @@ import { workspaceRoot } from '../../utils/workspace-root'; import { ChildProcess, spawn } from 'child_process'; import { openSync, readFileSync, statSync } from 'fs'; +import { FileHandle, open } from 'fs/promises'; import { ensureDirSync, ensureFileSync } from 'fs-extra'; import { connect } from 'net'; import { join } from 'path'; @@ -49,6 +50,8 @@ export class DaemonClient { private _enabled: boolean | undefined; private _connected: boolean; + private _out: FileHandle = null; + private _err: FileHandle = null; enabled() { if (this._enabled === undefined) { @@ -94,6 +97,12 @@ export class DaemonClient { this.currentResolve = null; this.currentReject = null; this._enabled = undefined; + + this._out?.close(); + this._err?.close(); + this._out = null; + this._err = null; + this._connected = false; } @@ -314,17 +323,17 @@ export class DaemonClient { ensureDirSync(DAEMON_DIR_FOR_CURRENT_WORKSPACE); ensureFileSync(DAEMON_OUTPUT_LOG_FILE); - const out = openSync(DAEMON_OUTPUT_LOG_FILE, 'a'); - const err = openSync(DAEMON_OUTPUT_LOG_FILE, 'a'); + this._out = await open(DAEMON_OUTPUT_LOG_FILE, 'a'); + this._err = await open(DAEMON_OUTPUT_LOG_FILE, 'a'); // Find the node path based on the path. // This allows us to use the node version based on the host os, rather than other host processes - const nodeExecutable = await which("node") + const nodeExecutable = await which('node'); const backgroundProcess = spawn( nodeExecutable, [join(__dirname, '../server/start.js')], { cwd: workspaceRoot, - stdio: ['ignore', out, err], + stdio: ['ignore', this._out.fd, this._err.fd], detached: true, windowsHide: true, shell: false, From ada101395c3df277e6416eb82be90f257f47dc89 Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Wed, 15 Mar 2023 12:32:55 -0400 Subject: [PATCH 18/21] fix(core): fix yarn.lock --- yarn.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/yarn.lock b/yarn.lock index 1daee185fcef3..5f6eed19b894e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8049,7 +8049,7 @@ "@types/which@^2.0.2": version "2.0.2" - resolved "http://localhost:4873/@types%2fwhich/-/which-2.0.2.tgz#54541d02d6b1daee5ec01ac0d1b37cecf37db1ae" + resolved "https://registry.yarnpkg.com/@types%2fwhich/-/which-2.0.2.tgz#54541d02d6b1daee5ec01ac0d1b37cecf37db1ae" integrity sha512-113D3mDkZDjo+EeUEHCFy0qniNc1ZpecGiAU7WSo7YDoSzolZIQKpYFHrPpjkB2nuyahcKfrmLXeQlh7gqJYdw== "@types/ws@^8.5.1": @@ -26025,7 +26025,7 @@ which@^2.0.1, which@^2.0.2: which@^3.0.0: version "3.0.0" - resolved "http://localhost:4873/which/-/which-3.0.0.tgz#a9efd016db59728758a390d23f1687b6e8f59f8e" + resolved "https://registry.yarnpkg.com/which/-/which-3.0.0.tgz#a9efd016db59728758a390d23f1687b6e8f59f8e" integrity sha512-nla//68K9NU6yRiwDY/Q8aU6siKlSs64aEC7+IV56QoAuyQT2ovsJcgGYGyqMOmI/CGN1BOR6mM5EN0FBO+zyQ== dependencies: isexe "^2.0.0" From a72e06779554dd39721537cd0f1d29e3cb96c3da Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Wed, 15 Mar 2023 13:30:31 -0400 Subject: [PATCH 19/21] fix(core): fix yarn.lock --- yarn.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yarn.lock b/yarn.lock index 5f6eed19b894e..cd9921a5365d0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8049,7 +8049,7 @@ "@types/which@^2.0.2": version "2.0.2" - resolved "https://registry.yarnpkg.com/@types%2fwhich/-/which-2.0.2.tgz#54541d02d6b1daee5ec01ac0d1b37cecf37db1ae" + resolved "https://registry.yarnpkg.com/@types/which/-/which-2.0.2.tgz#54541d02d6b1daee5ec01ac0d1b37cecf37db1ae" integrity sha512-113D3mDkZDjo+EeUEHCFy0qniNc1ZpecGiAU7WSo7YDoSzolZIQKpYFHrPpjkB2nuyahcKfrmLXeQlh7gqJYdw== "@types/ws@^8.5.1": From 5038b20cf09d2336aec04c15ff98b59b90843238 Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Thu, 16 Mar 2023 08:09:36 -0400 Subject: [PATCH 20/21] chore(core): remove which --- package.json | 5 ++--- packages/nx/package.json | 1 - packages/nx/src/daemon/client/client.ts | 7 ++----- yarn.lock | 5 ----- 4 files changed, 4 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index e36ca466b34b1..ce5765979fc45 100644 --- a/package.json +++ b/package.json @@ -117,7 +117,6 @@ "@types/semver": "^7.3.8", "@types/tar-stream": "^2.2.2", "@types/tmp": "^0.2.0", - "@types/which": "^2.0.2", "@types/yargs": "^17.0.10", "@types/yarnpkg__lockfile": "^1.1.5", "@typescript-eslint/eslint-plugin": "5.38.1", @@ -315,8 +314,7 @@ "tailwindcss": "3.2.4", "tslib": "^2.3.0", "vitest": "^0.25.8", - "weak-napi": "^2.0.2", - "which": "^3.0.0" + "weak-napi": "^2.0.2" }, "resolutions": { "**/xmlhttprequest-ssl": "~1.6.2", @@ -336,3 +334,4 @@ ] } } + diff --git a/packages/nx/package.json b/packages/nx/package.json index 4b37c4fd84718..1a4bc29f0ce84 100644 --- a/packages/nx/package.json +++ b/packages/nx/package.json @@ -63,7 +63,6 @@ "tsconfig-paths": "^4.1.2", "tslib": "^2.3.0", "v8-compile-cache": "2.3.0", - "which": "^3.0.0", "yargs": "^17.6.2", "yargs-parser": "21.1.1" }, diff --git a/packages/nx/src/daemon/client/client.ts b/packages/nx/src/daemon/client/client.ts index 470bdc8f1bba9..f42fb18badb23 100644 --- a/packages/nx/src/daemon/client/client.ts +++ b/packages/nx/src/daemon/client/client.ts @@ -6,7 +6,6 @@ import { ensureDirSync, ensureFileSync } from 'fs-extra'; import { connect } from 'net'; import { join } from 'path'; import { performance } from 'perf_hooks'; -import * as which from 'which'; import { output } from '../../utils/output'; import { FULL_OS_SOCKET_PATH, killSocketOrPath } from '../socket-utils'; import { @@ -325,11 +324,9 @@ export class DaemonClient { this._out = await open(DAEMON_OUTPUT_LOG_FILE, 'a'); this._err = await open(DAEMON_OUTPUT_LOG_FILE, 'a'); - // Find the node path based on the path. - // This allows us to use the node version based on the host os, rather than other host processes - const nodeExecutable = await which('node'); + const backgroundProcess = spawn( - nodeExecutable, + process.execPath, [join(__dirname, '../server/start.js')], { cwd: workspaceRoot, diff --git a/yarn.lock b/yarn.lock index cd9921a5365d0..0d69d0e018c9e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8047,11 +8047,6 @@ anymatch "^3.0.0" source-map "^0.6.0" -"@types/which@^2.0.2": - version "2.0.2" - resolved "https://registry.yarnpkg.com/@types/which/-/which-2.0.2.tgz#54541d02d6b1daee5ec01ac0d1b37cecf37db1ae" - integrity sha512-113D3mDkZDjo+EeUEHCFy0qniNc1ZpecGiAU7WSo7YDoSzolZIQKpYFHrPpjkB2nuyahcKfrmLXeQlh7gqJYdw== - "@types/ws@^8.5.1": version "8.5.3" resolved "https://registry.yarnpkg.com/@types/ws/-/ws-8.5.3.tgz#7d25a1ffbecd3c4f2d35068d0b283c037003274d" From 870093465b478cb96d39bbba68f1327dc44a2402 Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Thu, 16 Mar 2023 16:40:16 -0400 Subject: [PATCH 21/21] chore(core): review changes --- packages/nx/src/command-line/migrate.ts | 7 ++----- packages/nx/src/daemon/server/shutdown-utils.ts | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/packages/nx/src/command-line/migrate.ts b/packages/nx/src/command-line/migrate.ts index 6f93b59926ab8..544bc9d059914 100644 --- a/packages/nx/src/command-line/migrate.ts +++ b/packages/nx/src/command-line/migrate.ts @@ -56,6 +56,7 @@ import { isCI } from '../utils/is-ci'; import { getNxRequirePaths } from '../utils/installation-directory'; import { readNxJson } from '../config/configuration'; import { runNxSync } from '../utils/child-process'; +import { daemonClient } from '../daemon/client/client'; export interface ResolvedMigrationConfiguration extends MigrationsJson { packageGroup?: ArrayPackageGroup; @@ -1524,11 +1525,7 @@ export async function migrate( process.env.NX_VERBOSE_LOGGING = 'true'; } - try { - runNxSync('daemon --stop', { - cwd: workspaceRoot, - }); - } catch {} + await daemonClient.stop(); return handleErrors(process.env.NX_VERBOSE_LOGGING === 'true', async () => { const opts = parseMigrationsOptions(args); diff --git a/packages/nx/src/daemon/server/shutdown-utils.ts b/packages/nx/src/daemon/server/shutdown-utils.ts index 34abfb2c4b293..2ffb3a01061e1 100644 --- a/packages/nx/src/daemon/server/shutdown-utils.ts +++ b/packages/nx/src/daemon/server/shutdown-utils.ts @@ -43,6 +43,7 @@ export async function handleServerProcessTermination({ }: HandleServerProcessTerminationParams) { try { server.close(); + deleteDaemonJsonProcessCache(); if (sourceWatcherSubscription) { await sourceWatcherSubscription.unsubscribe(); serverLogger.watcherLog( @@ -62,7 +63,6 @@ export async function handleServerProcessTermination({ ); } serverLogger.log(`Server stopped because: "${reason}"`); - deleteDaemonJsonProcessCache(); } finally { process.exit(0); }