Skip to content

Commit

Permalink
feat(nxls): decouple daemon lifecycle from nxls lifecycle (#2339)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKless authored Dec 6, 2024
1 parent 3de7f68 commit 8ed9385
Show file tree
Hide file tree
Showing 11 changed files with 465 additions and 192 deletions.
2 changes: 1 addition & 1 deletion apps/nxls-e2e/src/completion/nx-json-completion-16.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,6 @@ describe('nx.json completion - 16', () => {
});

afterAll(async () => {
return await nxlsWrapper.stopNxls();
return await nxlsWrapper.stopNxls('16');
});
});
2 changes: 1 addition & 1 deletion apps/nxls-e2e/src/nx-cloud/nx-cloud-16.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,6 @@ describe('nx cloud - nx 16', () => {
});

afterAll(async () => {
return await nxlsWrapper.stopNxls();
return await nxlsWrapper.stopNxls('16');
});
});
2 changes: 1 addition & 1 deletion apps/nxls-e2e/src/nx-workspace/nx-workspace-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export function testNxWorkspace(
});

afterAll(async () => {
return await nxlsWrapper.stopNxls();
return await nxlsWrapper.stopNxls(version);
});
});
}
15 changes: 13 additions & 2 deletions apps/nxls-e2e/src/nxls-wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChildProcess, spawn } from 'child_process';
import { ChildProcess, execSync, spawn } from 'child_process';
import { join } from 'path';
import {
Message,
Expand All @@ -12,8 +12,10 @@ import {
} from 'vscode-languageserver/node';

import { killTree } from '@nx-console/shared/utils';
import { defaultVersion } from './utils';

export class NxlsWrapper {
private cwd?: string;
private messageReader?: StreamMessageReader;
private messageWriter?: StreamMessageWriter;
private process?: ChildProcess;
Expand Down Expand Up @@ -41,6 +43,7 @@ export class NxlsWrapper {
private idCounter = 1;

async startNxls(cwd: string) {
this.cwd = cwd;
try {
const nxlsPath = join(
__dirname,
Expand Down Expand Up @@ -92,7 +95,7 @@ export class NxlsWrapper {
}
}

async stopNxls() {
async stopNxls(version?: string) {
await this.sendRequest({
method: 'shutdown',
});
Expand Down Expand Up @@ -134,6 +137,14 @@ export class NxlsWrapper {

this.process?.removeListener('exit', this.earlyExitListener);

try {
execSync(`npx nx@${version ?? defaultVersion} daemon --stop`, {
cwd: this.cwd,
});
} catch (e) {
console.error(e);
}

if (this.process?.pid) {
try {
killTree(this.process.pid, 'SIGKILL');
Expand Down
9 changes: 0 additions & 9 deletions apps/nxls/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ import {
getGeneratorOptions,
getGenerators,
getNxCloudStatus,
getNxDaemonClient,
getNxVersion,
getPDVData,
getProjectByPath,
Expand Down Expand Up @@ -299,14 +298,6 @@ documents.onDidOpen(async (e) => {
connection.onShutdown(async () => {
unregisterFileWatcher();
jsonDocumentMapper.dispose();

if (WORKING_PATH) {
const nxDaemonClientModule = await getNxDaemonClient(
WORKING_PATH,
lspLogger
);
await nxDaemonClientModule?.daemonClient?.stop();
}
});

connection.onExit(() => {
Expand Down
2 changes: 0 additions & 2 deletions libs/language-server/watcher/src/lib/daemon-watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ export class DaemonWatcher {
if (!this.stopped) {
lspLogger.log('Daemon watcher connection closed, restarting');
this.tryRestartWatcher();
} else {
lspLogger.log('Daemon watcher connection closed');
}
} else if (error) {
lspLogger.log('Error watching files: ' + error);
Expand Down
14 changes: 14 additions & 0 deletions libs/language-server/workspace/src/lib/get-nx-workspace-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
getNxProjectGraph,
getNxProjectGraphUtils,
} from './get-nx-workspace-package';
import { getPackageManagerCommand } from '@nx-console/shared/utils';
import { execSync } from 'child_process';

let _defaultProcessExit: typeof process.exit;

Expand Down Expand Up @@ -87,6 +89,18 @@ export async function getNxWorkspaceConfig(
}
}

if (
nxDaemonClientModule &&
nxDaemonClientModule.daemonClient?.enabled() &&
!(await nxDaemonClientModule.daemonClient?.isServerAvailable())
) {
const pm = await getPackageManagerCommand(workspacePath, logger);
execSync(`${pm.exec} nx daemon --start`, {
cwd: workspacePath,
windowsHide: true,
});
}

try {
_defaultProcessExit = process.exit;
process.exit = function (code?: number) {
Expand Down
2 changes: 1 addition & 1 deletion libs/vscode/lsp-client/src/lib/nxls-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class NxlsClient {
}

public async refreshWorkspace() {
window.withProgress(
await window.withProgress(
{
location: ProgressLocation.Notification,
title: 'Refreshing Workspace',
Expand Down
1 change: 0 additions & 1 deletion nx.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"useDaemonProcess": true,
"tasksRunnerOptions": {
"default": {
"options": {
Expand Down
28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,22 @@
"devDependencies": {
"@babel/core": "7.21.0",
"@chromatic-com/storybook": "^1",
"@nx/cypress": "20.1.3",
"@nx/devkit": "20.1.3",
"@nx/esbuild": "20.1.3",
"@nx/eslint": "20.1.3",
"@nx/eslint-plugin": "20.1.3",
"@nx/gradle": "20.1.3",
"@nx/jest": "20.1.3",
"@nx/js": "20.1.3",
"@nx/node": "20.1.3",
"@nx/cypress": "20.1.4",
"@nx/devkit": "20.1.4",
"@nx/esbuild": "20.1.4",
"@nx/eslint": "20.1.4",
"@nx/eslint-plugin": "20.1.4",
"@nx/gradle": "20.1.4",
"@nx/jest": "20.1.4",
"@nx/js": "20.1.4",
"@nx/node": "20.1.4",
"@nx/powerpack-conformance": "1.1.0-beta.5",
"@nx/powerpack-enterprise-cloud": "1.1.0-beta.5",
"@nx/powerpack-owners": "1.1.0-beta.5",
"@nx/storybook": "20.1.3",
"@nx/web": "20.1.3",
"@nx/webpack": "20.1.3",
"@nx/workspace": "20.1.3",
"@nx/storybook": "20.1.4",
"@nx/web": "20.1.4",
"@nx/webpack": "20.1.4",
"@nx/workspace": "20.1.4",
"@schematics/angular": "18.1.0",
"@storybook/addon-essentials": "^8.3.1",
"@storybook/addon-knobs": "^8.0.1",
Expand Down Expand Up @@ -117,7 +117,7 @@
"jest-preset-angular": "14.1.1",
"memfs": "^3.4.7",
"mocha": "^10.0.0",
"nx": "20.1.3",
"nx": "20.1.4",
"ovsx": "^0.7.1",
"prettier": "2.7.1",
"prettier-plugin-tailwindcss": "^0.3.0",
Expand Down
Loading

0 comments on commit 8ed9385

Please sign in to comment.