Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): misc daemon fixes #15472

Merged
merged 21 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/nx/src/command-line/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1524,6 +1524,12 @@ export async function migrate(
process.env.NX_VERBOSE_LOGGING = 'true';
}

try {
Cammisuli marked this conversation as resolved.
Show resolved Hide resolved
runNxSync('daemon --stop', {
cwd: workspaceRoot,
});
} catch {}

return handleErrors(process.env.NX_VERBOSE_LOGGING === 'true', async () => {
const opts = parseMigrationsOptions(args);
if (opts.type === 'generateMigrations') {
Expand Down
17 changes: 14 additions & 3 deletions packages/nx/src/daemon/client/client.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -48,6 +49,8 @@ export class DaemonClient {

private _enabled: boolean | undefined;
private _connected: boolean;
private _out: FileHandle = null;
private _err: FileHandle = null;

enabled() {
if (this._enabled === undefined) {
Expand Down Expand Up @@ -86,12 +89,19 @@ export class DaemonClient {
}

reset() {
this.socketMessenger?.close();
this.socketMessenger = null;
this.queue = new PromisedBasedQueue();
this.currentMessage = null;
this.currentResolve = null;
this.currentReject = null;
this._enabled = undefined;

this._out?.close();
this._err?.close();
this._out = null;
this._err = null;

this._connected = false;
}

Expand Down Expand Up @@ -312,14 +322,15 @@ 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');

const backgroundProcess = spawn(
process.execPath,
[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,
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/daemon/server/shutdown-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export async function handleServerProcessTermination({
}: HandleServerProcessTerminationParams) {
try {
server.close();
deleteDaemonJsonProcessCache();
if (sourceWatcherSubscription) {
await sourceWatcherSubscription.unsubscribe();
serverLogger.watcherLog(
Expand All @@ -63,6 +62,7 @@ export async function handleServerProcessTermination({
);
}
serverLogger.log(`Server stopped because: "${reason}"`);
deleteDaemonJsonProcessCache();
Cammisuli marked this conversation as resolved.
Show resolved Hide resolved
} finally {
process.exit(0);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/daemon/tmp-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/nx/src/project-graph/project-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down