Skip to content

Commit

Permalink
refresh nxls after detecting workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKless committed Jun 10, 2024
1 parent bc22879 commit c01398b
Showing 1 changed file with 58 additions and 4 deletions.
62 changes: 58 additions & 4 deletions apps/vscode/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { existsSync } from 'fs';
import { dirname, join, parse } from 'path';
import {
CancellationTokenSource,
Disposable,
ExtensionContext,
ExtensionMode,
Expand Down Expand Up @@ -63,6 +64,12 @@ import {
getOutputChannel,
initOutputChannels,
} from '@nx-console/vscode/output-channels';
import {
NxStopDaemonRequest,
NxWorkspaceRefreshNotification,
NxWorkspaceRequest,
} from '@nx-console/language-server/types';
import { getNxGraphServer } from '@nx-console/vscode/graph-base';

let runTargetTreeView: TreeView<RunTargetTreeItem>;

Expand Down Expand Up @@ -286,14 +293,61 @@ async function registerWorkspaceFileWatcher(

workspaceFileWatcher = watchFile(
new RelativePattern(workspacePath, '{workspace,angular,nx,project}.json'),
() => {
async () => {
if (!isNxWorkspace) {
setTimeout(() => {
setWorkspace(workspacePath);
}, 1000);
await setWorkspace(workspacePath);
if (isNxWorkspace) {
getOutputChannel().appendLine(
'Detected Nx workspace. Refreshing workspace.'
);
refreshWorkspaceWithBackoff();
}
}
}
);

context.subscriptions.push(workspaceFileWatcher);

// when initializing Nx, there can be timing issues as the nxls starts up
// we make sure to refresh the workspace periodically as we start up so that we have the latest info
async function refreshWorkspaceWithBackoff(iteration = 1) {
if (iteration > 3) {
return;
}

await new Promise((resolve) => setTimeout(resolve, 1000 * iteration));

const nxlsClient = getNxlsClient();
if (!nxlsClient) {
return;
}

const workspace = await nxlsClient.sendRequest(NxWorkspaceRequest, {
reset: false,
});

const projects = workspace?.workspace.projects;
if (projects && Object.keys(projects).length > 0) {
return;
} else {
try {
await Promise.race([
nxlsClient.sendRequest(NxStopDaemonRequest, undefined),
new Promise((resolve) => setTimeout(resolve, 2000)),
]);
} catch (e) {
// errors while stopping the daemon aren't critical
}

nxlsClient.sendNotification(NxWorkspaceRefreshNotification);

await new Promise<void>((resolve) => {
const disposable = nxlsClient.subscribeToRefresh(() => {
disposable.dispose();
resolve();
});
});
refreshWorkspaceWithBackoff(iteration + 1);
}
}
}

0 comments on commit c01398b

Please sign in to comment.