feat(core): add support for skipping remote cache (#29574)

<!-- 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` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

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

Fixes #
This commit is contained in:
Jonathan Cammisuli 2025-01-15 10:33:47 -05:00 committed by GitHub
parent 0d5e53d60e
commit d46761d764
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 72 additions and 5 deletions

View File

@ -103,6 +103,7 @@ Print the task graph to the console:
| `--parallel` | string | Max number of parallel processes [default is 3]. | | `--parallel` | string | Max number of parallel processes [default is 3]. |
| `--runner` | string | This is the name of the tasks runner configured in nx.json. | | `--runner` | string | This is the name of the tasks runner configured in nx.json. |
| `--skipNxCache` | boolean | Rerun the tasks even when the results are available in the cache. (Default: `false`) | | `--skipNxCache` | boolean | Rerun the tasks even when the results are available in the cache. (Default: `false`) |
| `--skipRemoteCache` | boolean | Disables the remote cache. (Default: `false`) |
| `--skipSync` | boolean | Skips running the sync generators associated with the tasks. (Default: `false`) | | `--skipSync` | boolean | Skips running the sync generators associated with the tasks. (Default: `false`) |
| `--targets` | string | Tasks to run for affected projects. | | `--targets` | string | Tasks to run for affected projects. |
| `--uncommitted` | boolean | Uncommitted changes. | | `--uncommitted` | boolean | Uncommitted changes. |

View File

@ -131,6 +131,7 @@ nx release publish
| `--registry` | string | The registry to publish to. | | `--registry` | string | The registry to publish to. |
| `--runner` | string | This is the name of the tasks runner configured in nx.json. | | `--runner` | string | This is the name of the tasks runner configured in nx.json. |
| `--skipNxCache` | boolean | Rerun the tasks even when the results are available in the cache. (Default: `false`) | | `--skipNxCache` | boolean | Rerun the tasks even when the results are available in the cache. (Default: `false`) |
| `--skipRemoteCache` | boolean | Disables the remote cache. (Default: `false`) |
| `--skipSync` | boolean | Skips running the sync generators associated with the tasks. (Default: `false`) | | `--skipSync` | boolean | Skips running the sync generators associated with the tasks. (Default: `false`) |
| `--tag` | string | The distribution tag to apply to the published package. | | `--tag` | string | The distribution tag to apply to the published package. |
| `--verbose` | boolean | Prints additional information about the commands (e.g., stack traces). | | `--verbose` | boolean | Prints additional information about the commands (e.g., stack traces). |

View File

@ -107,6 +107,7 @@ Print the task graph to the console:
| `--projects` | string | Projects to run. (comma/space delimited project names and/or patterns). | | `--projects` | string | Projects to run. (comma/space delimited project names and/or patterns). |
| `--runner` | string | This is the name of the tasks runner configured in nx.json. | | `--runner` | string | This is the name of the tasks runner configured in nx.json. |
| `--skipNxCache` | boolean | Rerun the tasks even when the results are available in the cache. (Default: `false`) | | `--skipNxCache` | boolean | Rerun the tasks even when the results are available in the cache. (Default: `false`) |
| `--skipRemoteCache` | boolean | Disables the remote cache. (Default: `false`) |
| `--skipSync` | boolean | Skips running the sync generators associated with the tasks. (Default: `false`) | | `--skipSync` | boolean | Skips running the sync generators associated with the tasks. (Default: `false`) |
| `--targets` | string | Tasks to run for affected projects. | | `--targets` | string | Tasks to run for affected projects. |
| `--verbose` | boolean | Prints additional information about the commands (e.g., stack traces). | | `--verbose` | boolean | Prints additional information about the commands (e.g., stack traces). |

View File

@ -81,6 +81,7 @@ Run's a target named build:test for the myapp project. Note the quotes around th
| `--project` | string | Target project. | | `--project` | string | Target project. |
| `--runner` | string | This is the name of the tasks runner configured in nx.json. | | `--runner` | string | This is the name of the tasks runner configured in nx.json. |
| `--skipNxCache` | boolean | Rerun the tasks even when the results are available in the cache. (Default: `false`) | | `--skipNxCache` | boolean | Rerun the tasks even when the results are available in the cache. (Default: `false`) |
| `--skipRemoteCache` | boolean | Disables the remote cache. (Default: `false`) |
| `--skipSync` | boolean | Skips running the sync generators associated with the tasks. (Default: `false`) | | `--skipSync` | boolean | Skips running the sync generators associated with the tasks. (Default: `false`) |
| `--verbose` | boolean | Prints additional information about the commands (e.g., stack traces). | | `--verbose` | boolean | Prints additional information about the commands (e.g., stack traces). |
| `--version` | boolean | Show version number. | | `--version` | boolean | Show version number. |

View File

@ -14,6 +14,7 @@
- [remoteCache](../../devkit/documents/DefaultTasksRunnerOptions#remotecache): RemoteCache - [remoteCache](../../devkit/documents/DefaultTasksRunnerOptions#remotecache): RemoteCache
- [runtimeCacheInputs](../../devkit/documents/DefaultTasksRunnerOptions#runtimecacheinputs): string[] - [runtimeCacheInputs](../../devkit/documents/DefaultTasksRunnerOptions#runtimecacheinputs): string[]
- [skipNxCache](../../devkit/documents/DefaultTasksRunnerOptions#skipnxcache): boolean - [skipNxCache](../../devkit/documents/DefaultTasksRunnerOptions#skipnxcache): boolean
- [skipRemoteCache](../../devkit/documents/DefaultTasksRunnerOptions#skipremotecache): boolean
## Properties ## Properties
@ -74,3 +75,9 @@
### skipNxCache ### skipNxCache
`Optional` **skipNxCache**: `boolean` `Optional` **skipNxCache**: `boolean`
---
### skipRemoteCache
`Optional` **skipRemoteCache**: `boolean`

View File

@ -103,6 +103,7 @@ Print the task graph to the console:
| `--parallel` | string | Max number of parallel processes [default is 3]. | | `--parallel` | string | Max number of parallel processes [default is 3]. |
| `--runner` | string | This is the name of the tasks runner configured in nx.json. | | `--runner` | string | This is the name of the tasks runner configured in nx.json. |
| `--skipNxCache` | boolean | Rerun the tasks even when the results are available in the cache. (Default: `false`) | | `--skipNxCache` | boolean | Rerun the tasks even when the results are available in the cache. (Default: `false`) |
| `--skipRemoteCache` | boolean | Disables the remote cache. (Default: `false`) |
| `--skipSync` | boolean | Skips running the sync generators associated with the tasks. (Default: `false`) | | `--skipSync` | boolean | Skips running the sync generators associated with the tasks. (Default: `false`) |
| `--targets` | string | Tasks to run for affected projects. | | `--targets` | string | Tasks to run for affected projects. |
| `--uncommitted` | boolean | Uncommitted changes. | | `--uncommitted` | boolean | Uncommitted changes. |

View File

@ -131,6 +131,7 @@ nx release publish
| `--registry` | string | The registry to publish to. | | `--registry` | string | The registry to publish to. |
| `--runner` | string | This is the name of the tasks runner configured in nx.json. | | `--runner` | string | This is the name of the tasks runner configured in nx.json. |
| `--skipNxCache` | boolean | Rerun the tasks even when the results are available in the cache. (Default: `false`) | | `--skipNxCache` | boolean | Rerun the tasks even when the results are available in the cache. (Default: `false`) |
| `--skipRemoteCache` | boolean | Disables the remote cache. (Default: `false`) |
| `--skipSync` | boolean | Skips running the sync generators associated with the tasks. (Default: `false`) | | `--skipSync` | boolean | Skips running the sync generators associated with the tasks. (Default: `false`) |
| `--tag` | string | The distribution tag to apply to the published package. | | `--tag` | string | The distribution tag to apply to the published package. |
| `--verbose` | boolean | Prints additional information about the commands (e.g., stack traces). | | `--verbose` | boolean | Prints additional information about the commands (e.g., stack traces). |

View File

@ -107,6 +107,7 @@ Print the task graph to the console:
| `--projects` | string | Projects to run. (comma/space delimited project names and/or patterns). | | `--projects` | string | Projects to run. (comma/space delimited project names and/or patterns). |
| `--runner` | string | This is the name of the tasks runner configured in nx.json. | | `--runner` | string | This is the name of the tasks runner configured in nx.json. |
| `--skipNxCache` | boolean | Rerun the tasks even when the results are available in the cache. (Default: `false`) | | `--skipNxCache` | boolean | Rerun the tasks even when the results are available in the cache. (Default: `false`) |
| `--skipRemoteCache` | boolean | Disables the remote cache. (Default: `false`) |
| `--skipSync` | boolean | Skips running the sync generators associated with the tasks. (Default: `false`) | | `--skipSync` | boolean | Skips running the sync generators associated with the tasks. (Default: `false`) |
| `--targets` | string | Tasks to run for affected projects. | | `--targets` | string | Tasks to run for affected projects. |
| `--verbose` | boolean | Prints additional information about the commands (e.g., stack traces). | | `--verbose` | boolean | Prints additional information about the commands (e.g., stack traces). |

View File

@ -81,6 +81,7 @@ Run's a target named build:test for the myapp project. Note the quotes around th
| `--project` | string | Target project. | | `--project` | string | Target project. |
| `--runner` | string | This is the name of the tasks runner configured in nx.json. | | `--runner` | string | This is the name of the tasks runner configured in nx.json. |
| `--skipNxCache` | boolean | Rerun the tasks even when the results are available in the cache. (Default: `false`) | | `--skipNxCache` | boolean | Rerun the tasks even when the results are available in the cache. (Default: `false`) |
| `--skipRemoteCache` | boolean | Disables the remote cache. (Default: `false`) |
| `--skipSync` | boolean | Skips running the sync generators associated with the tasks. (Default: `false`) | | `--skipSync` | boolean | Skips running the sync generators associated with the tasks. (Default: `false`) |
| `--verbose` | boolean | Prints additional information about the commands (e.g., stack traces). | | `--verbose` | boolean | Prints additional information about the commands (e.g., stack traces). |
| `--version` | boolean | Show version number. | | `--version` | boolean | Show version number. |

View File

@ -32,6 +32,7 @@ export interface RunOptions {
nxBail: boolean; nxBail: boolean;
nxIgnoreCycles: boolean; nxIgnoreCycles: boolean;
skipNxCache: boolean; skipNxCache: boolean;
skipRemoteCache: boolean;
cloud: boolean; cloud: boolean;
dte: boolean; dte: boolean;
batch: boolean; batch: boolean;
@ -89,6 +90,13 @@ export function withRunOptions<T>(yargs: Argv<T>): Argv<T & RunOptions> {
'Rerun the tasks even when the results are available in the cache.', 'Rerun the tasks even when the results are available in the cache.',
type: 'boolean', type: 'boolean',
default: false, default: false,
alias: 'disableNxCache',
})
.options('skipRemoteCache', {
type: 'boolean',
describe: 'Disables the remote cache.',
default: false,
alias: 'disableRemoteCache',
}) })
.options('excludeTaskDependencies', { .options('excludeTaskDependencies', {
describe: 'Skips running dependent tasks first.', describe: 'Skips running dependent tasks first.',
@ -104,6 +112,7 @@ export function withRunOptions<T>(yargs: Argv<T>): Argv<T & RunOptions> {
type: 'boolean', type: 'boolean',
hidden: true, hidden: true,
}) })
.options('dte', { .options('dte', {
type: 'boolean', type: 'boolean',
hidden: true, hidden: true,

View File

@ -83,6 +83,7 @@ export function getCache(options: DefaultTasksRunnerOptions): DbCache | Cache {
? new DbCache({ ? new DbCache({
// Remove this in Nx 21 // Remove this in Nx 21
nxCloudRemoteCache: isNxCloudUsed(nxJson) ? options.remoteCache : null, nxCloudRemoteCache: isNxCloudUsed(nxJson) ? options.remoteCache : null,
skipRemoteCache: options.skipRemoteCache,
}) })
: new Cache(options); : new Cache(options);
} }
@ -95,7 +96,12 @@ export class DbCache {
private isVerbose = process.env.NX_VERBOSE_LOGGING === 'true'; private isVerbose = process.env.NX_VERBOSE_LOGGING === 'true';
constructor(private readonly options: { nxCloudRemoteCache: RemoteCache }) {} constructor(
private readonly options: {
nxCloudRemoteCache: RemoteCache;
skipRemoteCache?: boolean;
}
) {}
async init() { async init() {
// This should be cheap because we've already loaded // This should be cheap because we've already loaded
@ -185,6 +191,16 @@ export class DbCache {
} }
private async _getRemoteCache(): Promise<RemoteCacheV2 | null> { private async _getRemoteCache(): Promise<RemoteCacheV2 | null> {
if (this.options.skipRemoteCache) {
output.warn({
title: 'Remote Cache Disabled',
bodyLines: [
'Nx will continue running, but nothing will be written or read from the remote cache.',
],
});
return null;
}
const nxJson = readNxJson(); const nxJson = readNxJson();
if (isNxCloudUsed(nxJson)) { if (isNxCloudUsed(nxJson)) {
const options = getCloudOptions(); const options = getCloudOptions();
@ -270,7 +286,16 @@ export class Cache {
private _currentMachineId: string = null; private _currentMachineId: string = null;
constructor(private readonly options: DefaultTasksRunnerOptions) {} constructor(private readonly options: DefaultTasksRunnerOptions) {
if (this.options.skipRemoteCache) {
output.warn({
title: 'Remote Cache Disabled',
bodyLines: [
'Nx will continue running, but nothing will be written or read from the remote cache.',
],
});
}
}
removeOldCacheRecords() { removeOldCacheRecords() {
/** /**
@ -315,7 +340,7 @@ export class Cache {
if (res) { if (res) {
await this.assertLocalCacheValidity(task); await this.assertLocalCacheValidity(task);
return { ...res, remote: false }; return { ...res, remote: false };
} else if (this.options.remoteCache) { } else if (this.options.remoteCache && !this.options.skipRemoteCache) {
// didn't find it locally but we have a remote cache // didn't find it locally but we have a remote cache
// attempt remote cache // attempt remote cache
await this.options.remoteCache.retrieve(task.hash, this.cachePath); await this.options.remoteCache.retrieve(task.hash, this.cachePath);
@ -370,7 +395,7 @@ export class Cache {
await writeFile(join(td, 'source'), await this.currentMachineId()); await writeFile(join(td, 'source'), await this.currentMachineId());
await writeFile(tdCommit, 'true'); await writeFile(tdCommit, 'true');
if (this.options.remoteCache) { if (this.options.remoteCache && !this.options.skipRemoteCache) {
await this.options.remoteCache.store(task.hash, this.cachePath); await this.options.remoteCache.store(task.hash, this.cachePath);
} }

View File

@ -75,6 +75,7 @@ export interface DefaultTasksRunnerOptions {
lifeCycle: LifeCycle; lifeCycle: LifeCycle;
captureStderr?: boolean; captureStderr?: boolean;
skipNxCache?: boolean; skipNxCache?: boolean;
skipRemoteCache?: boolean;
batch?: boolean; batch?: boolean;
} }

View File

@ -37,6 +37,7 @@ describe('splitArgs', () => {
base: 'sha1', base: 'sha1',
head: 'sha2', head: 'sha2',
skipNxCache: false, skipNxCache: false,
skipRemoteCache: false,
}); });
}); });
@ -68,6 +69,7 @@ describe('splitArgs', () => {
).toEqual({ ).toEqual({
base: 'main', base: 'main',
skipNxCache: false, skipNxCache: false,
skipRemoteCache: false,
}); });
}); });
@ -85,6 +87,7 @@ describe('splitArgs', () => {
).toEqual({ ).toEqual({
base: 'develop', base: 'develop',
skipNxCache: false, skipNxCache: false,
skipRemoteCache: false,
}); });
}); });
@ -102,6 +105,7 @@ describe('splitArgs', () => {
).toEqual({ ).toEqual({
base: 'main', base: 'main',
skipNxCache: false, skipNxCache: false,
skipRemoteCache: false,
}); });
}); });
@ -193,6 +197,7 @@ describe('splitArgs', () => {
).toEqual({ ).toEqual({
projects: ['aaa', 'bbb'], projects: ['aaa', 'bbb'],
skipNxCache: false, skipNxCache: false,
skipRemoteCache: false,
}); });
}); });
@ -217,6 +222,7 @@ describe('splitArgs', () => {
base: 'envVarSha1', base: 'envVarSha1',
head: 'envVarSha2', head: 'envVarSha2',
skipNxCache: false, skipNxCache: false,
skipRemoteCache: false,
}); });
expect( expect(
@ -234,6 +240,7 @@ describe('splitArgs', () => {
base: 'envVarSha1', base: 'envVarSha1',
head: 'directlyOnCommandSha1', head: 'directlyOnCommandSha1',
skipNxCache: false, skipNxCache: false,
skipRemoteCache: false,
}); });
expect( expect(
@ -251,6 +258,7 @@ describe('splitArgs', () => {
base: 'directlyOnCommandSha2', base: 'directlyOnCommandSha2',
head: 'envVarSha2', head: 'envVarSha2',
skipNxCache: false, skipNxCache: false,
skipRemoteCache: false,
}); });
} }
); );

View File

@ -35,6 +35,7 @@ export interface NxArgs {
select?: string; select?: string;
graph?: string | boolean; graph?: string | boolean;
skipNxCache?: boolean; skipNxCache?: boolean;
skipRemoteCache?: boolean;
outputStyle?: string; outputStyle?: string;
nxBail?: boolean; nxBail?: boolean;
nxIgnoreCycles?: boolean; nxIgnoreCycles?: boolean;
@ -182,7 +183,15 @@ export function splitArgsIntoNxArgsAndOverrides(
} }
if (!nxArgs.skipNxCache) { if (!nxArgs.skipNxCache) {
nxArgs.skipNxCache = process.env.NX_SKIP_NX_CACHE === 'true'; nxArgs.skipNxCache =
process.env.NX_SKIP_NX_CACHE === 'true' ||
process.env.NX_DISABLE_NX_CACHE === 'true';
}
if (!nxArgs.skipRemoteCache) {
nxArgs.skipRemoteCache =
process.env.NX_DISABLE_REMOTE_CACHE === 'true' ||
process.env.NX_SKIP_REMOTE_CACHE === 'true';
} }
normalizeNxArgsRunner(nxArgs, nxJson, options); normalizeNxArgsRunner(nxArgs, nxJson, options);