fix(core): ensure daemon enabled check is unchanged (#30228)

This commit is contained in:
Craigory Coppola 2025-02-28 12:13:19 -05:00 committed by GitHub
parent 2986a02dc9
commit 5382c8af65
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 74 additions and 103 deletions

View File

@ -1,12 +1,6 @@
# Function: isDaemonEnabled # Function: isDaemonEnabled
**isDaemonEnabled**(`nxJson?`): `boolean` **isDaemonEnabled**(): `boolean`
#### Parameters
| Name | Type |
| :------- | :----------------------------------------------------------------------------------------- |
| `nxJson` | [`NxJsonConfiguration`](../../devkit/documents/NxJsonConfiguration)\<`string`[] \| `"*"`\> |
#### Returns #### Returns

View File

@ -16,6 +16,7 @@ import { getFullOsSocketPath, killSocketOrPath } from '../socket-utils';
import { import {
DAEMON_DIR_FOR_CURRENT_WORKSPACE, DAEMON_DIR_FOR_CURRENT_WORKSPACE,
DAEMON_OUTPUT_LOG_FILE, DAEMON_OUTPUT_LOG_FILE,
isDaemonDisabled,
removeSocketDir, removeSocketDir,
} from '../tmp-dir'; } from '../tmp-dir';
import { FileData, ProjectGraph } from '../../config/project-graph'; import { FileData, ProjectGraph } from '../../config/project-graph';
@ -95,7 +96,6 @@ import {
POST_TASKS_EXECUTION, POST_TASKS_EXECUTION,
PRE_TASKS_EXECUTION, PRE_TASKS_EXECUTION,
} from '../message-types/run-tasks-execution-hooks'; } from '../message-types/run-tasks-execution-hooks';
import { isDaemonEnabled } from './enabled';
const DAEMON_ENV_SETTINGS = { const DAEMON_ENV_SETTINGS = {
NX_PROJECT_GLOB_CACHE: 'false', NX_PROJECT_GLOB_CACHE: 'false',
@ -141,7 +141,48 @@ export class DaemonClient {
private _err: FileHandle = null; private _err: FileHandle = null;
enabled() { enabled() {
return isDaemonEnabled(this.nxJson); if (this._enabled === undefined) {
const useDaemonProcessOption = this.nxJson?.useDaemonProcess;
const env = process.env.NX_DAEMON;
// env takes precedence
// option=true,env=false => no daemon
// option=false,env=undefined => no daemon
// option=false,env=false => no daemon
// option=undefined,env=undefined => daemon
// option=true,env=true => daemon
// option=false,env=true => daemon
// CI=true,env=undefined => no daemon
// CI=true,env=false => no daemon
// CI=true,env=true => daemon
// docker=true,env=undefined => no daemon
// docker=true,env=false => no daemon
// docker=true,env=true => daemon
// WASM => no daemon because file watching does not work
if (
((isCI() || isDocker()) && env !== 'true') ||
isDaemonDisabled() ||
nxJsonIsNotPresent() ||
(useDaemonProcessOption === undefined && env === 'false') ||
(useDaemonProcessOption === true && env === 'false') ||
(useDaemonProcessOption === false && env === undefined) ||
(useDaemonProcessOption === false && env === 'false')
) {
this._enabled = false;
} else if (IS_WASM) {
output.warn({
title:
'The Nx Daemon is unsupported in WebAssembly environments. Some things may be slower than or not function as expected.',
});
this._enabled = false;
} else {
this._enabled = true;
}
}
return this._enabled;
} }
reset() { reset() {
@ -677,6 +718,27 @@ export class DaemonClient {
export const daemonClient = new DaemonClient(); export const daemonClient = new DaemonClient();
export function isDaemonEnabled() {
return daemonClient.enabled();
}
function isDocker() {
try {
statSync('/.dockerenv');
return true;
} catch {
try {
return readFileSync('/proc/self/cgroup', 'utf8')?.includes('docker');
} catch {}
return false;
}
}
function nxJsonIsNotPresent() {
return !hasNxJson(workspaceRoot);
}
function daemonProcessException(message: string) { function daemonProcessException(message: string) {
try { try {
let log = readFileSync(DAEMON_OUTPUT_LOG_FILE).toString().split('\n'); let log = readFileSync(DAEMON_OUTPUT_LOG_FILE).toString().split('\n');

View File

@ -1,79 +0,0 @@
import {
hasNxJson,
readNxJson,
type NxJsonConfiguration,
} from '../../config/nx-json';
import { readFileSync, statSync } from 'node:fs';
import { isCI } from '../../utils/is-ci';
import { workspaceRoot } from '../../utils/workspace-root';
import { isDaemonDisabled } from '../tmp-dir';
let _enabled: boolean | undefined;
export function isDaemonEnabled(nxJson: NxJsonConfiguration = readNxJson()) {
if (_enabled === undefined) {
const useDaemonProcessOption = nxJson?.useDaemonProcess;
const env = process.env.NX_DAEMON;
// env takes precedence
// option=true,env=false => no daemon
// option=false,env=undefined => no daemon
// option=false,env=false => no daemon
// option=undefined,env=undefined => daemon
// option=true,env=true => daemon
// option=false,env=true => daemon
// CI=true,env=undefined => no daemon
// CI=true,env=false => no daemon
// CI=true,env=true => daemon
// docker=true,env=undefined => no daemon
// docker=true,env=false => no daemon
// docker=true,env=true => daemon
// WASM => no daemon because file watching does not work
if (
((isCI() || isDocker()) && env !== 'true') ||
isDaemonDisabled() ||
nxJsonIsNotPresent() ||
(useDaemonProcessOption === undefined && env === 'false') ||
(useDaemonProcessOption === true && env === 'false') ||
(useDaemonProcessOption === false && env === undefined) ||
(useDaemonProcessOption === false && env === 'false')
) {
_enabled = false;
} else if (
(require('../../native') as typeof import('../../native')).IS_WASM
) {
(
require('../../utils/output') as typeof import('../../utils/output')
).output.warn({
title:
'The Nx Daemon is unsupported in WebAssembly environments. Some things may be slower than or not function as expected.',
});
_enabled = false;
} else {
_enabled = true;
}
}
return _enabled;
}
function isDocker() {
try {
statSync('/.dockerenv');
return true;
} catch {
try {
return readFileSync('/proc/self/cgroup', 'utf8')?.includes('docker');
} catch {}
return false;
}
}
function nxJsonIsNotPresent() {
return !hasNxJson(workspaceRoot);
}

View File

@ -260,4 +260,4 @@ export { cacheDir } from './utils/cache-directory';
*/ */
export { createProjectFileMapUsingProjectGraph } from './project-graph/file-map-utils'; export { createProjectFileMapUsingProjectGraph } from './project-graph/file-map-utils';
export { isDaemonEnabled } from './daemon/client/enabled'; export { isDaemonEnabled } from './daemon/client/client';

View File

@ -17,7 +17,7 @@ import type {
} from './public-api'; } from './public-api';
import { createNodesFromFiles } from './utils'; import { createNodesFromFiles } from './utils';
import { isIsolationEnabled } from './isolation/enabled'; import { isIsolationEnabled } from './isolation/enabled';
import { isDaemonEnabled } from '../../daemon/client/enabled'; import { isDaemonEnabled } from '../../daemon/client/client';
export class LoadedNxPlugin { export class LoadedNxPlugin {
index?: number; index?: number;
@ -123,10 +123,7 @@ export class LoadedNxPlugin {
this.preTasksExecution = async (context: PreTasksExecutionContext) => { this.preTasksExecution = async (context: PreTasksExecutionContext) => {
const updates = {}; const updates = {};
let originalEnv = process.env; let originalEnv = process.env;
if ( if (isIsolationEnabled() || isDaemonEnabled()) {
isIsolationEnabled() ||
isDaemonEnabled(context.nxJsonConfiguration)
) {
process.env = new Proxy<NodeJS.ProcessEnv>(originalEnv, { process.env = new Proxy<NodeJS.ProcessEnv>(originalEnv, {
set: (target, key: string, value) => { set: (target, key: string, value) => {
target[key] = value; target[key] = value;

View File

@ -4,13 +4,12 @@ import type {
} from './public-api'; } from './public-api';
import { getPlugins } from './get-plugins'; import { getPlugins } from './get-plugins';
import { isOnDaemon } from '../../daemon/is-on-daemon'; import { isOnDaemon } from '../../daemon/is-on-daemon';
import { daemonClient } from '../../daemon/client/client'; import { daemonClient, isDaemonEnabled } from '../../daemon/client/client';
import { isDaemonEnabled } from '../../daemon/client/enabled';
export async function runPreTasksExecution( export async function runPreTasksExecution(
pluginContext: PreTasksExecutionContext pluginContext: PreTasksExecutionContext
) { ) {
if (isOnDaemon() || !isDaemonEnabled(pluginContext.nxJsonConfiguration)) { if (isOnDaemon() || !isDaemonEnabled()) {
performance.mark(`preTasksExecution:start`); performance.mark(`preTasksExecution:start`);
const plugins = await getPlugins(pluginContext.workspaceRoot); const plugins = await getPlugins(pluginContext.workspaceRoot);
const envs = await Promise.all( const envs = await Promise.all(
@ -31,7 +30,7 @@ export async function runPreTasksExecution(
}) })
); );
if (!isDaemonEnabled(pluginContext.nxJsonConfiguration)) { if (!isDaemonEnabled()) {
applyProcessEnvs(envs); applyProcessEnvs(envs);
} }
performance.mark(`preTasksExecution:end`); performance.mark(`preTasksExecution:end`);
@ -58,7 +57,7 @@ function applyProcessEnvs(envs: NodeJS.ProcessEnv[]) {
export async function runPostTasksExecution( export async function runPostTasksExecution(
context: PostTasksExecutionContext context: PostTasksExecutionContext
) { ) {
if (isOnDaemon() || !isDaemonEnabled(context.nxJsonConfiguration)) { if (isOnDaemon() || !isDaemonEnabled()) {
performance.mark(`postTasksExecution:start`); performance.mark(`postTasksExecution:start`);
const plugins = await getPlugins(); const plugins = await getPlugins();
await Promise.all( await Promise.all(

View File

@ -1,7 +1,6 @@
import { exec } from 'child_process'; import { exec } from 'child_process';
import type { Compiler } from '@rspack/core'; import type { Compiler } from '@rspack/core';
import { daemonClient } from 'nx/src/daemon/client/client'; import { daemonClient, isDaemonEnabled } from 'nx/src/daemon/client/client';
import { isDaemonEnabled } from 'nx/src/daemon/client/enabled';
import { BatchFunctionRunner } from 'nx/src/command-line/watch/watch'; import { BatchFunctionRunner } from 'nx/src/command-line/watch/watch';
import { output } from 'nx/src/utils/output'; import { output } from 'nx/src/utils/output';

View File

@ -1,9 +1,8 @@
import { exec } from 'child_process'; import { exec } from 'child_process';
import type { Compiler } from 'webpack'; import type { Compiler } from 'webpack';
import { daemonClient } from 'nx/src/daemon/client/client'; import { daemonClient, isDaemonEnabled } from 'nx/src/daemon/client/client';
import { BatchFunctionRunner } from 'nx/src/command-line/watch/watch'; import { BatchFunctionRunner } from 'nx/src/command-line/watch/watch';
import { output } from 'nx/src/utils/output'; import { output } from 'nx/src/utils/output';
import { isDaemonEnabled } from 'nx/src/daemon/client/enabled';
type PluginOptions = { type PluginOptions = {
skipInitialBuild?: boolean; skipInitialBuild?: boolean;