Skip to content

Commit c7bc53d

Browse files
authored
fix(nxls): only make nx cloud requests if in an nx cloud workspace (#2356)
1 parent 270555b commit c7bc53d

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

Diff for: libs/language-server/utils/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export * from './lib/lsp-log';
88
export * from './lib/json-language-service';
99
export * from './lib/find-property';
1010
export * from './lib/executor';
11+
export * from './lib/nx-cloud';

Diff for: libs/language-server/utils/src/lib/nx-cloud.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { importNxPackagePath, readNxJson } from '@nx-console/shared/npm';
2+
import type { NxJsonConfiguration } from 'nx/src/devkit-exports';
3+
import { lspLogger } from './lsp-log';
4+
5+
export async function isNxCloudUsed(workspacePath: string): Promise<boolean> {
6+
const nxJson = await readNxJson(workspacePath);
7+
8+
let getIsNxCloudUsed: (nxJson: NxJsonConfiguration) => boolean;
9+
try {
10+
// try to use nx utils if they exist
11+
const nxCloudUtils = await importNxPackagePath<
12+
typeof import('nx/src/utils/nx-cloud-utils')
13+
>(workspacePath, 'src/utils/nx-cloud-utils', lspLogger);
14+
getIsNxCloudUsed = nxCloudUtils.isNxCloudUsed;
15+
} catch (e) {
16+
// fallback implementation, copied from nx
17+
getIsNxCloudUsed = (nxJson: NxJsonConfiguration) => {
18+
if (process.env.NX_NO_CLOUD === 'true' || nxJson.neverConnectToCloud) {
19+
return false;
20+
}
21+
22+
return (
23+
!!process.env.NX_CLOUD_ACCESS_TOKEN ||
24+
!!nxJson.nxCloudAccessToken ||
25+
!!nxJson.nxCloudId ||
26+
!!Object.values(nxJson.tasksRunnerOptions ?? {}).find(
27+
(r) => r.runner == '@nrwl/nx-cloud' || r.runner == 'nx-cloud'
28+
)
29+
);
30+
};
31+
}
32+
33+
return getIsNxCloudUsed(nxJson);
34+
}

Diff for: libs/language-server/workspace/src/lib/get-cloud-onboarding-info.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { existsSync, readFileSync } from 'fs';
33
import * as os from 'node:os';
44
import { join } from 'path';
55

6-
import { lspLogger } from '@nx-console/language-server/utils';
6+
import { isNxCloudUsed, lspLogger } from '@nx-console/language-server/utils';
77
import {
88
getNxAccessToken,
99
getNxCloudId,
@@ -24,6 +24,16 @@ export async function getCloudOnboardingInfo(
2424
content.includes('nx affected')
2525
);
2626

27+
if (!(await isNxCloudUsed(workspacePath))) {
28+
return {
29+
hasNxInCI,
30+
hasAffectedCommandsInCI,
31+
isConnectedToCloud: false,
32+
isWorkspaceClaimed: false,
33+
personalAccessToken: undefined,
34+
};
35+
}
36+
2737
const accessToken = await getNxAccessToken(workspacePath);
2838
const nxCloudId = await getNxCloudId(workspacePath);
2939
const nxCloudUrl = await getNxCloudUrl(workspacePath);
@@ -185,6 +195,7 @@ async function getNxCloudWorkspaceClaimed(
185195
headers['Nx-Cloud-Personal-Access-Token'] = pat;
186196
}
187197

198+
lspLogger.log(`Making claimed request`);
188199
try {
189200
const response = await xhr({
190201
type: 'POST',

Diff for: libs/language-server/workspace/src/lib/get-recent-cipe-data.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { lspLogger } from '@nx-console/language-server/utils';
1+
import { isNxCloudUsed, lspLogger } from '@nx-console/language-server/utils';
22
import {
33
getNxAccessToken,
44
getNxCloudId,
@@ -14,6 +14,15 @@ export async function getRecentCIPEData(workspacePath: string): Promise<{
1414
error?: CIPEInfoError;
1515
workspaceUrl?: string;
1616
}> {
17+
if (!(await isNxCloudUsed(workspacePath))) {
18+
return {
19+
error: {
20+
type: 'other',
21+
message: 'Nx Cloud is not used in this workspace',
22+
},
23+
};
24+
}
25+
1726
const branches = getRecentlyCommittedGitBranches(workspacePath);
1827

1928
const nxCloudUrl = await getNxCloudUrl(workspacePath);
@@ -44,6 +53,7 @@ export async function getRecentCIPEData(workspacePath: string): Promise<{
4453
headers['Authorization'] = accessToken;
4554
}
4655

56+
lspLogger.log(`Making recent CIPE request`);
4757
try {
4858
const response = await xhr({
4959
type: 'POST',

0 commit comments

Comments
 (0)