Skip to content

Commit 0e37963

Browse files
jaysooFrozenPandaz
authored andcommitted
fix(core): cloud commands are noop when not connected rather than errors (#34193)
This PR makes it so Cloud commands like `npx nx record` and `npx nx fix-ci` still work without `nxCloudId`. We'll log a warning so that `ci.yml` using these commands will still work. The warning let's users know that these do not work without being connected. Closes #NXC-3753 (cherry picked from commit 687357c)
1 parent a475a43 commit 0e37963

12 files changed

Lines changed: 316 additions & 238 deletions

File tree

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
import { executeNxCloudCommand } from '../utils';
1+
import { readNxJson } from '../../../config/nx-json';
2+
import { isNxCloudUsed } from '../../../utils/nx-cloud-utils';
3+
import { executeNxCloudCommand, warnNotConnectedToCloud } from '../utils';
24

35
export interface StopAllAgentsArgs {
46
verbose?: boolean;
57
}
68

79
export function stopAllAgentsHandler(args: StopAllAgentsArgs): Promise<number> {
10+
if (!isNxCloudUsed(readNxJson())) {
11+
warnNotConnectedToCloud();
12+
return Promise.resolve(0);
13+
}
814
return executeNxCloudCommand('stop-all-agents', args.verbose);
915
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
import { executeNxCloudCommand } from '../utils';
1+
import { readNxJson } from '../../../config/nx-json';
2+
import { isNxCloudUsed } from '../../../utils/nx-cloud-utils';
3+
import { executeNxCloudCommand, warnNotConnectedToCloud } from '../utils';
24

35
export interface FixCiArgs {
46
verbose?: boolean;
57
}
68

79
export function fixCiHandler(args: FixCiArgs): Promise<number> {
10+
if (!isNxCloudUsed(readNxJson())) {
11+
warnNotConnectedToCloud();
12+
return Promise.resolve(0);
13+
}
814
return executeNxCloudCommand('fix-ci', args.verbose);
915
}

packages/nx/src/command-line/nx-cloud/login/login.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
import { executeNxCloudCommand } from '../utils';
1+
import { readNxJson } from '../../../config/nx-json';
2+
import { isNxCloudUsed } from '../../../utils/nx-cloud-utils';
3+
import { executeNxCloudCommand, warnNotConnectedToCloud } from '../utils';
24

35
export interface LoginArgs {
46
nxCloudUrl?: string;
57
verbose?: boolean;
68
}
79

810
export function loginHandler(args: LoginArgs): Promise<number> {
11+
if (!isNxCloudUsed(readNxJson())) {
12+
warnNotConnectedToCloud();
13+
return Promise.resolve(0);
14+
}
15+
916
if (args.nxCloudUrl) {
1017
process.env.NX_CLOUD_API = args.nxCloudUrl;
1118
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
import { executeNxCloudCommand } from '../utils';
1+
import { readNxJson } from '../../../config/nx-json';
2+
import { isNxCloudUsed } from '../../../utils/nx-cloud-utils';
3+
import { executeNxCloudCommand, warnNotConnectedToCloud } from '../utils';
24

35
export interface LogoutArgs {
46
verbose?: boolean;
57
}
68

79
export function logoutHandler(args: LogoutArgs): Promise<number> {
10+
if (!isNxCloudUsed(readNxJson())) {
11+
warnNotConnectedToCloud();
12+
return Promise.resolve(0);
13+
}
814
return executeNxCloudCommand('logout', args.verbose);
915
}

packages/nx/src/command-line/nx-cloud/record/command-object.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ export const yargsRecordCommand: CommandModule = {
77
'Records a command execution for distributed task execution. This command is an alias for [`nx-cloud record`](/ci/reference/nx-cloud-cli#npx-nxcloud-record).',
88
builder: (yargs) =>
99
withVerbose(yargs)
10+
.parserConfiguration({
11+
'populate--': true,
12+
})
1013
.help(false)
1114
.showHelpOnFail(false)
1215
.option('help', { describe: 'Show help.', type: 'boolean' }),
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,36 @@
1+
import { spawnSync } from 'child_process';
2+
import { readNxJson } from '../../../config/nx-json';
3+
import { output } from '../../../utils/output';
4+
import { isNxCloudUsed } from '../../../utils/nx-cloud-utils';
15
import { executeNxCloudCommand } from '../utils';
26

37
export interface RecordArgs {
48
verbose?: boolean;
9+
'--'?: string[];
510
}
611

712
export function recordHandler(args: RecordArgs): Promise<number> {
13+
if (!isNxCloudUsed(readNxJson())) {
14+
let exitCode: number = 0;
15+
const commandArgs = args['--'];
16+
if (commandArgs && commandArgs.length > 0) {
17+
const [cmd, ...cmdArgs] = commandArgs;
18+
const result = spawnSync(cmd, cmdArgs, {
19+
stdio: 'inherit',
20+
shell: true,
21+
encoding: 'utf-8',
22+
maxBuffer: 10 * 1024 * 1024, // 10MB buffer
23+
windowsHide: true,
24+
});
25+
exitCode = result.status ?? 1;
26+
}
27+
output.warn({
28+
title: 'Nx Cloud is not enabled',
29+
bodyLines: [
30+
'To record command using Nx Cloud, connect your workspace with `nx connect`.',
31+
],
32+
});
33+
return Promise.resolve(exitCode);
34+
}
835
return executeNxCloudCommand('record', args.verbose);
936
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
import { executeNxCloudCommand } from '../utils';
1+
import { readNxJson } from '../../../config/nx-json';
2+
import { isNxCloudUsed } from '../../../utils/nx-cloud-utils';
3+
import { executeNxCloudCommand, warnNotConnectedToCloud } from '../utils';
24

35
export interface StartAgentArgs {
46
verbose?: boolean;
57
}
68

79
export function startAgentHandler(args: StartAgentArgs): Promise<number> {
10+
if (!isNxCloudUsed(readNxJson())) {
11+
warnNotConnectedToCloud();
12+
return Promise.resolve(0);
13+
}
814
return executeNxCloudCommand('start-agent', args.verbose);
915
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
import { executeNxCloudCommand } from '../utils';
1+
import { readNxJson } from '../../../config/nx-json';
2+
import { isNxCloudUsed } from '../../../utils/nx-cloud-utils';
3+
import { executeNxCloudCommand, warnNotConnectedToCloud } from '../utils';
24

35
export interface StartCiRunArgs {
46
verbose?: boolean;
57
}
68

79
export function startCiRunHandler(args: StartCiRunArgs): Promise<number> {
10+
if (!isNxCloudUsed(readNxJson())) {
11+
warnNotConnectedToCloud();
12+
return Promise.resolve(0);
13+
}
814
return executeNxCloudCommand('start-ci-run', args.verbose);
915
}

packages/nx/src/command-line/nx-cloud/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ import { verifyOrUpdateNxCloudClient } from '../../nx-cloud/update-manager';
22
import { getCloudOptions } from '../../nx-cloud/utilities/get-cloud-options';
33
import { handleErrors } from '../../utils/handle-errors';
44
import { findAncestorNodeModules } from '../../nx-cloud/resolution-helpers';
5+
import { output } from '../../utils/output';
6+
7+
export function warnNotConnectedToCloud(): void {
8+
output.warn({
9+
title: 'Nx Cloud is not enabled',
10+
bodyLines: [
11+
'This command requires a connection to the full Nx platform.',
12+
'Run `nx connect` to connect your workspace.',
13+
],
14+
});
15+
}
516

617
export async function executeNxCloudCommand(
718
commandName: string,

0 commit comments

Comments
 (0)