Skip to content

Commit

Permalink
fix(core): reset should cleanup temporary nx-cloud files (#23316)
Browse files Browse the repository at this point in the history
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

## Current Behavior
<!-- This is the behavior we have today -->
`nx reset` doesn't remove marker files used by nx cloud.
## Expected Behavior
`nx reset` removes marker files from nx cloud

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes ##23308

(cherry picked from commit b7472fd)
  • Loading branch information
AgentEnder authored and FrozenPandaz committed Jul 24, 2024
1 parent 9b962b6 commit ed4c5a9
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 19 deletions.
6 changes: 6 additions & 0 deletions docs/generated/cli/reset.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ Type: `boolean`

Clears the Nx cache directory. This will remove all local cache entries for tasks, but will not affect the remote cache.

### onlyCloud

Type: `boolean`

Resets the Nx Cloud client. NOTE: Does not clear the remote cache.

### onlyDaemon

Type: `boolean`
Expand Down
6 changes: 6 additions & 0 deletions docs/generated/packages/nx/documents/reset.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ Type: `boolean`

Clears the Nx cache directory. This will remove all local cache entries for tasks, but will not affect the remote cache.

### onlyCloud

Type: `boolean`

Resets the Nx Cloud client. NOTE: Does not clear the remote cache.

### onlyDaemon

Type: `boolean`
Expand Down
32 changes: 13 additions & 19 deletions packages/nx/bin/nx-cloud.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,40 @@
#!/usr/bin/env node

import { findAncestorNodeModules } from '../src/nx-cloud/resolution-helpers';
import { getCloudOptions } from '../src/nx-cloud/utilities/get-cloud-options';
import {
NxCloudClientUnavailableError,
NxCloudEnterpriseOutdatedError,
verifyOrUpdateNxCloudClient,
} from '../src/nx-cloud/update-manager';
import type { CloudTaskRunnerOptions } from '../src/nx-cloud/nx-cloud-tasks-runner-shell';
import { output } from '../src/utils/output';
import {
UnknownCommandError,
getCloudClient,
} from '../src/nx-cloud/utilities/client';

const command = process.argv[2];

const options = getCloudOptions();

Promise.resolve().then(async () => invokeCommandWithNxCloudClient(options));

async function invokeCommandWithNxCloudClient(options: CloudTaskRunnerOptions) {
export async function invokeCommandWithNxCloudClient(
options: CloudTaskRunnerOptions
) {
try {
const { nxCloudClient } = await verifyOrUpdateNxCloudClient(options);

const paths = findAncestorNodeModules(__dirname, []);
nxCloudClient.configureLightClientRequire()(paths);

if (command in nxCloudClient.commands) {
nxCloudClient.commands[command]()
.then(() => process.exit(0))
.catch((e) => {
console.error(e);
process.exit(1);
});
} else {
const client = await getCloudClient(options);
client.invoke(command);
} catch (e: any) {
if (e instanceof UnknownCommandError) {
output.error({
title: `Unknown Command "${command}"`,
title: `Unknown Command "${e.command}"`,
});
output.log({
title: 'Available Commands:',
bodyLines: Object.keys(nxCloudClient.commands).map((c) => `- ${c}`),
bodyLines: e.availableCommands.map((c) => `- ${c}`),
});
process.exit(1);
}
} catch (e: any) {
const body = ['Cannot run commands from the `nx-cloud` CLI.'];

if (e instanceof NxCloudEnterpriseOutdatedError) {
Expand Down
6 changes: 6 additions & 0 deletions packages/nx/src/command-line/reset/command-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type ResetCommandOptions = {
onlyCache?: boolean;
onlyDaemon?: boolean;
onlyWorkspaceData?: boolean;
onlyCloud?: boolean;
};

export const yargsResetCommand: CommandModule<
Expand All @@ -26,6 +27,11 @@ export const yargsResetCommand: CommandModule<
'Stops the Nx Daemon, it will be restarted fresh when the next Nx command is run.',
type: 'boolean',
})
.option('onlyCloud', {
description:
'Resets the Nx Cloud client. NOTE: Does not clear the remote cache.',
type: 'boolean',
})
.option('onlyWorkspaceData', {
description:
'Clears the workspace data directory. Used by Nx to store cached data about the current workspace (e.g. partial results, incremental data, etc)',
Expand Down
14 changes: 14 additions & 0 deletions packages/nx/src/command-line/reset/reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { output } from '../../utils/output';
import { getNativeFileCacheLocation } from '../../native/native-file-cache-location';
import { ResetCommandOptions } from './command-object';

import { getCloudClient } from '../../nx-cloud/utilities/client';
import { getCloudOptions } from '../../nx-cloud/utilities/get-cloud-options';

// Wait at max 5 seconds before giving up on a failing operation.
const INCREMENTAL_BACKOFF_MAX_DURATION = 5000;

Expand Down Expand Up @@ -62,6 +65,9 @@ export async function resetHandler(args: ResetCommandOptions) {
errors.push('Failed to clean up the workspace data directory.');
}
}
if (all || args.onlyCloud) {
await resetCloudClient();
}
if (errors.length > 0) {
output.error({
title: 'Failed to reset the Nx workspace.',
Expand All @@ -79,6 +85,14 @@ function killDaemon() {
return daemonClient.stop();
}

async function resetCloudClient() {
// Remove nx cloud marker files. This helps if the use happens to run `nx-cloud start-ci-run` or
// similar commands on their local machine.
try {
(await getCloudClient(getCloudOptions())).invoke('cleanup');
} catch {}
}

function cleanupCacheEntries() {
return incrementalBackoff(
INCREMENTAL_BACKOFF_FIRST_DELAY,
Expand Down
35 changes: 35 additions & 0 deletions packages/nx/src/nx-cloud/utilities/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { findAncestorNodeModules } from '../resolution-helpers';
import { verifyOrUpdateNxCloudClient } from '../update-manager';
import { CloudTaskRunnerOptions } from '../nx-cloud-tasks-runner-shell';

export class UnknownCommandError extends Error {
constructor(public command: string, public availableCommands: string[]) {
super(`Unknown Command "${command}"`);
}
}

export async function getCloudClient(options: CloudTaskRunnerOptions) {
const { nxCloudClient } = await verifyOrUpdateNxCloudClient(options);

const paths = findAncestorNodeModules(__dirname, []);
nxCloudClient.configureLightClientRequire()(paths);

return {
invoke: (command: string) => {
if (command in nxCloudClient.commands) {
nxCloudClient.commands[command]()
.then(() => process.exit(0))
.catch((e) => {
console.error(e);
process.exit(1);
});
} else {
throw new UnknownCommandError(
command,
Object.keys(nxCloudClient.commands)
);
}
},
availableCommands: Object.keys(nxCloudClient.commands),
};
}

0 comments on commit ed4c5a9

Please sign in to comment.