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

feat: check if nx daemon is enabled #1397

Merged
merged 2 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 13 additions & 2 deletions apps/vscode/src/commands/refresh-workspace.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import { NxWorkspaceRefreshNotification } from '@nx-console/language-server/types';
import { checkIsNxWorkspace } from '@nx-console/shared/utils';
import { sendNotification } from '@nx-console/vscode/lsp-client';
import { getWorkspacePath, outputLogger } from '@nx-console/vscode/utils';
import { debounceTime, Subject } from 'rxjs';
import { commands } from 'vscode';
import { commands, window } from 'vscode';

export const REFRESH_WORKSPACE = 'nxConsole.refreshWorkspace';

const refresh = new Subject();

refresh.pipe(debounceTime(150)).subscribe(async () => {
const { nxWorkspace } = await import('@nx-console/shared/workspace');
await nxWorkspace(getWorkspacePath(), outputLogger, true);
const { daemonEnabled, workspaceType } = await nxWorkspace(
getWorkspacePath(),
outputLogger,
true
);
if (!daemonEnabled && workspaceType === 'nx') {
window.showErrorMessage(
'It looks like the Nx daemon is not enabled.\nPlease check your configuration and restart the daemon with `nx reset`.',
'OK'
);
}
sendNotification(NxWorkspaceRefreshNotification);
commands.executeCommand('nxConsole.refreshNxProjectsTree');
commands.executeCommand('nxConsole.refreshRunTargetTree');
Expand Down
29 changes: 20 additions & 9 deletions libs/shared/workspace/src/lib/get-nx-workspace-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
} from '@nrwl/devkit';
import { join } from 'path';
import {
getNxDaemonClient,
getNxProjectGraph,
getNxWorkspacePackageFileUtils,
} from './get-nx-workspace-package';
Expand All @@ -15,6 +16,8 @@ import { Logger } from '@nx-console/shared/schema';
export type NxWorkspaceConfiguration = ProjectsConfigurations &
NxJsonConfiguration;

let projectGraph: ProjectGraph | null = null;

/**
* There's a couple things that we need to handle here.
*
Expand All @@ -32,6 +35,7 @@ export async function getNxWorkspaceConfig(
): Promise<{
workspaceConfiguration: NxWorkspaceConfiguration;
configPath: string;
daemonEnabled?: boolean;
}> {
const version = await nxVersion(workspacePath);

Expand All @@ -40,11 +44,14 @@ export async function getNxWorkspaceConfig(
}

try {
const [nxWorkspacePackage, nxProjectGraph] = await Promise.all([
getNxWorkspacePackageFileUtils(workspacePath, logger),
getNxProjectGraph(workspacePath, logger),
]);
const [nxWorkspacePackage, nxProjectGraph, nxDaemonClient] =
await Promise.all([
getNxWorkspacePackageFileUtils(workspacePath, logger),
getNxProjectGraph(workspacePath, logger),
getNxDaemonClient(workspacePath, logger),
]);
const configFile = nxWorkspacePackage.workspaceFileName();
const isDaemonEnabled = nxDaemonClient.daemonClient.enabled();

let workspaceConfiguration: NxWorkspaceConfiguration;
try {
Expand All @@ -58,7 +65,6 @@ export async function getNxWorkspaceConfig(
).workspaceConfiguration;
}

let projectGraph: ProjectGraph | null = null;
try {
if (!isNxWorkspace) {
throw 'No project graph support';
Expand All @@ -71,10 +77,14 @@ export async function getNxWorkspaceConfig(
if (version.major < 13) {
projectGraph = (nxProjectGraph as any).createProjectGraph();
} else {
projectGraph = await nxProjectGraph.createProjectGraphAsync({
exitOnError: false,
resetDaemonClient: true,
});
// we always want to try to get the project graph on first load.
// So even if the daemon is disabled, we still want to try to get the project graph
if (isDaemonEnabled || projectGraph === null) {
projectGraph = await nxProjectGraph.createProjectGraphAsync({
exitOnError: false,
resetDaemonClient: true,
});
}
}
} catch {
//noop
Expand All @@ -85,6 +95,7 @@ export async function getNxWorkspaceConfig(
return {
workspaceConfiguration,
configPath: join(workspacePath, configFile),
daemonEnabled: isDaemonEnabled,
};
} catch (e) {
return readWorkspaceConfigs(format, workspacePath);
Expand Down
23 changes: 23 additions & 0 deletions libs/shared/workspace/src/lib/get-nx-workspace-package.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// import { getOutputChannel } from '@nx-console/utils';
import type * as NxFileUtils from 'nx/src/project-graph/file-utils';
import type * as NxProjectGraph from 'nx/src/project-graph/project-graph';
import type * as NxDaemonClient from 'nx/src/daemon/client/client';
import { platform } from 'os';
import { join } from 'path';
import { findNxPackagePath } from '@nx-console/shared/npm';
Expand All @@ -10,6 +11,28 @@ declare function __non_webpack_require__(importPath: string): any;

let RESOLVED_FILEUTILS_IMPORT: typeof NxFileUtils;
let RESOLVED_PROJECTGRAPH_IMPORT: typeof NxProjectGraph;
let RESOLVED_DAEMON_CLIENT: typeof NxDaemonClient;

export async function getNxDaemonClient(
workspacePath: string,
logger: Logger
): Promise<typeof NxDaemonClient> {
if (RESOLVED_DAEMON_CLIENT) {
return RESOLVED_DAEMON_CLIENT;
}

const importPath = await findNxPackagePath(
workspacePath,
join('src', 'daemon', 'client', 'client.js')
);
const backupPackage = await import('nx/src/daemon/client/client');
return getNxPackage(
importPath,
backupPackage,
RESOLVED_DAEMON_CLIENT,
logger
);
}

export async function getNxProjectGraph(
workspacePath: string,
Expand Down
2 changes: 2 additions & 0 deletions libs/shared/workspace/src/lib/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface NxWorkspace {
workspace: NxWorkspaceConfiguration;
workspaceType: 'ng' | 'nx';
configurationFilePath: string;
daemonEnabled?: boolean;
workspacePath: string;
isLerna: boolean;
workspaceLayout: {
Expand Down Expand Up @@ -106,6 +107,7 @@ async function _workspace(
workspaceType: isAngularWorkspace ? 'ng' : 'nx',
workspace: toWorkspaceFormat(config.workspaceConfiguration),
configurationFilePath: config.configPath,
daemonEnabled: config.daemonEnabled,
isLerna,
workspaceLayout: {
appsDir:
Expand Down