Skip to content

Commit d75bab7

Browse files
MaxKlessFrozenPandaz
authored andcommitted
fix(core): read socket dir on demand & load .env files on client startup (#23348)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> ## Current Behavior The daemon doesn't correctly read the `NX_DAEMON_SOCKET_DIR` env variable if it's set in `.env`. Also, the `.env` files aren't loaded if the daemon client is imported & used directly (like it is in Nx Console). ## Expected Behavior The daemon should correctly read the `NX_DAEMON_SOCKET_DIR` variable regardless of where it's specified and setting it shouldn't cause any issues with Nx Console. ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes nrwl/nx-console#2114 (cherry picked from commit 6c36bef)
1 parent d9c5fe9 commit d75bab7

File tree

6 files changed

+33
-27
lines changed

6 files changed

+33
-27
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { connect } from 'net';
77
import { join } from 'path';
88
import { performance } from 'perf_hooks';
99
import { output } from '../../utils/output';
10-
import { FULL_OS_SOCKET_PATH, killSocketOrPath } from '../socket-utils';
10+
import { getFullOsSocketPath, killSocketOrPath } from '../socket-utils';
1111
import {
1212
DAEMON_DIR_FOR_CURRENT_WORKSPACE,
1313
DAEMON_OUTPUT_LOG_FILE,
@@ -29,6 +29,7 @@ import {
2929
DaemonProjectGraphError,
3030
ProjectGraphError,
3131
} from '../../project-graph/error-types';
32+
import { loadRootEnvFiles } from '../../utils/dotenv';
3233

3334
const DAEMON_ENV_SETTINGS = {
3435
NX_PROJECT_GLOB_CACHE: 'false',
@@ -50,6 +51,8 @@ enum DaemonStatus {
5051
export class DaemonClient {
5152
private readonly nxJson: NxJsonConfiguration | null;
5253
constructor() {
54+
loadRootEnvFiles(workspaceRoot);
55+
5356
try {
5457
this.nxJson = readNxJson();
5558
} catch (e) {
@@ -202,7 +205,7 @@ export class DaemonClient {
202205

203206
await this.queue.sendToQueue(() => {
204207
messenger = new DaemonSocketMessenger(
205-
connect(FULL_OS_SOCKET_PATH)
208+
connect(getFullOsSocketPath())
206209
).listen(
207210
(message) => {
208211
try {
@@ -256,7 +259,7 @@ export class DaemonClient {
256259
async isServerAvailable(): Promise<boolean> {
257260
return new Promise((resolve) => {
258261
try {
259-
const socket = connect(FULL_OS_SOCKET_PATH, () => {
262+
const socket = connect(getFullOsSocketPath(), () => {
260263
socket.destroy();
261264
resolve(true);
262265
});
@@ -277,7 +280,7 @@ export class DaemonClient {
277280

278281
private setUpConnection() {
279282
this.socketMessenger = new DaemonSocketMessenger(
280-
connect(FULL_OS_SOCKET_PATH)
283+
connect(getFullOsSocketPath())
281284
).listen(
282285
(message) => this.handleMessage(message),
283286
() => {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { setupWorkspaceContext } from '../../utils/workspace-context';
1212
import { workspaceRoot } from '../../utils/workspace-root';
1313
import { writeDaemonJsonProcessCache } from '../cache';
1414
import {
15-
FULL_OS_SOCKET_PATH,
15+
getFullOsSocketPath,
1616
isWindows,
1717
killSocketOrPath,
1818
} from '../socket-utils';
@@ -388,9 +388,9 @@ export async function startServer(): Promise<Server> {
388388

389389
return new Promise(async (resolve, reject) => {
390390
try {
391-
server.listen(FULL_OS_SOCKET_PATH, async () => {
391+
server.listen(getFullOsSocketPath(), async () => {
392392
try {
393-
serverLogger.log(`Started listening on: ${FULL_OS_SOCKET_PATH}`);
393+
serverLogger.log(`Started listening on: ${getFullOsSocketPath()}`);
394394
// this triggers the storage of the lock file hash
395395
daemonIsOutdated();
396396

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { workspaceRoot } from '../../utils/workspace-root';
22
import { dirname, relative } from 'path';
3-
import { FULL_OS_SOCKET_PATH } from '../socket-utils';
3+
import { getFullOsSocketPath } from '../socket-utils';
44
import { handleServerProcessTermination } from './shutdown-utils';
55
import { Server } from 'net';
66
import { normalizePath } from '../../utils/path';
@@ -13,7 +13,10 @@ import { platform } from 'os';
1313
import { getDaemonProcessIdSync, serverProcessJsonPath } from '../cache';
1414
import type { WatchEvent } from '../../native';
1515

16-
const ALWAYS_IGNORE = [...getAlwaysIgnore(workspaceRoot), FULL_OS_SOCKET_PATH];
16+
const ALWAYS_IGNORE = [
17+
...getAlwaysIgnore(workspaceRoot),
18+
getFullOsSocketPath(),
19+
];
1720

1821
export type FileWatcherCallback = (
1922
err: Error | string | null,

packages/nx/src/daemon/socket-utils.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { unlinkSync } from 'fs';
22
import { platform } from 'os';
33
import { join, resolve } from 'path';
4-
import { DAEMON_SOCKET_PATH, socketDir } from './tmp-dir';
4+
import { getDaemonSocketDir, getSocketDir } from './tmp-dir';
55
import { createSerializableError } from '../utils/serializable-error';
66

77
export const isWindows = platform() === 'win32';
@@ -12,18 +12,19 @@ export const isWindows = platform() === 'win32';
1212
* See https://nodejs.org/dist/latest-v14.x/docs/api/net.html#net_identifying_paths_for_ipc_connections for a full breakdown
1313
* of OS differences between Unix domain sockets and named pipes.
1414
*/
15-
export const FULL_OS_SOCKET_PATH = isWindows
16-
? '\\\\.\\pipe\\nx\\' + resolve(DAEMON_SOCKET_PATH)
17-
: resolve(DAEMON_SOCKET_PATH);
15+
export const getFullOsSocketPath = () =>
16+
isWindows
17+
? '\\\\.\\pipe\\nx\\' + resolve(getDaemonSocketDir())
18+
: resolve(getDaemonSocketDir());
1819

19-
export const FORKED_PROCESS_OS_SOCKET_PATH = (id: string) => {
20-
let path = resolve(join(socketDir, 'fp' + id + '.sock'));
20+
export const getForkedProcessOsSocketPath = (id: string) => {
21+
let path = resolve(join(getSocketDir(), 'fp' + id + '.sock'));
2122
return isWindows ? '\\\\.\\pipe\\nx\\' + resolve(path) : resolve(path);
2223
};
2324

2425
export function killSocketOrPath(): void {
2526
try {
26-
unlinkSync(FULL_OS_SOCKET_PATH);
27+
unlinkSync(getFullOsSocketPath());
2728
} catch {}
2829
}
2930

packages/nx/src/daemon/tmp-dir.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@ export const DAEMON_OUTPUT_LOG_FILE = join(
2121
'daemon.log'
2222
);
2323

24-
export const socketDir = createSocketDir();
25-
26-
export const DAEMON_SOCKET_PATH = join(
27-
socketDir,
28-
// As per notes above on socket/named pipe length limitations, we keep this intentionally short
29-
'd.sock'
30-
);
24+
export const getDaemonSocketDir = () =>
25+
join(
26+
getSocketDir(),
27+
// As per notes above on socket/named pipe length limitations, we keep this intentionally short
28+
'd.sock'
29+
);
3130

3231
export function writeDaemonLogs(error?: string) {
3332
const file = join(DAEMON_DIR_FOR_CURRENT_WORKSPACE, 'daemon-error.log');
@@ -59,7 +58,7 @@ function socketDirName() {
5958
* We try to create a socket file in a tmp dir, but if it doesn't work because
6059
* for instance we don't have permissions, we create it in DAEMON_DIR_FOR_CURRENT_WORKSPACE
6160
*/
62-
function createSocketDir() {
61+
export function getSocketDir() {
6362
try {
6463
const dir = process.env.NX_DAEMON_SOCKET_DIR ?? socketDirName();
6564
ensureDirSync(dir);
@@ -71,6 +70,6 @@ function createSocketDir() {
7170

7271
export function removeSocketDir() {
7372
try {
74-
rmSync(socketDir, { recursive: true, force: true });
73+
rmSync(getSocketDir(), { recursive: true, force: true });
7574
} catch (e) {}
7675
}

packages/nx/src/tasks-runner/pseudo-terminal.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ChildProcess, RustPseudoTerminal } from '../native';
22
import { PseudoIPCServer } from './pseudo-ipc';
3-
import { FORKED_PROCESS_OS_SOCKET_PATH } from '../daemon/socket-utils';
3+
import { getForkedProcessOsSocketPath } from '../daemon/socket-utils';
44
import { Serializable } from 'child_process';
55
import * as os from 'os';
66

@@ -16,7 +16,7 @@ export function getPseudoTerminal(skipSupportCheck: boolean = false) {
1616
}
1717

1818
export class PseudoTerminal {
19-
private pseudoIPCPath = FORKED_PROCESS_OS_SOCKET_PATH(process.pid.toString());
19+
private pseudoIPCPath = getForkedProcessOsSocketPath(process.pid.toString());
2020
private pseudoIPC = new PseudoIPCServer(this.pseudoIPCPath);
2121

2222
private initialized: boolean = false;

0 commit comments

Comments
 (0)